Java-更新Swing中的GUI
我试图创建一个简单的GUI表单,它只有两个元素——一个简单的标签和一个按钮。按钮上显示的文本为“开始”。默认情况下,标签显示为0 当我单击开始按钮时,将执行以下操作:Java-更新Swing中的GUI,java,swing,user-interface,netbeans,Java,Swing,User Interface,Netbeans,我试图创建一个简单的GUI表单,它只有两个元素——一个简单的标签和一个按钮。按钮上显示的文本为“开始”。默认情况下,标签显示为0 当我单击开始按钮时,将执行以下操作: 计数器应从0开始每隔1秒递增1 启动按钮上显示的文本应更改为停止 当我再次点击同一按钮(现在显示标题为“停止”)时,增量将停止 按钮上的文字应更改为开始。等等 我正在用Netbeans开发我的应用程序 如上图所示,有2个.java文件 AGC.java的内容包括: public class AGC extends javax.sw
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
}
public class Main
{
public static int count = 0;
public static boolean started = false;
}
Main.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
}
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
}
}
我的问题是:
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,停止计时器后,只需添加
else if (flag)
{
startStopButton.setText("START TIMER");
timer.stop();
flag = false;
counter = 0;
changingLabel.setText("" + counter);
}
这部分是对
按钮操作的操作执行(…)
方法的说明。好的,所以我建议大家看一下。您可以扩展SwingWorker,并在doBackground()中使用线程执行while(!isCancelled())
循环。sleep(1000)代码>执行睡眠后,您可以简单地触发一个属性更改,以增加标签的值
无论何时按下停止按钮,只要取消当前的swing worker即可。当您按下开始按钮时,如果我缺少任何相关信息,只需执行(
execute()
swing worker),然后请回复。我将能够提供同样的。我已经添加了工作样本代码,这是你想要的吗?非常感谢Gagandeep。它让我的一天。。。太好了。现在我可以在此基础上继续我的模拟设计。再次感谢。@Jay:非常欢迎你,保持微笑:-)