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秒从0开始递增1.
- 开始"按钮上显示的文本应更改为停止".
- 再次单击同一按钮(现在将标题显示为停止")时,增量将停止.
- 按钮上的文本应更改为开始".等等...
我正在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秒更新一次lblCounter?
- 我应采用哪种逻辑来启动1秒计时器,以及如何在该方法中访问lblCounter?
请帮助.一个有效的代码将非常受赞赏.预先感谢.
Kindly help. A working code would be very highly appreciated. Thanks in advance.
周杰伦
只需使用 ActionListener 为您执行此操作.给我十分钟的工作代码示例:-)
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);
}
这是buttonAction
的actionPerformed(...)
方法的一部分.