Java - 更新使用Swing制作的GUI界面

3
我正在尝试创建一个简单的GUI表单,只有两个元素 - 简单的标签和按钮。按钮上显示的文本为“开始”。默认情况下,标签显示0。
当我点击“开始”按钮时,将执行以下操作:
  1. 计数器将从0开始每1秒递增1。
  2. 显示在“开始”按钮上的文本将更改为“停止”。
  3. 再次点击同一按钮(现在显示为“停止”),递增将停止。
  4. 按钮上的文本将更改为“开始”,依此类推...
我正在Netbeans中开发我的应用程序。
如上图所示,有2个.java文件
AGC.java的内容是:
public class AGC extends javax.swing.JFrame 
{
    public AGC()
    {    
        initComponents();
    }

    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() 
            {
                new AGC().setVisible(true);
            }
        });
    }

    private javax.swing.JButton btnStartStop;  // name of start stop button
    private javax.swing.JLabel lblCounter;   // name of the label

}

Main.java的内容如下:

public class Main 
{
    public static int count = 0;
    public static boolean started = false;
}

我希望实现以下逻辑:
private void btnStartStopMouseClicked(java.awt.event.MouseEvent evt) 
{
    if (Main.stared == true)
    {
        // logic to start counting
    }
    else
    {
        // logic to stop counting
    }
}

我的问题是:

  1. 如何在每1秒钟更新lblCounter?
  2. 我应该实现什么逻辑来启动1秒的计时器,以及如何在该方法中访问lblCounter?

请帮忙解决。非常感谢提供工作代码。谢谢。

Jay


如果我漏掉了任何相关信息,请回复。我可以提供相同的信息。 - Jay
我已经添加了可工作的示例代码,这是你想要的吗? - nIcE cOw
2个回答

6

使用javax.swing.Timer,并创建一个ActionListener,可以为您完成这件事。请给我十分钟时间提供一个运行代码示例 :-)

以下是一个样例程序以供参考:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class UpdateWithTimer extends JFrame
{
    private Timer timer;
    private JButton startStopButton;
    private JLabel changingLabel;
    private int counter = 0;
    private boolean flag = false;
    private ActionListener timerAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            counter++;
            changingLabel.setText("" + counter);
        }
    };

    private ActionListener buttonAction = new ActionListener()  
    {
        public void actionPerformed(ActionEvent ae)
        {
            if (!flag)
            {
                startStopButton.setText("STOP TIMER");
                timer.start();
                flag = true;
            }
            else if (flag)
            {
                startStopButton.setText("START TIMER");
                timer.stop();
                flag = false;
            }
        }
    };

    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        JPanel contentPane = new JPanel();
        changingLabel = new JLabel("" + counter);
        contentPane.add(changingLabel);

        startStopButton = new JButton("START TIMER");
        startStopButton.addActionListener(buttonAction);

        add(contentPane, BorderLayout.CENTER);
        add(startStopButton, BorderLayout.PAGE_END);

        timer = new Timer(1000, timerAction);

        setSize(300, 300);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new UpdateWithTimer().createAndDisplayGUI();
            }
        });
    }
}

如果您想让计数器再次归零,在停止计时器时,只需添加以下内容:
else if (flag)
{
    startStopButton.setText("START TIMER");
    timer.stop();
    flag = false;
    counter = 0;
    changingLabel.setText("" + counter);
}

这部分内容涉及到buttonActionactionPerformed(...)方法。


非常感谢你,Gagandeep。这让我很开心……太好了。现在我可以在此基础上构建并开始我的仿真设计了。再次感谢。 - Jay
@Jay:非常欢迎你,保持微笑 :-) - nIcE cOw

2
好的,我建议您查看SwingWorker。您可以扩展SwingWorker,在doBackground()中执行while(!isCancelled())循环,并使用Thread.sleep(1000);进行睡眠。在睡眠执行之后,您可以简单地触发一个属性更改,以增加标签的值。
每当您按下停止按钮时,只需取消当前的Swing Worker。当您按下开始按钮时,只需执行Swing Worker。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接