Java JFrame 不更新按钮的设置

发布于 2025-01-05 02:15:59 字数 449 浏览 4 评论 0原文

我目前遇到一个 Java Jframe 和按钮未更新的小问题。

我正在尝试禁用“打印”按钮,直到它打开的新 JFrame 的打印完成并且 JFrame 关闭...

该按钮仅在出现新窗口时才会禁用,但在此之前不会,这可能需要一段时间一点时间......

我通过执行以下操作将按钮设置为禁用: PrintBttn.setEnabled(false);

我尝试调用 mainPanel.revalidate(); mainPanel.repaint(); PrintBttn.revalidate(); PrintBttn.repaint 以及他们在其他论坛中推荐的上述内容的混合...

我现在有点迷失了这一点,以及为什么它不会禁用按钮,直到出现一个新窗口,因为我做的第一件事是如上所示禁用它,然后浏览并创建新窗口......

谢谢, 埃里克

I am currently having a minor issue with a Java Jframe and a button not updating.

I am trying to disable the Print Button until the printing of the new JFrame it opens is done and that JFrame is closed...

The button will only disable if and when a new window occurs, but will not until then, which can take a little bit of time....

I set the button to disable by doing this: PrintBttn.setEnabled(false);

I have tried calling mainPanel.revalidate(); mainPanel.repaint(); PrintBttn.revalidate(); PrintBttn.repaint as well as a mixture of the above as they recommended in other forums...

I am kind of lost at the moment on this and to why it is not disabling the button until a new window appears since the first thing i do is Disable it as shown above, and then go through and create the new window....

Thanks,
Erik

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

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

发布评论

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

评论(2

撩动你心 2025-01-12 02:15:59

最有可能的是释放 EDT 以允许其重新绘制禁用按钮的问题。

一般来说,它看起来像这样:

PrintBttn.setEnabled(false);
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        // Code to display the second JFrame goes here
    }
};

Most likely, it's a question of releasing the EDT to allow it to repaint the disabled button.

Generally, it will look something like this:

PrintBttn.setEnabled(false);
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        // Code to display the second JFrame goes here
    }
};
请爱~陌生人 2025-01-12 02:15:59

可能您也未能将第一帧放入 EDT,请观看代码,这就是您真正想要的:

import java.awt.event.*;

import javax.swing.*;

public class TwoFrames
{
    private JFrame frame1, frame2;
    private JPanel panel1, panel2;
    private JButton button1, button2, button3;
    private ActionListener action;

    public TwoFrames()
    {               
        frame1 = new JFrame("Frame One");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame2 = new JFrame("Frame Two");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel1 = new JPanel();      

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (ae.getSource() == button1)
                {
                    // Here goes your code for displaying your Second Frame.
                    SwingUtilities.invokeLater(new Runnable()
                    {
                        public void run()
                        {
                            if (!frame2.isShowing())
                            {                                                           
                                panel2 = new JPanel();

                                button2 = new JButton("Click Me to HIDE FRAME.");
                                button2.setHorizontalTextPosition(AbstractButton.CENTER);
                                button2.setVerticalTextPosition(AbstractButton.CENTER);
                                button2.addActionListener(action);

                                panel2.add(button2);
                                panel2.setOpaque(true);
                                frame2.setContentPane(panel2);

                                frame2.setSize(200, 200);
                                frame2.setLocationRelativeTo(null);
                                frame2.setVisible(true);
                            }
                        }
                    });             
                    button3.setEnabled(false);
                }
                else if (ae.getSource() == button2)
                {
                    frame2.dispose();
                    button3.setEnabled(true);
                }
            }       
        };

        button1 = new JButton("Click Me to Display FRAME.");
        button1.setHorizontalTextPosition(AbstractButton.CENTER);
        button1.setVerticalTextPosition(AbstractButton.CENTER);
        button1.addActionListener(action);          

        button3 = new JButton("Watch Me getting DISABLED");
        button3.setHorizontalTextPosition(AbstractButton.CENTER);
        button3.setVerticalTextPosition(AbstractButton.CENTER);
        button3.addActionListener(action);

        panel1.add(button1);
        panel1.add(button3);
        panel1.setOpaque(true);
        frame1.setContentPane(panel1);      

        frame1.setSize(200, 200);       

        frame1.setVisible(true);
    }

    public static void main(String... args)
    {
        // Here we are Scheducling a JOB for Event Dispatcher Thread.
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()               
            {
                new TwoFrames();
            }
        });
    }
}

Might be you had failed to put your first frame in the EDT too, do watch the code, is this what you actually want :

import java.awt.event.*;

import javax.swing.*;

public class TwoFrames
{
    private JFrame frame1, frame2;
    private JPanel panel1, panel2;
    private JButton button1, button2, button3;
    private ActionListener action;

    public TwoFrames()
    {               
        frame1 = new JFrame("Frame One");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame2 = new JFrame("Frame Two");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel1 = new JPanel();      

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (ae.getSource() == button1)
                {
                    // Here goes your code for displaying your Second Frame.
                    SwingUtilities.invokeLater(new Runnable()
                    {
                        public void run()
                        {
                            if (!frame2.isShowing())
                            {                                                           
                                panel2 = new JPanel();

                                button2 = new JButton("Click Me to HIDE FRAME.");
                                button2.setHorizontalTextPosition(AbstractButton.CENTER);
                                button2.setVerticalTextPosition(AbstractButton.CENTER);
                                button2.addActionListener(action);

                                panel2.add(button2);
                                panel2.setOpaque(true);
                                frame2.setContentPane(panel2);

                                frame2.setSize(200, 200);
                                frame2.setLocationRelativeTo(null);
                                frame2.setVisible(true);
                            }
                        }
                    });             
                    button3.setEnabled(false);
                }
                else if (ae.getSource() == button2)
                {
                    frame2.dispose();
                    button3.setEnabled(true);
                }
            }       
        };

        button1 = new JButton("Click Me to Display FRAME.");
        button1.setHorizontalTextPosition(AbstractButton.CENTER);
        button1.setVerticalTextPosition(AbstractButton.CENTER);
        button1.addActionListener(action);          

        button3 = new JButton("Watch Me getting DISABLED");
        button3.setHorizontalTextPosition(AbstractButton.CENTER);
        button3.setVerticalTextPosition(AbstractButton.CENTER);
        button3.addActionListener(action);

        panel1.add(button1);
        panel1.add(button3);
        panel1.setOpaque(true);
        frame1.setContentPane(panel1);      

        frame1.setSize(200, 200);       

        frame1.setVisible(true);
    }

    public static void main(String... args)
    {
        // Here we are Scheducling a JOB for Event Dispatcher Thread.
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()               
            {
                new TwoFrames();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文