2012-02-13 31 views
5

我目前Java Jframe和一個按鈕沒有更新的小問題。Java JFrame沒有更新按鈕的設置

我試圖禁用打印按鈕,直至打開完成和JFrame的關閉新JFrame的印刷...

,如果當一個新的窗口時,此按鈕將只禁用,但會直到那時,它可以採取一點點時間....

我設置的按鈕做這個禁用:PrintBttn.setEnabled(false);

我已經打過電話mainPanel.revalidate(); mainPanel.repaint(); PrintBttn.revalidate(); PrintBttn.repaint以及它們上面的混合物在其他論壇推薦...

我有點迷失在此刻,爲什麼它不會禁用按鈕,直到出現一個新窗口,因爲我做的第一件事就是如上所示禁用它,然後通過並創建新窗口。 ..

感謝, 埃裏克

+1

請學習java命名約定並堅持使用它們 – kleopatra 2012-02-13 09:11:47

回答

6

最有可能的,這是釋放EDT允許它重繪禁用的按鈕的問題。

一般情況下,它會是這個樣子:

PrintBttn.setEnabled(false); 
SwingUtilities.invokeLater(new Runnable() { 
    public void run() { 
     // Code to display the second JFrame goes here 
    } 
}; 
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(); 
      } 
     }); 
    } 
}