Guest User

Untitled

a guest
Dec 15th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JComboBox;
  6. import javax.swing.JFrame;
  7.  
  8. public class Layout {
  9.  
  10. private JFrame frame;
  11. private JButton addBut;
  12. private Sentinela sentinela;
  13.  
  14. private String[] optionsArray= {"Static class listener", "Inner class listener", "Local class listener", "Anonymous class listener"};
  15. private JComboBox<String> optionsList = new JComboBox<String>(optionsArray);
  16.  
  17. public Layout() {
  18. frame = new JFrame("Add Buttons");
  19. frame.setSize(400, 500);
  20. addBut = new JButton("Add new Button");
  21. sentinela = new Sentinela();
  22.  
  23.  
  24. frame.getContentPane().add(optionsList);
  25. frame.getContentPane().add(addBut);
  26.  
  27. frame.getContentPane().setLayout(new FlowLayout());
  28.  
  29. addBut.addActionListener(sentinela);
  30.  
  31. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.  
  33. }
  34.  
  35. public class Sentinela implements ActionListener {
  36. public void actionPerformed(ActionEvent event) {
  37. JButton b = (JButton) event.getSource();
  38. if (b == addBut) {
  39. JButton newB = new JButton("Delete");
  40. frame.getContentPane().add(newB);
  41. newB.addActionListener(sentinela);
  42. } else
  43. frame.getContentPane().remove(b);
  44. frame.repaint();
  45. frame.validate();
  46. }
  47. }
  48.  
  49. public void actionPerformed(ActionEvent event) {
  50. JButton b = (JButton) event.getSource();
  51. if (b == addBut) {
  52. addNewButton().addActionListener(null);
  53. System.out.println("new button");
  54. }
  55. }
  56.  
  57. private JButton addNewButton() {
  58. JButton newButton = new JButton("Delete");
  59. frame.getContentPane().add(newButton, FlowLayout.LEADING);
  60. return newButton;
  61. }
  62.  
  63. public void init() {
  64. frame.setVisible(true);
  65. }
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. public class Main {
  104.  
  105. /**
  106. * @param args
  107. */
  108. public static void main(String[] args) {
  109.  
  110. Layout layout = new Layout();
  111. System.out.print("sdfgh");
  112. layout.init();
  113. System.out.print("sdfgh");
  114.  
  115. }
  116.  
  117. }
Add Comment
Please, Sign In to add comment