【发布时间】:2014-01-31 14:48:41
【问题描述】:
我想在我的主框架中添加一个计时器按钮,但它在另一个类中,我不知道如何在我的主类中使用它。 我的框架中需要一个计时器按钮,但如果没有其他课程,我将无法做到。 在那堂课中,我无法调用我的主框架。 这是我的代码:
class ButtonTimer extends Thread{
private JButton button = new JButton(" ");
private int count = 1;
public ButtonTimer() {
Timer time = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
button.setText(String.valueOf(count));
count++;
}
});
time.start();
JFrame frame1 = new JFrame();
frame1.add(button);
frame1.setBounds(0, 20, 100, 50);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);
}
}
public class game {
public static void main(String[] args) {
JFrame frame2 = new JFrame();
frame2.setBounds(0, 0, 1000, 5000);
frame2.setVisible(true);
JLayeredPane jlp = new JLayeredPane();
jlp.setBounds(0, 0, 1000, 500);
frame2.add(jlp);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ButtonTimer();
}
});
}
}
我该怎么做?
【问题讨论】:
-
你想让你的 timerButton 显示在 frame2 中吗??