繁体   English   中英

一旦在ActionListener中关闭后如何打开按钮

[英]How to turn on buttons once turned off in ActionListener

问题:代码无法重新打开按钮(示例给出了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开关方法)

问题来自班级的不良设计。 关键是您不会在同一button1上调用setEnabled(true)setEnabled(false) 在你的main

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

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

无论如何,您做错了:

  1. 不要将主(入口)线程与EDT混合使用。
  2. 不要持有与包含类相同的类类型的成员(除非有特殊原因这样做)。

这是工作代码的真实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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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