快速 Jbutton 单击导致无任何操作

发布于 2024-11-09 12:15:17 字数 3375 浏览 3 评论 0原文

嘿伙计们,我一直在编写的代码有问题。

我有一个包含两个按钮的 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)
            {

            }
        }

    }

}

这是单击时 btnDone 按钮调用的方法:

public static void backToMainScreen()
{

    frame.setVisible(false);
    frame.dispose();
}

这是显示 JFrame 的代码:

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

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

}

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

简而言之,我只是想确保无论用户单击按钮多少次,无论单击的速度有多快,框架都应该正常运行。 有什么建议吗? (我尝试同步我的方法,但没有成功..)

Hey guys, I have a problem with a code that I've been writing.

I have a JFrame that contains two buttons. Each of these buttons has an action. The problem I'm having is with a JButton called "btnDone" that's supposed to get back to a previous screen. If I I keep pushing the button repeatedly, eventually the "btnDone" would stop doing the logic it's supposed to do. My code is as follows:

For the frame:

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)
            {

            }
        }

    }

}

This is the method that the btnDone button calls upon clicking:

public static void backToMainScreen()
{

    frame.setVisible(false);
    frame.dispose();
}

This is the code that displays the JFrame:

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

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

}

Please note that the "frame" object is static, and all of my methods are static, and they exist in a static class called Actual.

So in short, I just want to make sure that no matter how many times a user clicks on the button, and no matter how fast the clicks were, the frame should act normally.
Any suggestions? (I tried synchronizing my methods with no luck..)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的年少不轻狂 2024-11-16 12:15:17

我通常更喜欢使用 Action你想做什么。

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

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) 行 - 这应该禁用按钮并防止用户再次单击它。显然我不知道您的确切要求是什么,但这是我会采取的一般方法。

I would generally prefer to use an Action for what you're trying to do.

So your code might look like this:

btnDone = new JButton(new CloseFrameAction());

...

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

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

Notice the setEnabled(false) line - this should disable the button and prevent the user clicking on it again. Obviously I don't know what your exact requirements are but this is the general approach I would take.

蓝天白云 2024-11-16 12:15:17

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

The problem was with using a static panel that was instantiated with the click of the button each time. Removing "static" has finally fixed my problem! Thanks everyone for the help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文