Java - 更新在Swing中创建的GUI

Jay*_*Jay 3 java user-interface swing netbeans

我正在尝试创建一个只有2个元素的简单GUI表单 - 一个简单的标签和一个按钮.按钮上显示的文字是"开始".标签默认显示为0.

当我单击"开始"按钮时,将执行以下操作:

  1. 计数器应每1秒从0开始递增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

}
Run Code Online (Sandbox Code Playgroud)

Main.java的内容是:

public class Main 
{
    public static int count = 0;
    public static boolean started = false;
}
Run Code Online (Sandbox Code Playgroud)

我想实现以下逻辑:

private void btnStartStopMouseClicked(java.awt.event.MouseEvent evt) 
{
    if (Main.stared == true)
    {
        // logic to start counting
    }
    else
    {
        // logic to stop counting
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

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

请帮助.非常感谢工作代码.提前致谢.

松鸦

nIc*_*cOw 6

只需使用javax.swing.Timer,并制作一个ActionListener,即可为您完成此任务.给我一个工作代码示例10分钟:-)

这是一个示例程序以获得进一步的帮助:

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();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您希望计数器再次恢复为0,则在停止计时器时,只需添加即可

else if (flag)
{
    startStopButton.setText("START TIMER");
    timer.stop();
    flag = false;
    counter = 0;
    changingLabel.setText("" + counter);
}
Run Code Online (Sandbox Code Playgroud)

这部分的buttonActionactionPerformed(...)方法.