2012-02-13 35 views
5

Tôi hiện đang gặp sự cố nhỏ với Java Jframe và nút không cập nhật.Java JFrame không cập nhật cài đặt của một nút

Tôi cố gắng để vô hiệu hóa các nút Print cho đến việc in ấn của JFrame mới nó sẽ mở ra được thực hiện và rằng JFrame là đóng cửa ...

Nút sẽ chỉ vô hiệu hóa nếu và khi một cửa sổ mới xuất hiện, nhưng sẽ Mãi đến sau đó, có thể mất một chút thời gian ....

tôi đặt nút để vô hiệu hóa bằng cách làm này: PrintBttn.setEnabled(false);

tôi đã thử gọi mainPanel.revalidate(); mainPanel.repaint(); PrintBttn.revalidate(); PrintBttn.repaint cũng như một hỗn hợp của các bên trên như họ được đề xuất trong các diễn đàn khác ...

Tôi bị lạc vào lúc này và tại sao nó không vô hiệu hóa nút cho đến khi một cửa sổ mới xuất hiện vì điều đầu tiên tôi làm là Tắt nó như được hiển thị ở trên, và sau đó đi qua và tạo cửa sổ mới .. ..

Cảm ơn, Erik

+1

hãy tìm hiểu các quy ước đặt tên java và gắn bó với chúng – kleopatra

Trả lời

6

Nhiều khả năng, đó là một vấn đề giải phóng EDT để cho phép nó để sơn lại nút khuyết tật.

Nói chung, nó sẽ giống như thế này:

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

có thể là bạn đã thất bại trong việc đưa hình ảnh đầu tiên của bạn trong EDT quá, làm xem mã, là những gì bạn thực sự muốn:

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(); 
      } 
     }); 
    } 
} 
Các vấn đề liên quan