java 在JTextArea中如何显示时间 按了开始键子后间格式是hh:mm:ss (系统时间)每过一秒另一行显示 09:30:33 09:30:34 09:30:35 09:30:3609:30:37就这样一直显示呢??按停止键后就那么表示着谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.BorderLayout;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;import javax.swing.JTextArea;public class ShowTimeInTextArea { public static void main(String[] args) { TextAreaFrame frame = new TextAreaFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }}class TextAreaFrame extends JFrame { private JTextArea area; private JPanel areaPanel; private JPanel buttonPanel; private JScrollPane pane; private JButton startButton; private JButton stopButton; private SimpleDateFormat sdf; private boolean runflag; public TextAreaFrame() { sdf = new SimpleDateFormat("HH:mm:ss"); setSize(400, 300); areaPanel = new JPanel(); area = new JTextArea(8, 20); pane = new JScrollPane(area); areaPanel.add(pane); buttonPanel = new JPanel(); startButton = new JButton("开始"); stopButton = new JButton("停止"); buttonPanel.add(startButton); buttonPanel.add(stopButton); startButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { runflag = true; new Thread(new Runn()).start(); } }); stopButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { runflag = false; } }); add(areaPanel, BorderLayout.NORTH); add(buttonPanel, BorderLayout.SOUTH); } private class Runn implements Runnable { @Override public void run() { while (runflag) { area.append(sdf.format(new Date()) + "\n"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }} 哦,晚了一步呵呵,楼上的写的很标准啊,很好!我的也贴上了,刚学java不长时间,新手希望大家指教!import java.applet.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.util.*;import java.text.SimpleDateFormat;public class Test extends JFrame implements ActionListener,Runnable{ JTextArea jta; JButton btn1; JButton btn2; JScrollPane jsp; private static SimpleDateFormat sdf = new SimpleDateFormat("kk:mm:ss",Locale.SIMPLIFIED_CHINESE); String timestr ; static boolean b; public Test(){ GridLayout grid = new GridLayout(2,1); setLayout(grid); jta = new JTextArea(); jsp = new JScrollPane(jta); add(jsp); JPanel jpl = new JPanel(); btn1 = new JButton("开始"); btn1.addActionListener(this); btn2 = new JButton("停止"); btn2.addActionListener(this); jpl.add(btn1); jpl.add(btn2); add(jpl); setVisible(true); } public void actionPerformed(ActionEvent ae){ String command = ae.getActionCommand(); if(command == "开始"){ b = true; new Thread(this).start(); } if(command =="停止"){ b = false; } } public void run(){ while(b){ try{ timestr = sdf.format(new Date())+"\n"; jta.append(timestr); Thread.sleep(1000); }catch(Exception e){ } } } public static void main(String[]args){ Test test = new Test(); test.setSize(300,300); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} 我做的这个看看,吧JLabel 换成JTextArea 就行了,每隔一秒添加.append()就行了:package myClock;import java.util.Date;public class MyThread extends Thread { public boolean flag=false; @SuppressWarnings("deprecation") public synchronized void start(){ while(flag){ Date time=new Date(); MainFrame.setM3(time.getSeconds()); MainFrame.setM2(time.getMinutes()); MainFrame.setM1(time.getHours()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } MainFrame.getLab().setText(MainFrame.getM1()+":"+MainFrame.getM2()+":"+MainFrame.getM3()); } } public synchronized void pause(){ try{ wait(); }catch(Exception e){ e.printStackTrace(); } }}package myClock;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.JTextField;public class MainFrame extends JFrame { private static final long serialVersionUID = 1L; private static JLabel lab; MyThread t1; public static JLabel getLab() { return lab; } public static void setLab(JLabel lab) { MainFrame.lab = lab; } private static JButton b1, b2; public static JButton getB1() { return b1; } public static void setB1(JButton b1) { MainFrame.b1 = b1; } private static int m1, m2, m3; public static int getM1() { return m1; } public static void setM1(int m1) { MainFrame.m1 = m1; } public static int getM2() { return m2; } public static void setM2(int m2) { MainFrame.m2 = m2; } public static int getM3() { return m3; } public static void setM3(int m3) { MainFrame.m3 = m3; } public MainFrame() { super("clock"); this.setBounds(400, 200, 400, 500); this.setLayout(null); b1 = new JButton("暂停"); b1.setBounds(50, 300, 100, 30); b1.addActionListener(new Handler()); this.add(b1); b2 = new JButton("清零"); b2.setBounds(250, 300, 100, 30); b2.addActionListener(new ClearHandler()); this.add(b2); lab = new JLabel(); lab.setHorizontalAlignment(JTextField.CENTER); lab.setText(00 + ":" + 00 + ":" + 00); lab.setBounds(100, 100, 200, 30); this.add(lab); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class Start extends Thread { public void run() { t1 = new MyThread(); t1.flag=true; t1.start(); } } class pause extends Thread { public void run() { t1.flag=false; t1.pause(); } } class Handler implements ActionListener { public void actionPerformed(ActionEvent arg0) { if (MainFrame.getB1().getText().equals("暂停")) { MainFrame.getB1().setText("开始"); Start t1 = new Start(); t1.start(); } else { MainFrame.getB1().setText("暂停"); } } } class ClearHandler implements ActionListener { public void actionPerformed(ActionEvent e) { MainFrame.getLab().setText(00 + ":" + 00 + ":" + 00); } }public static void main(String[]args){ new MainFrame(); }} 在java里怎么实现不满足条件指定控制台输出指定对象附源码 求助一个JAVA作业题啊。。。各位大哥大姐帮帮忙~~~ 在线等,哪个线程先执行,说明为什么 有点发迷,请指点一下 this的作用 关于java配置的问题呢???刚开始学不懂呢~~~~~~~~~~~~~ JDK1.5中的新功能注释,最大的作用是什么呢? java应用程序保存 书上的简单代码无法执行,新人请求指点(jb下编写) (敬请关注)作为一个真正Java编码人员,你需要了解或者熟悉多少相关知识点? 一个JAVA题目。。。。 在JEditorPane中改变选中的文字的颜色
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;
import javax.swing.JTextArea;public class ShowTimeInTextArea {
public static void main(String[] args) {
TextAreaFrame frame = new TextAreaFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class TextAreaFrame extends JFrame {
private JTextArea area;
private JPanel areaPanel;
private JPanel buttonPanel;
private JScrollPane pane;
private JButton startButton;
private JButton stopButton;
private SimpleDateFormat sdf;
private boolean runflag; public TextAreaFrame() {
sdf = new SimpleDateFormat("HH:mm:ss");
setSize(400, 300);
areaPanel = new JPanel();
area = new JTextArea(8, 20);
pane = new JScrollPane(area);
areaPanel.add(pane); buttonPanel = new JPanel();
startButton = new JButton("开始");
stopButton = new JButton("停止");
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
runflag = true;
new Thread(new Runn()).start();
}
}); stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
runflag = false;
} }); add(areaPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
} private class Runn implements Runnable {
@Override
public void run() {
while (runflag) {
area.append(sdf.format(new Date()) + "\n");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.text.SimpleDateFormat;public class Test extends JFrame implements ActionListener,Runnable{
JTextArea jta;
JButton btn1;
JButton btn2;
JScrollPane jsp; private static SimpleDateFormat sdf = new SimpleDateFormat("kk:mm:ss",Locale.SIMPLIFIED_CHINESE);
String timestr ;
static boolean b;
public Test(){
GridLayout grid = new GridLayout(2,1);
setLayout(grid);
jta = new JTextArea();
jsp = new JScrollPane(jta);
add(jsp);
JPanel jpl = new JPanel();
btn1 = new JButton("开始");
btn1.addActionListener(this);
btn2 = new JButton("停止");
btn2.addActionListener(this);
jpl.add(btn1);
jpl.add(btn2);
add(jpl);
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
String command = ae.getActionCommand();
if(command == "开始"){
b = true;
new Thread(this).start();
}
if(command =="停止"){
b = false;
}
}
public void run(){
while(b){
try{
timestr = sdf.format(new Date())+"\n";
jta.append(timestr);
Thread.sleep(1000);
}catch(Exception e){
}
}
}
public static void main(String[]args){
Test test = new Test();
test.setSize(300,300);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public boolean flag=false;
@SuppressWarnings("deprecation")
public synchronized void start(){
while(flag){
Date time=new Date();
MainFrame.setM3(time.getSeconds());
MainFrame.setM2(time.getMinutes());
MainFrame.setM1(time.getHours()); try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} MainFrame.getLab().setText(MainFrame.getM1()+":"+MainFrame.getM2()+":"+MainFrame.getM3());
}
}
public synchronized void pause(){
try{
wait();
}catch(Exception e){
e.printStackTrace();
}
}}package myClock;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.JTextField;public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static JLabel lab;
MyThread t1; public static JLabel getLab() {
return lab;
} public static void setLab(JLabel lab) {
MainFrame.lab = lab;
} private static JButton b1, b2; public static JButton getB1() {
return b1;
} public static void setB1(JButton b1) {
MainFrame.b1 = b1;
} private static int m1, m2, m3; public static int getM1() {
return m1;
} public static void setM1(int m1) {
MainFrame.m1 = m1;
} public static int getM2() {
return m2;
} public static void setM2(int m2) {
MainFrame.m2 = m2;
} public static int getM3() {
return m3;
} public static void setM3(int m3) {
MainFrame.m3 = m3;
} public MainFrame() {
super("clock");
this.setBounds(400, 200, 400, 500);
this.setLayout(null); b1 = new JButton("暂停");
b1.setBounds(50, 300, 100, 30);
b1.addActionListener(new Handler());
this.add(b1); b2 = new JButton("清零");
b2.setBounds(250, 300, 100, 30);
b2.addActionListener(new ClearHandler());
this.add(b2); lab = new JLabel();
lab.setHorizontalAlignment(JTextField.CENTER);
lab.setText(00 + ":" + 00 + ":" + 00);
lab.setBounds(100, 100, 200, 30);
this.add(lab); this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} class Start extends Thread { public void run() {
t1 = new MyThread();
t1.flag=true;
t1.start(); } } class pause extends Thread {
public void run() {
t1.flag=false;
t1.pause();
} } class Handler implements ActionListener { public void actionPerformed(ActionEvent arg0) {
if (MainFrame.getB1().getText().equals("暂停")) {
MainFrame.getB1().setText("开始");
Start t1 = new Start();
t1.start();
} else {
MainFrame.getB1().setText("暂停"); } } } class ClearHandler implements ActionListener {
public void actionPerformed(ActionEvent e) { MainFrame.getLab().setText(00 + ":" + 00 + ":" + 00); } }
public static void main(String[]args){
new MainFrame();
}}