2013-02-19 45 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