如何在ActionListener中关闭按钮

时间:2016-01-31 00:03:15

标签: java swing jbutton

问题:代码没有重新打开按钮(示例为您按下5秒钟)

示例代码:

public static void main(String[] args) throws InterruptedException
{
    Example call = new Example();
    Thread.sleep(5000);
    call.ButtonSwitch(1);
}

注意:这是我可以用来表示我的问题的最小编码

public class Example extends JFrame implements ActionListener {

static Example frame2 = new Example();

GridLayout experimentLayout = new GridLayout(0,1);

JPanel Game = new JPanel();

JButton button1 = new JButton("Press");

public Example()
{
    Create();
}

public void Set() 
{
    setResizable(false);
}

public static void Create() {
    /* Use an appropriate Look and Feel */
    try {
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    /* Turn off metal's use of bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);

    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
    public static void createAndShowGUI() 
    {
        //Create and set up the window.
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Set up the content pane.
        frame2.addComponentsToPane(frame2.getContentPane());
        //Display the window.
        frame2.pack();
        frame2.setVisible(true);
    }

    public void addComponentsToPane(final Container pane) 
    {
        Game.setLayout(experimentLayout);
        JPanel controls = new JPanel();
        controls.setLayout(new GridLayout(2,3));

        //Set up components preferred size
        JButton b = new JButton("Just fake button");
        Dimension buttonSize = b.getPreferredSize();
        Game.setPreferredSize(new Dimension((int)(buttonSize.getWidth() * 2),
                (int)(buttonSize.getHeight() * 1)* 4));

        Game.add(button1);
        button1.addActionListener(this);

        //Process the Apply gaps button press
        pane.add(Game, BorderLayout.NORTH);
        pane.add(new JSeparator(), BorderLayout.CENTER);
        pane.add(controls, BorderLayout.SOUTH);

    }
    //Turns button off On Click
    public void actionPerformed(ActionEvent e) 
    {
        if (e.getSource() == button1)
        {
            button1.setEnabled(false);
        }
    }

    //This does not turn the button on but tries to
    public void ButtonSwitch(int num)
    {
        if (num == 1)
        {
            System.out.println("This is called");
            button1.setEnabled(true);
        }
    }

}

我想让方法启用按钮,但是如果这是不可能的话,在没有用户输入的情况下在动作侦听器中执行此操作的方法将是第二个选项(看起来像放在ActionListener中的Button切换方法)< / p>

1 个答案:

答案 0 :(得分:0)

问题来自于班级的糟糕设计。关键是您没有在同一setEnabled(true)上致电setEnabled(false)button1。在main

Example call = new Example();
Thread.sleep(5000);
call.ButtonSwitch(1);

最后一行在setEnabled(true)按钮上调用call,而actionPerformed调用setEnabled(false)按钮上的frame2

无论如何,你做错了:

  1. 不要将主(入口)线程与EDT混合。
  2. 不要拥有与包含类相同类别的成员(除非有特殊原因这样做)。
  3. 以下是工作代码的真实MCVE:

    public class Example extends JFrame {
    
        JButton button = new JButton("Press");
        Timer timer = new Timer(5000, e -> button.setEnabled(true));
    
        public Example() {
    
            add(button);
            button.addActionListener(e -> { 
                button.setEnabled(false);
                timer.start();
            });
    
            timer.setRepeats(false);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) throws InterruptedException {
    
            SwingUtilities.invokeLater(() -> new Example());
        }
    }
    

    备注:

    • 方法和非最终变量名称以小写字母开头。
    • 请勿使用setPreferredSize,而是覆盖getPreferredSize