import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDraw extends JPanel implements MouseMotionListener, MouseListener{
private JFrame f;
private JTextField tf;
private int initx,inity,currx,curry;
public static void main(String args[]) {
MyDraw two = new MyDraw();
two.go();
}
public void go() {
f = new JFrame("Two listeners example");
f.getContentPane().add (new Label ("Drag the mouse to finish the line"),
BorderLayout.PAGE_START);
f.getContentPane().add(this);
tf = new JTextField (30);
f.getContentPane().add (tf, BorderLayout.PAGE_END);
this.addMouseMotionListener(this); //?getContentPane()
this.addMouseListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 200);
f.setVisible(true);
}
public MyDraw()
{
initx = 0;
inity = 0;
currx = 0;
curry = 0;
}
public void paint (Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(initx,inity,currx,curry);
}
// These are MouseMotionListener events
public void mouseDragged (MouseEvent e) {
currx = e.getX();
curry = e.getY();
String s = "Mouse dragging: X = " + e.getX() + " Y = " + e.getY();
tf.setText (s);
repaint();
} public void mouseMoved (MouseEvent e) {}
// These are MouseListener events
public void mouseClicked (MouseEvent e) {}
public void mouseEntered (MouseEvent e) {
String s = "The mouse entered";
tf.setText (s);
}
public void mouseExited (MouseEvent e) {
String s = "The mouse has left the building";
tf.setText (s);
}
public void mousePressed (MouseEvent e)
{
String s = "The mouse Pressed";
tf.setText (s);
initx = e.getX();
inity = e.getY();
}
public void mouseReleased (MouseEvent e)
{
String s = "The mouse Released";
tf.setText (s);
}
}
1.其中是不是不能在JPanel类对象上画图?所以要引入JFrame的对象?
2.画完第一条线再画第二条线时,前一条线会消失,怎么让画后的直线不消失呢?
import java.awt.event.*;
import javax.swing.*;
public class MyDraw extends JPanel implements MouseMotionListener, MouseListener{
private JFrame f;
private JTextField tf;
private int initx,inity,currx,curry;
public static void main(String args[]) {
MyDraw two = new MyDraw();
two.go();
}
public void go() {
f = new JFrame("Two listeners example");
f.getContentPane().add (new Label ("Drag the mouse to finish the line"),
BorderLayout.PAGE_START);
f.getContentPane().add(this);
tf = new JTextField (30);
f.getContentPane().add (tf, BorderLayout.PAGE_END);
this.addMouseMotionListener(this); //?getContentPane()
this.addMouseListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 200);
f.setVisible(true);
}
public MyDraw()
{
initx = 0;
inity = 0;
currx = 0;
curry = 0;
}
public void paint (Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(initx,inity,currx,curry);
}
// These are MouseMotionListener events
public void mouseDragged (MouseEvent e) {
currx = e.getX();
curry = e.getY();
String s = "Mouse dragging: X = " + e.getX() + " Y = " + e.getY();
tf.setText (s);
repaint();
} public void mouseMoved (MouseEvent e) {}
// These are MouseListener events
public void mouseClicked (MouseEvent e) {}
public void mouseEntered (MouseEvent e) {
String s = "The mouse entered";
tf.setText (s);
}
public void mouseExited (MouseEvent e) {
String s = "The mouse has left the building";
tf.setText (s);
}
public void mousePressed (MouseEvent e)
{
String s = "The mouse Pressed";
tf.setText (s);
initx = e.getX();
inity = e.getY();
}
public void mouseReleased (MouseEvent e)
{
String s = "The mouse Released";
tf.setText (s);
}
}
1.其中是不是不能在JPanel类对象上画图?所以要引入JFrame的对象?
2.画完第一条线再画第二条线时,前一条线会消失,怎么让画后的直线不消失呢?
在paint()中将Vector中的所有直线画出来,
这样,就不会消失了。