0

我有一个框架,当我单击 tester2 框架上的 ok 按钮时,应该看到 tester1 框架,当单击 showbumber 按钮时,我的标签中应该显示一个随机数。

但是当我使用睡眠方法时,我看不到这个生成的数字!

感谢您的帮助。

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) {
    }
}
}
4

2 回答 2

2

如果您打算在短暂延迟后自动关闭第二帧,则应使用 ajavax.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();
}
于 2013-02-19T10:10:11.630 回答
2

dialog如果您之前显示 showButton 和 Label,我不会。因为我必须添加一个面板才能显示它们。之后,您需要一个Timer类来处理 auto 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();
    }
}
于 2013-02-19T10:28:42.830 回答