package org.gui;import java.awt.*;
import java.awt.event.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class TestBiao extends Frame{ /**
* @param args
*/
public TestBiao(){
this.setSize(400, 400);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}
});
TestCanvas tc = new TestCanvas();
this.add(tc);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestBiao();
}}
class TestCanvas extends Canvas{
long startTime = 0;
long endTime = 0;
Date dd,dn = null;
String show = "";
boolean bTrue =true;
SimpleDateFormat sf = null;
public TestCanvas(){
this.setVisible(true);
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
sf = new SimpleDateFormat("HH:mm:ss");
try {
dd = (Date)sf.parseObject("00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
}
public void paint(Graphics g){
System.out.println("进入paint方法..");
if(bTrue){
g.fill3DRect(0, 0, 80, 30, true);
g.setColor(Color.WHITE);
g.drawString("00:00:00", 20, 20);
}else{
g.fill3DRect(0, 0, 80, 30, true);
g.setColor(Color.WHITE);
g.drawString(show, 20, 20);
}
}
protected void processMouseEvent(MouseEvent e){
if(e.getID()==MouseEvent.MOUSE_PRESSED){
System.out.println("鼠标点击..");
bTrue = true;
startTime = System.currentTimeMillis();
}else if(e.getID()==MouseEvent.MOUSE_RELEASED){
System.out.println("鼠标释放..");
bTrue = false;
}
}
}我这个程序开始执行有时间能画出方块,有时候画不出来是怎么回事哈,谢谢??
import java.awt.event.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class TestBiao extends Frame{ /**
* @param args
*/
public TestBiao(){
this.setSize(400, 400);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}
});
TestCanvas tc = new TestCanvas();
this.add(tc);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestBiao();
}}
class TestCanvas extends Canvas{
long startTime = 0;
long endTime = 0;
Date dd,dn = null;
String show = "";
boolean bTrue =true;
SimpleDateFormat sf = null;
public TestCanvas(){
this.setVisible(true);
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
sf = new SimpleDateFormat("HH:mm:ss");
try {
dd = (Date)sf.parseObject("00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
}
public void paint(Graphics g){
System.out.println("进入paint方法..");
if(bTrue){
g.fill3DRect(0, 0, 80, 30, true);
g.setColor(Color.WHITE);
g.drawString("00:00:00", 20, 20);
}else{
g.fill3DRect(0, 0, 80, 30, true);
g.setColor(Color.WHITE);
g.drawString(show, 20, 20);
}
}
protected void processMouseEvent(MouseEvent e){
if(e.getID()==MouseEvent.MOUSE_PRESSED){
System.out.println("鼠标点击..");
bTrue = true;
startTime = System.currentTimeMillis();
}else if(e.getID()==MouseEvent.MOUSE_RELEASED){
System.out.println("鼠标释放..");
bTrue = false;
}
}
}我这个程序开始执行有时间能画出方块,有时候画不出来是怎么回事哈,谢谢??
public TestBiao(){
this.setSize(400, 400);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}
});
TestCanvas tc = new TestCanvas();
this.add(tc);
this.setVisible(true);
}
package org.gui;import java.awt.*;
import java.awt.event.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class TestBiao extends Frame{ /**
* @param args
*/
public TestBiao(){
this.setSize(400, 400);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}
});
TestCanvas tc = new TestCanvas();
new Thread(tc).start();
this.add(tc);
}
public static void main(String[] args) {
new TestBiao();
}}
class TestCanvas extends Canvas implements Runnable{
long startTime = 0;
long endTime = 0;
Date dd,dn = null;
String show = "";
boolean bTrue =true;
SimpleDateFormat sf = null;
public TestCanvas(){
System.out.println("进入构造方法之中....");
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
sf = new SimpleDateFormat("HH:mm:ss");
try {
dd = (Date)sf.parseObject("00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
this.setVisible(true);
}
public void paint(Graphics g){
System.out.println("进入paint方法..");
if(bTrue){
g.fill3DRect(0, 0, 80, 30, true);
g.setColor(Color.WHITE);
g.drawString("00:00:00", 20, 20);
}else{
g.fill3DRect(0, 0, 80, 30, true);
g.setColor(Color.WHITE);
g.drawString(show, 20, 20);
}
}
protected void processMouseEvent(MouseEvent e){
if(e.getID()==MouseEvent.MOUSE_PRESSED){
System.out.println("鼠标点击..");
bTrue = true;
startTime = System.currentTimeMillis();
}else if(e.getID()==MouseEvent.MOUSE_RELEASED){
System.out.println("鼠标释放..");
bTrue = false;
}
}
public void run() {
System.out.println("进入RUN方法...");
while(bTrue){
endTime = System.currentTimeMillis();
dn = new Date(dd.getTime()+ endTime - startTime);
show = sf.format(dn);
System.out.println(show);
repaint();
try {
System.out.println("WHILE 循环中...");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("退出RUN方法...");
}
}