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.画完第一条线再画第二条线时,前一条线会消失,怎么让画后的直线不消失呢?
解决方案 »
- java的垃圾回收的疑问,求高手指点
- JFrame不需要重写windowClosing了吗?
- 求助Java 中调用C 函数遇到的问题。
- 一个字符串类型的变量,保存sql,执行时总是说 ORA-00911: 无效字符,为什么????????????????????????????????
- 读一张图,gif格式,然后往这张图上写字,怎么实现,能否给个例子?谢谢
- java打开本地程序,如何自己退出,而不影响本地程序的运行?
- 各位师兄,做的程序在系统任务栏显示相关菜单和图标,怎么搞?
- jdk配置问题
- copy下来的这个程序有问题吗(显示图片)
- JAVA控制台输入问题
- 一次提出两个问题!!~~~~~~~~~~~~
- 本人第一次接触JBOSS,希望大家给我提提意见哈,不甚感谢哈!!
在paint()中将Vector中的所有直线画出来,
这样,就不会消失了。