我在stopclock应用程序中添加了两个按钮;一个用于开始,另一个用于停止。开始是有效的,但停止不是。我用过一根线来做这个。
另外,代码正在创建两个框架,如何避免呢?
我们有四个类:一个是main类,两个是针对两个按钮实现actionListener的类,另一个是实现线程的类。
import java.awt.*;
import java.awt.event.*;
public class stopclock
{
Frame f;
Panel p;
Button b1,b2;
Label l;
stopclock(boolean k)
{
f=new Frame();
p=new Panel();
b1=new Button("Start");
b2=new Button("Stop");
l=new Label("00:00:00");
p.add(l);
p.add(b1);
p.add(b2);
f.add(p);
f.setTitle("Stop Clock");
f.setSize(300,300);
f.setVisible(true);
b1.addActionListener(new one());
b2.addActionListener(new two());
}
public static void main(String[] args)
{
stopclock s=new stopclock(false);
}
}
class one implements ActionListener
{
//Thread th=new Thread();
public void actionPerformed(ActionEvent ae)
{
new th().start();
}
}
class two implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
th run=new th();
Thread anb=new Thread(run);
anb.suspend();
}
}
class th extends Thread
{
int sec=0;
int hr=0;
int min=0;
public void run()
{
stopclock st=new stopclock(true);
//System.out.println("Hello");
//st.f.setVisible(true);
Label l2=st.l;
try
{
while(true)
{
if(sec>59)
{
min++;
sec=0;
}
if(min>59)
{
hr++;
min=0;
}
if(hr>12)
{
hr=0;
}
sec++;
String str=hr+":"+min+":"+sec;
l2.setText(str);
th.sleep(1000);
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}发布于 2015-12-19 13:52:15
我把你的组件改成了Swing组件。您正在获得2帧,因为在您的线程中,您正在使用stopclock st = new stopclock(true)创建一个新的框架。此外,在这方面使用Thread会有很大的困难。我使用了Swing Timer,而不是:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class StopClock {
JLabel label = new JLabel("00:00:00");;
Timer timer = new Timer(1000, new TimerListener());
StopClock() {
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
startButton.addActionListener(new StartActionListener());
stopButton.addActionListener(new StopActionListener());
JPanel panel = new JPanel();
panel.add(label);
panel.add(startButton);
panel.add(stopButton);
JFrame frame = new JFrame();
frame.add(panel);
frame.setTitle("Stop Clock");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new StopClock());
}
class StartActionListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
timer.start();
}
}
class StopActionListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
timer.stop();
}
}
class TimerListener implements ActionListener {
int sec = 0;
int hr = 0;
int min = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (sec > 59) {
min++;
sec = 0;
}
if (min > 59) {
hr++;
min = 0;
}
if (hr > 12) {
hr = 0;
}
sec++;
String str = hr + ":" + min + ":" + sec;
label.setText(str);
}
}
}有些东西是可以改进的,但我试着让它接近你写的东西。
备注:
boolean参数。pack()而不是setSize(...)。setVisible(...)作为你做的最后一件事。https://stackoverflow.com/questions/34370903
复制相似问题