为什么这个程序画了直线后,那条直线不能马上显示,而要对窗口进行放大或缩小时才能看到?
还有如果要想多画几条直线,怎样才能保留原先画的直线?
请指教!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class man extends JFrame
{
int x1,y1,x2,y2;
public man(String str)
{
super(str);
}
public void put(man JF)
{
Container contentPane=JF.getContentPane();
contentPane.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
}
});
women JP=new women();
contentPane.add(JP);
JF.setSize(600,400);
JF.setLocation(180,180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setVisible(true);
}
public static void main(String[] args)
{
man JF=new man("菜单窗口");
JF.put(JF);
}
class women extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(x1,y1,x2,y2);
}
}
}
----------------解决方案--------------------------------------------------------
在改了xy座标之后,再调用一下repaint()方法
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------
帮你改了,不知道你是不是想实现这样的效果?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class man extends JFrame
{
int x1,y1,x2,y2;
public man(String str)
{
super(str);
}
public void put(man JF)
{
women JP=new women();
this.add(JP);
JF.setSize(600,400);
JF.setLocation(180,180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setVisible(true);
}
public static void main(String[] args)
{
man JF=new man("菜单窗口");
JF.put(JF);
}
class women extends JPanel
{
public women()
{
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
repaint();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
repaint();
}
});
}
public void paintComponent(Graphics g)
{
// super.paintComponent(g);
g.drawLine(x1,y1,x2,y2);
}
}
}
----------------解决方案--------------------------------------------------------
如果在每次画直线的时候,都能保留先前画的直线,那该怎么实现啊?请赐教!!
----------------解决方案--------------------------------------------------------