Kearone 2014年01月01日
求大神帮小弟看看问题所在!
程序能够运行,但是打开附件的秒表点击按钮没有反应,单独一个秒表程序可以。请大神看看问题出在哪里,帮忙修改一下,感激不尽
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class NotePad extends JFrame {
JTextArea jta;

class newl implements ActionListener {
public void actionPerformed(ActionEvent e) {
jta.setText("");
}
}

class openl implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser jf = new JFileChooser();
jf.showOpenDialog(NotePad.this);

}

}

// 保存文件的监听
class savel implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser jf = new JFileChooser();
jf.showSaveDialog(NotePad.this);

}
}

// 退出记事本的监听
class exitl implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);// 退出
}
}

class timerl implements ActionListener {
public void actionPerformed(ActionEvent e) {
new TestTimer() ;// 秒表
}
}

class tipl implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "程序 信息");
}
}
// 构造函数
public NotePad() {
jta = new JTextArea("", 24, 40);
JScrollPane jsp = new JScrollPane(jta);
JMenuBar jmb = new JMenuBar();
JMenu mFile = new JMenu("文件");
JMenu mStuff = new JMenu("附件");
JMenu mHelp= new JMenu("帮助");

JMenuItem mNew = new JMenuItem("新建");
mNew.addActionListener(new newl());
mFile.add(mNew);

JMenuItem mOpen = new JMenuItem("打开");
mOpen.addActionListener(new openl());
mFile.add(mOpen);

JMenuItem mSave = new JMenuItem("保存");
mSave.addActionListener(new savel());
mFile.add(mSave);

mFile.addSeparator(); // 添加分割线

JMenuItem mExit=new JMenuItem("退出");
mExit.addActionListener(new exitl());
mFile.add(mExit);
//
JMenuItem mTime=new JMenuItem("秒表");
mTime.addActionListener(new timerl());
mStuff.add(mTime);

JMenuItem mTi=new JMenuItem("关于");
mTi.addActionListener(new tipl());
mHelp.add(mTi);


jmb.add(mFile);
jmb.add(mStuff);
jmb.add(mHelp);

setJMenuBar(jmb);

setTitle("课程设计 ");
getContentPane().add(jsp);
setSize(634, 500);
setVisible(true);
}

// 主函数,程序入口点
public static void main(String s[]) {
new NotePad();
}

}

/*.........................................................*/
class TestTimer extends JFrame implements ActionListener, Runnable {
private static TestTimer obj;
private JButton btnStart;
private JButton btnPause;
private JButton btnResume;
private JButton btnStop;
private JLabel lblTime;
private static Thread th;
private long count;

public TestTimer() {
super("秒表");
btnStart = new JButton("开始");
btnPause = new JButton("暂停");
btnResume = new JButton("继续");
btnStop = new JButton("停止");
lblTime = new JLabel("00:00:00.000");
this.setLayout(new FlowLayout());
this.add(btnStart);
this.add(btnPause);
this.add(btnResume);
this.add(btnStop);
this.add(lblTime);
btnStart.addActionListener(this);
btnPause.addActionListener(this);
btnResume.addActionListener(this);
btnStop.addActionListener(this);
this.setSize(167, 144);
this.setResizable(false);
this.setVisible(true);
}

public static void main(String[] args) {
obj = new TestTimer();
}


public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if (btn.getText().equals("开始")) {
th = new Thread(obj);
count = 0;
th.start();
} else if (btn.getText().equals("暂停")) {
th.suspend();
} else if (btn.getText().equals("继续")) {
th.resume();
} else if (btn.getText().equals("停止")) {
th.stop();
}
}

public void run() {
while (true) {
int ms, seconds, minutes, hours;
String msg = "";
hours = (int) (count / 3600000);
minutes = (int) ((count - hours * 3600000) / 60000);
seconds = (int) ((count - hours * 3600000 - minutes * 60000) / 1000);
ms = (int) (count % 1000);
if (hours < 10) {
msg += "0" + hours + ":";
} else {
msg += hours + ":";
}
if (minutes < 10) {
msg += "0" + minutes + ":";
} else {
msg += minutes + ":";
}
if (seconds < 10) {
msg += "0" + seconds + ":";
} else {
msg += seconds + ":";
}
if (ms < 10) {
msg += "00" + ms;
} else if (ms < 100) {
msg += "0" + ms;
} else {
msg += ms;
}
lblTime.setText(msg);
count++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
...全文
429 点赞 收藏 2
写回复
2 条回复

还没有回复,快来抢沙发~

发动态
发帖子
J2ME
创建于2007-09-28

1.1w+

社区成员

2.6w+

社区内容

Java J2ME
社区公告
暂无公告