How to disable javax.swing.JButton in java?

前端 未结 3 834
失恋的感觉
失恋的感觉 2020-12-15 04:25

I have created a swings application and there is a \"Start\" button on the GUI. I want that whenever I clicked on that \"Start\" button, the start button should be disabled

3条回答
  •  萌比男神i
    2020-12-15 05:03

    This works.

    public class TestButton {
    
    public TestButton() {
        JFrame f = new JFrame();
        f.setSize(new Dimension(200,200));
        JPanel p = new JPanel();
        p.setLayout(new FlowLayout());
    
        final JButton stop = new JButton("Stop");
        final JButton start = new JButton("Start");
        p.add(start);
        p.add(stop);
        f.getContentPane().add(p);
        stop.setEnabled(false);
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(true);
                stop.setEnabled(false);
    
            }
        });
    
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(false);
                stop.setEnabled(true);
    
            }
        });
        f.setVisible(true);
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        new TestButton();
    
    }
    

    }

提交回复
热议问题