【问题标题】:Adding a timer button in a frame to another frame将一个框架中的计时器按钮添加到另一个框架
【发布时间】: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();
        }
    });

}
}

我该怎么做?

【问题讨论】:

标签: java swing time jframe


【解决方案1】:

你可以...

创建一个自定义JButton,在其内部包装一个Timer

这使您可以将按钮和计时器自包含在一个单元中,并在您想要的任何地方重复使用...

你可以...

创建一个自定义 Timer,它引用 JButton 并在每个触发器上自动更新文本...

你可以...

创建一个自定义 ActionListener 甚至 Action,它引用 JButton 并更新文本,然后将其传递给您选择的 Timer 实例...

【讨论】:

  • 但是怎么做呢?可以举个例子吗?
  • 您接受任何建议,只需将其添加到您需要的地方...根据您实际尝试执行的操作,您可能需要对您尝试实现的框架的引用。 ..
  • 不,请详细说明您要达到的目标,以便我更好地确定最佳解决方案 - 这不是代码工厂...
【解决方案2】:

试一试。这里我们在你的主框架中创建一个 JButton,然后我们在另一个类的 actionPerformed 上设置文本。

public class game1 {
    private static JFrame frame2;
    private static JButton button1=new JButton(" ");
public static void main(String[] args) {
    frame2 = new JFrame();
    frame2.setBounds(0, 0, 1000, 5000);

    JLayeredPane jlp = new JLayeredPane();
    jlp.setBounds(0, 0, 1000, 500);
    jlp.add(button1);
    frame2.add(jlp);

    frame2.add(button1);
    frame2.setVisible(true);
     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
       new ButtonTimer();

     }
    });

}

    private static class ButtonTimer {
private JButton button = new JButton(" ");
private int count = 1;
public ButtonTimer() {

    javax.swing.Timer timer = new javax.swing.Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                     button.setText(String.valueOf(count));
                     button1.setText(String.valueOf(count));
            count++;
                }
            });
    timer.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);
}
    }
}

【讨论】:

    猜你喜欢
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2018-10-31
    • 2013-03-28
    相关资源
    最近更新 更多