Java JFrame不更新按钮的设置

时间:2012-02-13 03:22:57

标签: java swing netbeans jframe jbutton

我目前在Java Jframe和没有更新的按钮方面遇到了一个小问题。

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

按钮只会在新窗口出现时停用,但在此之前不会停用,这可能需要一点时间......

我将按钮设置为禁用:PrintBttn.setEnabled(false);

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

我现在有点迷失了,并且为什么在新窗口出现之前它没有禁用按钮,因为我做的第一件事是如上所示禁用它,然后通过并创建新窗口。 ...

谢谢, 埃里克

2 个答案:

答案 0 :(得分:6)

很有可能,这是一个释放EDT以允许它重新绘制禁用按钮的问题。

通常,它看起来像这样:

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

答案 1 :(得分:3)

你可能也没有把你的第一帧放在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();
            }
        });
    }
}