2013-02-19 44 views
0

我有一個框架,當我在tester2框架上單擊確定按鈕時,應該看到tester1框架,並且當單擊showbumber按鈕時,應該在我的標籤中顯示一個隨機數。JButton和JLabel不顯示在JDialog上並且睡眠不起作用

但我看不到這個生成的數字,而我使用睡眠方法!

感謝您的幫助。

public class tester2 extends JFrame implements ActionListener { 

public tester2() { 
    setTitle("Hello"); 
    setLayout(new FlowLayout()); 
    JButton okButton = new JButton("Ok"); 
    okButton.addActionListener(this); 
    add(okButton); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setBounds(40, 50, 300, 400); 

} 

@Override 
public void actionPerformed(ActionEvent e) { 
    tester1 tester1 = new tester1(tester2.this); 
    tester1.setVisible(true); 
} 

public static void main(String[] args) { 
    new tester2().setVisible(true); 
} 
} 

測試儀1:

public class tester1 extends JDialog implements ActionListener { 

JLabel lbl1; 
JButton showButton; 

public tester1(JFrame owner) { 
    super(owner, "tester1", true); 
    showButton = new JButton("Show Number"); 
    showButton.addActionListener(this); 
    lbl1 = new JLabel("  "); 

    this.add(showButton); 
    this.add(lbl1); 
    this.setBounds(40, 50, 300, 400); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == showButton) { 
     GenerateNumber(); 
     tester1.this.dispose(); 
    } 
} 

public void GenerateNumber() { 
    Random rnd1 = new Random(); 
    try { 
     Thread.sleep(1000); 
     lbl1.setText(String.valueOf(rnd1.nextInt(100))); 
    } catch (InterruptedException inrptdEx) { 
    } 
} 
} 
+0

您要處理你的框架,在標籤設置文本之後。爲什麼?它不會讓你看到你設定的文字。同時遵循[命名約定](http://www.oracle.com/technetwork/java/codeconv-138413.html),同時在java中進行編碼。 – 2013-02-19 09:53:02

+0

在EDT內睡覺將阻止Swing執行任何重繪。而不是使用Thread.sleep,請使用javax.swing.Timer – MadProgrammer 2013-02-19 10:00:52

+0

看看我的帖子,看看我的答案 – 2013-02-19 10:30:18

回答

2

如果你的目的是要在短暫的延遲後自動關閉第二幀,你應該使用javax.swing.Timer代替。

阻斷EDT將(其中包括)處理重繪請求,這意味着你的UI時,你可以Thread.sleep

相反,你應該使用javax.swing.Timer

public void GenerateNumber() { 
    Random rnd1 = new Random(); 
    try { 
     lbl1.setText(String.valueOf(rnd1.nextInt(100))); 
    } catch (InterruptedException inrptdEx) { 
    } 
    Timer timer = new Timer(1000, new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
      dispose(); 
     } 
    }); 
    timer.setRepeats(false); 
    timer.start(); 
} 
+0

預先感謝您! – Sajad 2013-02-19 12:16:53

2

不能更新停止如果您的dialog之前顯示showButton和Label,我不會。因爲我必須添加一個面板才能顯示它們。之後,你需要一個Timer類來處理汽車dispose

你tester1現在這個樣子

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 

import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class tester1 extends JDialog implements ActionListener { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    JLabel lbl1; 

    JButton showButton; 

    public tester1(JFrame owner) { 
     super(owner, "tester1", true); 
     JPanel jPanel = new JPanel(); 
     jPanel.setLayout(new BorderLayout()); 
     this.add(jPanel); 

     showButton = new JButton("Show Number"); 
     showButton.addActionListener(this); 
     lbl1 = new JLabel(); 

     jPanel.add(showButton, BorderLayout.NORTH); 
     jPanel.add(lbl1, BorderLayout.CENTER); 
     this.setBounds(40, 50, 300, 400); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == showButton) { 
      GenerateNumber(); 
     } 
    } 

    public void GenerateNumber() { 
     Random rnd1 = new Random(); 
     lbl1.setText(String.valueOf(rnd1.nextInt(1000000))); 
     Timer timer = new Timer(1000 * 1, new ActionListener() { 

      public void actionPerformed(ActionEvent evt) { 
       dispose(); 
      } 
     }); 
     timer.setRepeats(false); 
     timer.start(); 
    } 
} 
+0

謝謝你的幫助。 – Sajad 2013-02-19 12:17:29

+0

@ Sajjad-HiFriend不用客氣 – 2013-02-19 12:24:14