雪地,裸体,跪求,两个超小程序!
小弟在校学生,java学的不好,求各位大侠帮忙!
老师的作业 一个是简单的加减乘除的计算器 一个是带计时功能的表,电子表那种
求各位大侠了!还有几天就要交了!小弟先在这里感谢各位了!
------解决方案--------------------大功告成:
package execices;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class TestStopWatch_v10
{
public static void main(String [] args)
{
MenuBar menubar = new MenuBar();
Menu menu = new Menu( "选择 ");
MenuItem stop = new MenuItem( "计时器 ");
MenuItem time = new MenuItem( "时间 ");
menu.add(stop);
menu.add(time);
menubar.add(menu);
final Frame f = new Frame( "StopWatch ");
f.setMenuBar(menubar);
f.setSize(200,200);
final StopWatch_v10 stop_1 = new StopWatch_v10();
final StopWatch_v20 time_1 = new StopWatch_v20();
stop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
f.remove(time_1);
f.add(stop_1);
f.validate();
}
});
time.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
f.remove(stop_1);
f.add(time_1);
f.validate();
}
});
f.setVisible(true);
f.validate();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
f.setVisible(false);
f.dispose();
System.exit(0);
}
});
}
}
class StopWatch_v10 extends Canvas implements Runnable
{
private long startTime = 0;
private long endTime = 0;
private boolean bstart = false;
private boolean pORr = true;
StopWatch_v10()
{
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
setSize(80,30);
}
public void paint(Graphics g)
{
SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm:ss ");
Date d = null ;
try
{
d = sdf.parse( "00:00:00 ");
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
}
d.setTime((int)(d.getTime()+(endTime - startTime)));
String display = sdf.format(d);
g.setColor(Color.black);
g.drawRect(10,10,70,28);
g.fill3DRect(12,12,66,24,false);
g.setColor(Color.WHITE);
g.drawString(display,20,30);
}
protected void processMouseEvent(MouseEvent e)
{
if(e.getID()==MouseEvent.MOUSE_PRESSED)
{
if (pORr == true)
{
bstart = true;
startTime = endTime = System.currentTimeMillis();
repaint();
new Thread(this).start();
pORr = false;
}
else
{
bstart = false;
endTime = System.currentTimeMillis();
repaint();
pORr = true;
}
}
}
public void run()
{
while(bstart)
{
try
{
Thread.sleep(100);
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
}
endTime = System.currentTimeMillis();
repaint();
}
}
}
class StopWatch_v20 extends Canvas implements Runnable
{
StopWatch_v20()
{
setSize(80,30);
new Thread(this).start();
}
public void paint(Graphics g)
{
SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm:ss ");
long time = System.currentTimeMillis();
Date d2 = new Date(time);
String display2 = sdf.format(d2);