- Java code
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; } }}
我这个程序开始执行有时间能画出方块,有时候画不出来是怎么回事哈,谢谢??
------解决方案--------------------
将this.setVisible(true)移到构造函数的最下方
------解决方案--------------------
this.setVisible(true)
放主函数 new TestBiao();之后最好,一目了然
------解决方案--------------------
我刚测试了下,每次时间的方格都是出来的呀?楼主你到底想要什么结果?
------解决方案--------------------
像this.setVisible(true) ,setResized(true)之类的最好放在构造方法最后或者外面,这些方法放在构造方法最前面会出现lz说的情况。