我写了一个这如下的选定一区域的程序.
结果什么都没有显示.请大家帮忙看看..
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
class CaptureScreen extends Applet implements MouseListener
\\ 选定一区域并画出矩形.
{
int x1,x2,y1,y2,width,height;
boolean flag = false;
public void init()
{
this.addMouseListener(this);
}
public void mousePressed(MouseEvent Event)
{
flag = true;
x1 = Event.getX();
y1 = Event.getY();
}
public void mouseReleased(MouseEvent Event)
{
x2 = Event.getX();
y2 = Event.getY();
}
public void paint(Graphics g)
{
if(flag)
{
if(x2<x1 && y2<y1) {x1 = x2;y1 = y2;}
if(x2<x1 && y1>y2) {x1 = x2;}
if(x2>x1 && y2<y1) {y1 = y2;}
width=Math.abs(x2-x1);
height=Math.abs(y2-y1);
g.drawRect(x1,y1,width,height);
}
}
public void mouseClicked(MouseEvent Event) {}
public void mouseEntered(MouseEvent Event) {}
public void mouseExited(MouseEvent Event) {}
}
public class Appl5_1
{ \\main 方法
public static void main(String[] args)
{
JFrame Frame = new JFrame();
Frame.setSize(1000,3000);
Frame.setTitle("");
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CaptureScreen capturescreen = new CaptureScreen();
Frame.add(capturescreen);
\\这里添加组件,怎么没有显示??
Frame.setVisible(true);
}
}