programm1
import java.awt.*;
class example extends Frame
{
public static void main(String args[])
{Frame f=new Frame( "直线图 ");
myCanvas canvas=new myCanvas();
f.add(canvas);
Button b1=new Button( "退出 ");
f.setLayout(new FlowLayout());
f.add(b1);
}
}
class myCanvas extends Canvas
{ myCanvas(){}
public void paint(Graphics g)
{ g.setColor(Color.red);
g.drawLine(0,50,200,50);
}
}
------解决方案--------------------
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class example extends Frame
{
public static void main(String args[])
{
Frame f = new Frame( "直线图 ");
myCanvas canvas = new myCanvas();
f.add(canvas);
Button b1 = new Button( "退出 ");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}});
f.setLayout(new FlowLayout());
f.add(b1);
f.pack();
f.setVisible(true);
}
}
class myCanvas extends Canvas
{
myCanvas()
{
this.setPreferredSize(new Dimension(500, 500));
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(0, 50, 200, 50);
}
}
===========================
赶快结帖
------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
public class Test extends Frame implements ActionListener
{
Test(String str)
{
super(str);
myCanvas canvas=new myCanvas();
add(canvas);
Button b1=new Button( "退出 ");
b1.addActionListener(this);
setLayout(new FlowLayout());
setBounds(100,100,100,100);
add(b1);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
public static void main(String args[])