Java-更新在Swing中制作的GUI [英] Java - Updating a GUI made in Swing

查看:67
本文介绍了Java-更新在Swing中制作的GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个仅包含2个元素的简单GUI表单-一个简单的标签和一个按钮.按钮上显示的文本为开始".默认情况下,标签显示为0.

I am trying to create a simple GUI form which has only 2 elements - a simple label and a button. The text displayed on button is 'Start'. The label is displaying 0 by default.

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

When I click Start button following actions shall take place:

  1. 计数器应每1秒从0开始递增1.
  2. 开始"按钮上显示的文本应更改为停止".
  3. 再次单击同一按钮(现在将标题显示为停止")时,增量将停止.
  4. 按钮上的文本应更改为开始".等等...

我正在NetBeans中开发我的应用程序.

I am developing my application in Netbeans.

如上图所示,有2个.java文件

As shown in the above diagram, there are 2 .java files

AGC.java的内容是:

Contents of AGC.java are:

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的内容是:

Contents of Main.java are:

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?

请帮助.一个有效的代码将非常受赞赏.预先感谢.

Kindly help. A working code would be very highly appreciated. Thanks in advance.

周杰伦

推荐答案

只需使用

Simply use a javax.swing.Timer, and make one ActionListener, to do this thing for you . Give me ten mins for a working code example :-)

这是一个示例程序,可提供进一步的帮助:

Here is a sample program for further help :

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();
            }
        });
    }
}

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

If you want the counter to again revert back to 0, on Stopping the Timer, simply add

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

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

这篇关于Java-更新在Swing中制作的GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆