Java 快速单击Jbutton不会导致任何操作

Java 快速单击Jbutton不会导致任何操作,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,嘿,伙计们,我写的代码有点问题 我有一个包含两个按钮的JFrame。每个按钮都有一个动作。我遇到的问题是一个名为“btnDone”的JButton按钮应该返回到上一个屏幕。如果我反复按下按钮,最终“btnDone”将停止执行它应该执行的逻辑。我的代码如下: 对于框架: public class ItemLocatorPnl extends JPnl { private static final long serialVersionUID = 1L; private Pnl pnl

嘿,伙计们,我写的代码有点问题

我有一个包含两个按钮的
JFrame
。每个按钮都有一个动作。我遇到的问题是一个名为
“btnDone”
JButton
按钮应该返回到上一个屏幕。如果我反复按下按钮,最终
“btnDone”
将停止执行它应该执行的逻辑。我的代码如下:

对于框架:

public class ItemLocatorPnl extends JPnl
{
    private static final long serialVersionUID = 1L;
    private Pnl pnl;
    private JButton btnDone;
    private JButton btnRefreshData;

    public void setPnl(Pnl pnl) {
        this.pnl = pnl;
    }

    public ItemLocatorPnl(Pnl pnl)
    {
        super();

        this.pnl=pnl;
        initialize();   
    }


    private void initialize()
    {
        this.setSize(300, 200);

        JPanel jContentPane = new JPanel();
        jContentPane.setLayout(new MigLayout());

        // (1) Remove window frame
        setUndecorated(true);


        // (3) Set background to white
        jContentPane.setBackground(Color.white);


        // (5) Add components to the JPnl's contentPane
        POSLoggers.initLog.writeDebug("ItemLocator: Adding icon");
        jContentPane.add(wmIconLabel, "align left");
        POSLoggers.initLog.writeDebug("ItemLocator: Adding global controls");
        jContentPane.add(createUpperPanel(), "align right, wrap");
        POSLoggers.initLog.writeDebug("ItemLocator: Adding main panel");
        jContentPane.add(pnl,"width 100%,height 100%, span 3");

        // (6) Attach the content pane to the JPnl
        this.setContentPane(jContentPane);

    }

    private JPanel createUpperPanel()
    {
        JPanel upperPanel=new JPanel();


        MigLayout mig = new MigLayout("align right", "", "");
        upperPanel.setLayout(mig);
        upperPanel.setBackground(Color.WHITE);


        // Create the Done button
        btnDone= GraphicalUtilities.getPOSButton("<html><center>Done</center></html>");
        btnDone.addActionListener(new ButtonListener());

        // Create the Refresh Data button
        btnRefreshData = GraphicalUtilities.getPOSButton("<html><center>Refresh<br>Data</center></html>");
        btnRefreshData.addActionListener(new ButtonListener()); 

        //Addiing buttons to the Panel
        upperPanel.add(btnRefreshData, "width 100:170:200, height 100!");
        upperPanel.add(btnDone, "width 100:170:200, height 100!");

        return upperPanel;

    }

    public class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            try {
                if (e.getSource() == btnRefreshData) {

                    Actual.refreshData();
                } else if (e.getSource() == btnDone) {
                    Actual.backToMainScreen();

                }
            }
            catch (Exception ex)
            {

            }
        }

    }

}
这是显示
JFrame
的代码:

public static void displayItemLocatorFrame()
{
    pnl = new Pnl();
    frame = new Frame(pnl);

    frame.setVisible(true);
    pnl.getSearchCriteria().requestFocus();

}
请注意,“frame”对象是静态的,我所有的方法都是静态的,它们存在于一个名为
Actual
的静态类中

简而言之,我只想确保无论用户点击按钮多少次,无论点击速度有多快,框架都应该正常工作。
有什么建议吗?(我尝试同步我的方法,但运气不佳。)

我通常更喜欢使用一个用于您尝试执行的操作

因此,您的代码可能如下所示:

btnDone = new JButton(new CloseFrameAction());

...

private class CloseFrameAction extends AbstractAction
{
    public CloseFrameAction()
    {
         super("Done");
    }

    public void actionPerformed(ActionEvent e) 
    {
        frame.dispose();
        setEnabled(false);
    }
}

注意setEnabled(false)行-这将禁用按钮并防止用户再次单击它。显然,我不知道您的具体要求是什么,但这是我将采取的一般方法。

问题在于使用静态面板,该面板通过每次单击按钮进行实例化。删除“静态”终于解决了我的问题!谢谢大家的帮助。

1)要更快地获得更好的帮助,请发布一个。2) 可能已经过期了,你应该接受一些答案。有关详细信息,请参阅。请注意,“frame”对象是静态的,我所有的方法都是静态的,它们存在于一个名为
Actual
”的静态类中,这个类的味道明显不好。“我尝试了同步我的方法,但没有成功。”不要指望在IDE中键入随机的代码行会修复bug。你的评论将在我以后的帖子中考虑,谢谢。添加同步方法并不是我随机做的事情。我有理由相信,当一个对象的两个实例调用同一对象的同一方法时,我遇到了竞争条件问题。
btnDone = new JButton(new CloseFrameAction());

...

private class CloseFrameAction extends AbstractAction
{
    public CloseFrameAction()
    {
         super("Done");
    }

    public void actionPerformed(ActionEvent e) 
    {
        frame.dispose();
        setEnabled(false);
    }
}