求大神帮小弟看看问题所在!

Kearone 2014-01-01 03:38:11
程序能够运行,但是打开附件的秒表点击按钮没有反应,单独一个秒表程序可以。请大神看看问题出在哪里,帮忙修改一下,感激不尽
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();
}
}
}
}
...全文
472 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
静山晚风 2014-03-20
  • 打赏
  • 举报
回复
把43行改为 TestTimer.main(null); 即可正常使用秒表 整个整个程序不跑 TestTimer是因为 Thread包装Runnable实现类时 要放在 NotePad这个类里,总之有点绕,想想Thread应该放的位置
  • 打赏
  • 举报
回复
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() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		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(this);
			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();
			}
		}
	}
}
看了下,代码写的比较难受,静态变量的问题,可以正常运行了。

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧