import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class listener extends MouseAdapter
{
private int x1,y1,x2,y2;
public void mousePressed(MouseEvent evt)
{
x1=evt.getX();
y1=evt.getY();
}
public void Released(MouseEvent evt)
{
x2=evt.getX();
y2=evt.getY();
}
public void paint(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
}
}
class mypanel8 extends JPanel
{
private JButton bt;
public mypanel8()
{
bt=new JButton("exit");
add(bt);
bt.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
int i=evt.getClickCount();
if(i==2)
System.exit(0);
}
});
listener listener1=new listener();
addMouseListener(listener1);
}
}
class myframe8 extends JFrame
{
public myframe8()
{
mypanel8 pp8=new mypanel8();
Container pane=getContentPane();
pane.add(pp8);
setSize(300,300);
setLocation(300,100);
setTitle("test");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
}
}
public class test
{
public static void main(String args[])
{
myframe8 fm8 =new myframe8();
fm8.show();
}
}
以上是一个小程序,通过了编译,要实现的是当按下鼠标然后拖动,最后释放鼠标,会在起点和终点画一条直线,双击界面上的button键,则会关闭窗口。这个程序改了好几次,可还是画不出图,有哪个高手可以帮忙解答一下,万分感谢
----------------解决方案--------------------------------------------------------
你的paint方法放错地方了
----------------解决方案--------------------------------------------------------
不好意思一直没上,电脑出了问题,请问paint 应该放在哪里呢
----------------解决方案--------------------------------------------------------
没有人可以回答我吗??好伤心啊
----------------解决方案--------------------------------------------------------
没有人可以回答我吗??好伤心啊
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mypanel8 extends JPanel
{
private int x1,y1,x2,y2;
public mypanel8()
{
this.setBackground(Color.white);
listener listener1=new listener();
this.addMouseListener(listener1);
}
class listener extends MouseAdapter
{
public void mousePressed(MouseEvent evt)
{
x1=evt.getX();
y1=evt.getY();
}
public void mouseReleased(MouseEvent evt)
{
x2=evt.getX();
y2=evt.getY();
repaint();
}
}
public void paint(Graphics g )
{
super.paint(g);
g.setColor(Color.red);
g.drawLine(x1,y1,x2,y2);
}
}
class myframe8 extends JFrame
{
public myframe8()
{
JButton bt=new JButton("exit");
bt.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
int i=evt.getClickCount();
if(i==2)
System.exit(0);
}
});
mypanel8 pp8=new mypanel8();
Container pane=getContentPane();
pane.add(bt,BorderLayout.NORTH);
pane.add(pp8,BorderLayout.CENTER);
setSize(300,300);
setLocation(300,100);
setTitle("test");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
}
}
public class drawLine {
public static void main(String args[])
{
myframe8 fm8 =new myframe8();
fm8.show();
}
}
----------------解决方案--------------------------------------------------------
谢谢,我来试一试啊,万分感谢
----------------解决方案--------------------------------------------------------