为什么这个程序画了直线后,那条直线不能马上显示,而要对窗口进行放大或缩小时才能看到?
还有如果要想多画几条直线,怎样才能保留原先画的直线?
请指教!!
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);
}
}
}