【问题标题】:Trying to move a code to main - Java试图将代码移动到 main - Java
【发布时间】:2013-09-28 11:41:31
【问题描述】:

我正在尝试将此代码放入 main()

public class TestPane extends JPanel {

    private JTextField field;
    private JButton button;
    private int tick; 
    private Timer timer;

    public TestPane() {

        field = new JTextField(10);
        field.setEditable(false);
        button = new JButton("Start");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                button.setEnabled(false);
                tick = 0;
                timer.start();
            }
        });

        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Success" + tick);
                field.setText(Integer.toString(++tick));
                if (tick > 4) {
                    timer.stop();
                    button.setEnabled(true);
                }
            }
        });
        timer.setInitialDelay(0);


        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        add(field, gbc);
        add(button, gbc);

    }
}

这是做什么的;它打开一个窗口 然后有一个开始按钮 如果你按下那个开始按钮,它会在一段时间后显示一些文本

我想要做的是将代码放在主函数中 应该发生的是 没有开始按钮,当你运行程序时, 它应该在某个时间间隔内在该区域中设置文本 (不按按钮自动)

我试过但失败了 这是代码

public static void main(String args[]) {
    //int tick; 
    // Timer timer;
    final Timer timer = new Timer(1000, new ActionListener() {
        int tick=0;

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Success" + ++tick);
            if (tick > 4) {
                ((Timer)e.getSource()).stop(); 
            }
        }
    });
    timer.setInitialDelay(0);
    System.out.format("About to schedule task.%n");
    new NewJFrame();
    System.out.format("Task scheduled.%n");

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

我不知道该怎么办?

【问题讨论】:

  • 像按钮一样使用timer.start();怎么样?
  • @JonathanDrapeau 你能告诉我怎么做吗??????
  • 尝试在new NewJFrame().setVisible(true)下方添加timer.start();
  • 作为一个有用的说明,全部大写和过多的标点符号!!!!不经常收到正面评价,至少在Stack Overflow 上没有。

标签: java swing timer move main


【解决方案1】:

正如dict19所说,你可以这样做,还添加了另一个可以启动它的地方,直接在main方法中,真的取决于你想要实现的目标:

public static void main(String args[]) {

  final Timer timer = new Timer(1000, new ActionListener() {
    int tick = 0;

    @Override
    public void actionPerformed(ActionEvent e) {
      System.out.println("Success" + ++tick);
      if (tick > 4) {
        ((Timer) e.getSource()).stop();
      }
    }
  });

  timer.setInitialDelay(0);
  System.out.format("About to schedule task.%n");
  // timer.start(); Or here
  System.out.format("Task scheduled.%n");

  java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
      new NewJFrame().setVisible(true); // previously new Test().setVisible(true);
      timer.start();
    }
  });
}

【讨论】:

  • +1 因为是问题的正确答案。额外:注意如果用户在定时器执行完成之前关闭帧,JVM将继续执行直到定时器停止。
  • 我在其中复制了您的代码的测试文件名为 Test,请将其更改为 new NewJFrame().setVisible(true); 抱歉。编辑为使用您的 JFrame 的名称。
猜你喜欢
  • 1970-01-01
  • 2011-08-30
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多