Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- public class Layout {
- private JFrame frame;
- private JButton addBut;
- private Sentinela sentinela;
- private String[] optionsArray= {"Static class listener", "Inner class listener", "Local class listener", "Anonymous class listener"};
- private JComboBox<String> optionsList = new JComboBox<String>(optionsArray);
- public Layout() {
- frame = new JFrame("Add Buttons");
- frame.setSize(400, 500);
- addBut = new JButton("Add new Button");
- sentinela = new Sentinela();
- frame.getContentPane().add(optionsList);
- frame.getContentPane().add(addBut);
- frame.getContentPane().setLayout(new FlowLayout());
- addBut.addActionListener(sentinela);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public class Sentinela implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- JButton b = (JButton) event.getSource();
- if (b == addBut) {
- JButton newB = new JButton("Delete");
- frame.getContentPane().add(newB);
- newB.addActionListener(sentinela);
- } else
- frame.getContentPane().remove(b);
- frame.repaint();
- frame.validate();
- }
- }
- public void actionPerformed(ActionEvent event) {
- JButton b = (JButton) event.getSource();
- if (b == addBut) {
- addNewButton().addActionListener(null);
- System.out.println("new button");
- }
- }
- private JButton addNewButton() {
- JButton newButton = new JButton("Delete");
- frame.getContentPane().add(newButton, FlowLayout.LEADING);
- return newButton;
- }
- public void init() {
- frame.setVisible(true);
- }
- }
- public class Main {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Layout layout = new Layout();
- System.out.print("sdfgh");
- layout.init();
- System.out.print("sdfgh");
- }
- }
Add Comment
Please, Sign In to add comment