简体   繁体   中英

Add JButton by pressing another JButton in Java

I am new to Java Development. I want to ask that is there any way that I can add a new JButton while user press one JButton?

Certainly. I suggest reading up on Action Listeners . You could set one button's visibility to false and then make it visible like this:

button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        otherButton.setVisible(true);
    }
});

Hope this helps!

import javax.swing.JButton;

public class NewJFrame1 extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame1
     */
    public NewJFrame1() {
        initComponents();
    }

    @SuppressWarnings("unchecked")

    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.FlowLayout());

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);

        pack();
    }                     
int button = 0;
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        JButton jButton = new javax.swing.JButton();
        jButton.setText("button " + (++button));
        jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton);

        this.revalidate();
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewJFrame1 frame = new NewJFrame1();
                frame.setSize(800, 600);
                frame.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   
}

Start by having a read through...

The trick here is dealing with the LayoutManager . Some layouts, like BorderLayout can give you issues with dynamically adding new components. Make sure you're using a LayoutManager which will update when new components are added to them (like FlowLayout , GridLayout or GridBagLayout )

After you've added a component to a container, you also need to call revalidate and repaint on the container you've updated, so that a layout and paint pass are scheduled and the UI can be updated based on your changes. Remember, Swing is lazy, it will wait until you tell it that it needs to be updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM