有两个按键
按按键一在textarea中显示thread1(0)=系统时间,每过一秒打印一次,例如
thread1(0)=13:16:11
thread1(1)=13:16:12
thread1(2)=13:16:13
按键2同按键1差不多显示thread2(0)=系统时间
两个是可以同时出现的
thread2(0)=13:16:11
thread2(1)=13:16:12
thread2(2)=13:16:13 这是我现在做成的代码但是以上功能我还不知道要如何插入 请指教 谢谢
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;/**
*
* @author ouso
*/
public class Main extends JFrame implements ActionListener { /**
* @param args the command line arguments
*/
/** 按钮(start1 ) */
public JButton btnStart1;
/** 按钮(start2 ) */
public JButton btnStart2;
/** 按钮(start3 ) */
public JButton btnStart3;
/** 按钮(stop ) */
public JButton btnStop;
/** 时间表示 */
public TextArea txtText;
/** 面板*/
public JPanel panThread;
/** text */
public JPanel panText;
/** */
public JScrollPane jspPane;
/** 表示 */
private Date time;
/** 表示時間*/
private Thread timeThread; public Main() {
/* fream設定 */
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("タイマー");
setSize(400, 350); //画面定義
btnStart1 = new JButton("start1");
btnStart2 = new JButton("start2");
btnStart3 = new JButton("start3");
btnStop = new JButton("stop");
panText = new JPanel();
txtText = new TextArea(10, 10); /* 位置設定 */
txtText.setBounds(10, 10, 350, 200);
btnStart1.setBounds(10, 250, 80, 30);
btnStart2.setBounds(100, 250, 80, 30);
btnStart3.setBounds(190, 250, 80, 30);
btnStop.setBounds(280, 250, 80, 30);
/* 面板設定 */
panText.setLayout(null);
panText.add(txtText);
panText.add(btnStart1);
btnStart1.addActionListener(this);
panText.add(btnStart2);
btnStart2.addActionListener(this);
panText.add(btnStart3);
btnStart3.addActionListener(this);
panText.add(btnStop); /* fream追加 */
getContentPane().add(panText);
} public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(btnStart1)) {
txtText.setText("");
/* 新线程*/
timeThread = new Thread(new Time());
timeThread.start();
}
if (evt.getSource().equals(btnStart2)) {
txtText.setText("");
/* 新线程*/
timeThread = new Thread(new Time());
timeThread.start();
}
if (evt.getSource().equals(btnStart3)) {
txtText.setText("");
/* 新线程 */
timeThread = new Thread(new Time());
timeThread.start();
}
} public class Time extends Thread {
/** 時間*/
public Time() {
} @Override
public void run() {
try {
while (true) {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(" hh:mm:ss ");
txtText.append(sdf.format(d)+"\n");
Thread.sleep(1000);
}
} catch (InterruptedException e) {
}
}
} public static void main(String[] args) {
Main main = new Main();
main.setVisible(true);
}
}
按按键一在textarea中显示thread1(0)=系统时间,每过一秒打印一次,例如
thread1(0)=13:16:11
thread1(1)=13:16:12
thread1(2)=13:16:13
按键2同按键1差不多显示thread2(0)=系统时间
两个是可以同时出现的
thread2(0)=13:16:11
thread2(1)=13:16:12
thread2(2)=13:16:13 这是我现在做成的代码但是以上功能我还不知道要如何插入 请指教 谢谢
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;/**
*
* @author ouso
*/
public class Main extends JFrame implements ActionListener { /**
* @param args the command line arguments
*/
/** 按钮(start1 ) */
public JButton btnStart1;
/** 按钮(start2 ) */
public JButton btnStart2;
/** 按钮(start3 ) */
public JButton btnStart3;
/** 按钮(stop ) */
public JButton btnStop;
/** 时间表示 */
public TextArea txtText;
/** 面板*/
public JPanel panThread;
/** text */
public JPanel panText;
/** */
public JScrollPane jspPane;
/** 表示 */
private Date time;
/** 表示時間*/
private Thread timeThread; public Main() {
/* fream設定 */
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("タイマー");
setSize(400, 350); //画面定義
btnStart1 = new JButton("start1");
btnStart2 = new JButton("start2");
btnStart3 = new JButton("start3");
btnStop = new JButton("stop");
panText = new JPanel();
txtText = new TextArea(10, 10); /* 位置設定 */
txtText.setBounds(10, 10, 350, 200);
btnStart1.setBounds(10, 250, 80, 30);
btnStart2.setBounds(100, 250, 80, 30);
btnStart3.setBounds(190, 250, 80, 30);
btnStop.setBounds(280, 250, 80, 30);
/* 面板設定 */
panText.setLayout(null);
panText.add(txtText);
panText.add(btnStart1);
btnStart1.addActionListener(this);
panText.add(btnStart2);
btnStart2.addActionListener(this);
panText.add(btnStart3);
btnStart3.addActionListener(this);
panText.add(btnStop); /* fream追加 */
getContentPane().add(panText);
} public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(btnStart1)) {
txtText.setText("");
/* 新线程*/
timeThread = new Thread(new Time());
timeThread.start();
}
if (evt.getSource().equals(btnStart2)) {
txtText.setText("");
/* 新线程*/
timeThread = new Thread(new Time());
timeThread.start();
}
if (evt.getSource().equals(btnStart3)) {
txtText.setText("");
/* 新线程 */
timeThread = new Thread(new Time());
timeThread.start();
}
} public class Time extends Thread {
/** 時間*/
public Time() {
} @Override
public void run() {
try {
while (true) {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(" hh:mm:ss ");
txtText.append(sdf.format(d)+"\n");
Thread.sleep(1000);
}
} catch (InterruptedException e) {
}
}
} public static void main(String[] args) {
Main main = new Main();
main.setVisible(true);
}
}
解决方案 »
- 鼠标适配器问题,求指教,错在哪里怎么改?
- 如何提取各个像素的图象数据值[50分],回帖有帮助立即给分
- 书上有几个文件管理的小问题没有提到,我不太明白,哪位大哥给看看,最好写上解释,谢谢啦
- 求教Class.forname,Class.newInstance方法的详解~~
- 关于MaskFormatter的错误
- 一个小问题,简单!!
- Java生产者消费者例子,大家看还有哪里可以改进? Java多线程 wait和notify运用 synchronized运用
- jbuilder8保存文件时总是产生许多:jsp.jsp~1~ jsp.jsp~2~ jsp.jsp~3~,......
- IE中如何直接浏览WORD和PDF文件?
- 郁闷无罪,散分有理!
- 关于语音的问题
- 两个对象的值相等的问题
1、将代码进行良好的格式化,以方便阅读。
2、在发帖文本框的上方单击“#”按钮,选择 Java
3、将代码粘贴到【code=Java】和【/code】之间。发出来的帖子就会是下面的效果:public class Hello { // 程序入口
public static void main(String[] args) {
System.out.println("Hello!");
}
}
public class ThreadedServerSocketTest { private int port = 8189;
public void testThreadedServerSocket() {
try{
int i = 1;
ServerSocket s = new ServerSocket(port);
while(true) {
Socket inComing = s.accept();
System.out.println("Spawning " + i);
Runnable r = new ThreadedEchoHandler(inComing, port);
Thread t = new Thread(r);
t.start();
i++;
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ThreadedServerSocketTest tt = new ThreadedServerSocketTest();
tt.testThreadedServerSocket();
}
}
class ThreadedEchoHandler implements Runnable { private Socket inComing;
private int port;
public ThreadedEchoHandler(Socket i, int port) {
this.inComing = i;
this.port = port;
}
public void run() {
try{
try{
//服务器输入流
InputStream inStream = inComing.getInputStream();
//服务器输出流
OutputStream outStream = inComing.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream, true/*autoFlush*/);
out.println("Hello! Enter BYE to exist.");
boolean done = false;
while(!done && in.hasNextLine()) {
String line = in.nextLine();
out.println("Echo: " + line);
if(line.trim().equals("exist"))
done = true;
}
}finally {
inComing.close();
}
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
看一下这部分代码,对你有帮助
package com.test.thread;import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTextArea;public class TimeThread extends Thread {
private JTextArea ta = null;
private int i = 0;
private String name = null;
public TimeThread(JTextArea ta,String name) {//JTextArea 的引用和线程名称
// TODO Auto-generated constructor stub
this.ta = ta;
this.name = name;//用于设置当前线程的名称,懒得用setName()和getName()设置
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setTime();
}
}
private void setTime(){
DateFormat format = new SimpleDateFormat("hh:mm:ss");
String str = this.ta.getText()+name
+"("+i+"):"+format.format(new Date())+"\n";
i++;
this.ta.setText(str);
}
}Main类:
package com.test.thread;import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTextArea showArea = null;
private JPanel jPanel = null;
private JButton btnThread1 = null;
private JButton btnThread2 = null;
private JButton btnStop = null;
private TimeThread tt1 = null;
private TimeThread tt2 = null; /**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getShowArea());
}
return jScrollPane;
} /**
* This method initializes showArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getShowArea() {
if (showArea == null) {
showArea = new JTextArea();
showArea.setLineWrap(true);
}
return showArea;
} /**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.insets = new Insets(3, 3, 3, 3);
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.insets = new Insets(3, 3, 3, 3);
gridBagConstraints1.gridy = 2;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.insets = new Insets(3, 3, 3, 3);
gridBagConstraints.gridy = 1;
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.add(getBtnThread1(), gridBagConstraints2);
jPanel.add(getBtnThread2(), gridBagConstraints);
jPanel.add(getBtnStop(), gridBagConstraints1);
}
return jPanel;
} /**
* This method initializes btnThread1
*
* @return javax.swing.JButton
*/
private JButton getBtnThread1() {
if (btnThread1 == null) {
btnThread1 = new JButton();
btnThread1.setText("线程1");
btnThread1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(tt1 == null){
tt1 = new TimeThread(showArea,"thread1");
tt1.start();
}
}
});
}
return btnThread1;
} /**
* This method initializes btnThread2
*
* @return javax.swing.JButton
*/
private JButton getBtnThread2() {
if (btnThread2 == null) {
btnThread2 = new JButton();
btnThread2.setText("线程2");
btnThread2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(tt2 == null){
tt2 = new TimeThread(showArea,"thread2");
tt2.start();
}
}
});
}
return btnThread2;
} /**
* This method initializes btnStop
*
* @return javax.swing.JButton
*/
private JButton getBtnStop() {
if (btnStop == null) {
btnStop = new JButton();
btnStop.setText("停止");
btnStop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(tt1 != null)
tt1.stop();
if(tt2 != null)
tt2.stop();
}
});
}
return btnStop;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main thisClass = new Main();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
} /**
* This is the default constructor
*/
public Main() {
super();
initialize();
} /**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(465, 330);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
} /**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
jContentPane.add(getJPanel(), BorderLayout.EAST);
}
return jContentPane;
}} // @jve:decl-index=0:visual-constraint="10,10"
如果有什么疑问请提出。
最好使用Eclipse3.4,代码里面的个别注释3.2不支持
1. 在“线程1”按钮的执行代码中修改后如下 if(tt1 == null || !tt1.isAlive()){
tt1 = new TimeThread(showArea,"thread1");
tt1.start();
2. “线程2”按钮的修改同:“线程1”按钮3. “停止”按钮中修改如下: if(tt1 != null || tt1.isAlive())
tt1.stop();
if(tt2 != null || tt2.isAlive())
tt2.stop();
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;/**
*
* @author ouso
*/
public class Main extends JFrame implements ActionListener { /**
* @param args the command line arguments
*/
/** ボタン(start1 ) */
public JButton btnStart1;
/** ボタン(start2 ) */
public JButton btnStart2;
/** ボタン(start3 ) */
public JButton btnStart3;
/** ボタン(stop ) */
public JButton btnStop;
/** 表示するところ */
public TextArea txtText;
/** パネル*/
public JPanel panThread;
/** パネルtext */
public JPanel panText;
/** スクロールバー */
public JScrollPane jspPane;
/** 表示タイマー1 */
private Date time;
/** 表示時間*/
private Thread timeThread;
public int i ;
public int p ;
public boolean isThread ; public Main() {
/* フレームの設定 */
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("タイマー");
setSize(400, 350); //画面定義
btnStart1 = new JButton("start1");
btnStart2 = new JButton("start2");
btnStart3 = new JButton("start3");
btnStop = new JButton("stop");
panText = new JPanel();
txtText = new TextArea(10, 10); /* 位置設定 */
txtText.setBounds(10, 10, 350, 200);
btnStart1.setBounds(10, 250, 80, 30);
btnStart2.setBounds(100, 250, 80, 30);
btnStart3.setBounds(190, 250, 80, 30);
btnStop.setBounds(280, 250, 80, 30);
/* パネル設定 */
panText.setLayout(null);
panText.add(txtText);
panText.add(btnStart1);
btnStart1.addActionListener(this);
panText.add(btnStart2);
btnStart2.addActionListener(this);
panText.add(btnStart3);
btnStart3.addActionListener(this);
panText.add(btnStop); /* フレームに追加 */
getContentPane().add(panText);
} public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(btnStart1)) {
i = 1;
p =3000 ;
isThread = true ;
/* 新しいスレッド */
timeThread = new Thread(new Time(i,p));
timeThread.start();
}
if (evt.getSource().equals(btnStart2)) {
i=2;
p = 5000 ;
isThread = true ;
/* 新しいスレッド */
timeThread = new Thread(new Time(i,p));
timeThread.start();
}
if (evt.getSource().equals(btnStart3)) {
i =3 ;
p = 7000 ;
isThread = true ;
/* 新しいスレッド */
timeThread = new Thread(new Time(i,p));
timeThread.start();
}
if (evt.getSource().equals(btnStop)) {
if (timeThread != null) {
/* stop run()メソッド */
timeThread.interrupt();
}
}
} public class Time extends Thread {
/** 時間*/
private int count =-1 ;
private int thIndex;
private int thIndex1;
public Time(int index,int index1) {
thIndex = index;
thIndex1 = index1 ;
} @Override
public void run() {
try {
while (isThread) {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(" hh:mm:ss ");
txtText.append("Thread"+thIndex+"("+ ++count + ")"+"="+ sdf.format(d)+"\n");
timeThread.sleep(1000);
}
timeThread.sleep(thIndex1) ;
} catch (InterruptedException e) { }
}
} public static void main(String[] args) {
Main main = new Main();
main.setVisible(true);
}
}
就是想让第一个线程3秒后停下来出现字停下 第二个5秒后停下来出现停下 第三个7秒后停下
完成要求:4个按钮 1个textarea表示时间
3个开始1个停止 开始毽子 开始1显示thread1(0)=系统时间 每秒更新 开始2显示thread2(0)=系统时间 每秒更新
开始3显示thread3(0)=系统时间 每秒更新
停止毽子 开始1三秒后出现停止 开始2五秒后出现停止 开始2七秒后出现停止做出来感觉不是很难 但是刚开始做的时候 我觉得很乱 做完以后对线程有了一点点了解
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;/**
*
* @author ouso
*/
public class Main extends JFrame implements ActionListener { /**
* @param args the command line arguments
*/
/** ボタン(start1 ) */
public JButton btnStart1;
/** ボタン(start2 ) */
public JButton btnStart2;
/** ボタン(start3 ) */
public JButton btnStart3;
/** ボタン(stop ) */
public JButton btnStop;
/** 表示するところ */
public TextArea txtText;
/** パネル*/
public JPanel panThread;
/** パネルtext */
public JPanel panText;
/** スクロールバー */
public JScrollPane jspPane;
/** 表示タイマー1 */
private Date time;
/** 表示時間*/
private Thread timeThread;
public int i;
public int stopTime;
public boolean isThread; public Main() {
/* フレームの設定 */
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("タイマー");
setSize(400, 350); //画面定義
btnStart1 = new JButton("start1");
btnStart2 = new JButton("start2");
btnStart3 = new JButton("start3");
btnStop = new JButton("stop");
panText = new JPanel();
txtText = new TextArea(10, 10); /* 位置設定 */
txtText.setBounds(10, 10, 350, 200);
btnStart1.setBounds(10, 250, 80, 30);
btnStart2.setBounds(100, 250, 80, 30);
btnStart3.setBounds(190, 250, 80, 30);
btnStop.setBounds(280, 250, 80, 30);
/* パネル設定 */
panText.setLayout(null);
panText.add(txtText);
panText.add(btnStart1);
btnStart1.addActionListener(this);
panText.add(btnStart2);
btnStart2.addActionListener(this);
panText.add(btnStart3);
btnStart3.addActionListener(this);
panText.add(btnStop);
btnStop.addActionListener(this); /* フレームに追加 */
getContentPane().add(panText);
} public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(btnStart1)) {
i = 1;
stopTime = 3000;
isThread = true;
/* 新しいスレッド */
timeThread = new Thread(new Time(i, stopTime));
timeThread.start(); }
if (evt.getSource().equals(btnStart2)) {
i = 2;
stopTime = 5000;
isThread = true;
/* 新しいスレッド */
timeThread = new Thread(new Time(i, stopTime));
timeThread.start(); }
if (evt.getSource().equals(btnStart3)) {
i = 3;
stopTime = 7000;
isThread = true;
/* 新しいスレッド */
timeThread = new Thread(new Time(i, stopTime));
timeThread.start(); }
if (evt.getSource().equals(btnStop)) {
try {
isThread = false;
timeThread = new Thread(new Time(i, stopTime));
timeThread.start();
timeThread.join();
txtText.append("終了\n");
} catch (Exception e) {
//何もしない
}
}
} public class Time extends Thread { /** 時間*/
private int count = -1;
private int thIndex;
private int thIndex1; public Time(int index, int index1) {
thIndex = index;
thIndex1 = index1;
} @Override
public void run() {
try {
while (isThread) {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(" hh:mm:ss ");
txtText.append("Thread" + thIndex + "(" + ++count + ")" + "=" + sdf.format(d) + "\n");
Thread.sleep(1000);
}
Thread.sleep(thIndex1);
} catch (InterruptedException e) {
}
}
} public static void main(String[] args) {
Main main = new Main();
main.setVisible(true);
}
}