当前位置: 技术问答>java相关
帮忙看个程序。
来源: 互联网 发布时间:2015-09-09
本文导语: 实现跟踪鼠标画个小矩形,并填充,但是在屏蔽repaint()方法后,鼠标点击事件仍能自动调用paintComponent()方法,Why?Thanks.. package graphicstest; /** * Title: MouseAction Test * Description: * Copyright: Copyright guanzhonj (c) 200...
实现跟踪鼠标画个小矩形,并填充,但是在屏蔽repaint()方法后,鼠标点击事件仍能自动调用paintComponent()方法,Why?Thanks..
package graphicstest;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.swing.border.*;
public class Common {
public static void main(String [] args)
{
CommonFrame frame=new CommonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CommonFrame extends JFrame
{
public CommonFrame()
{
setTitle("Common");
setSize(400,300);
CommomPanel panel=new CommomPanel();
Container contentPane=getContentPane();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS));
contentPane.add(panel);
label= new JLabel("mouseAction");
contentPane.add(label);
}
private JLabel label;
}
class CommomPanel extends JPanel
{
public CommomPanel()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
int optionX=e.getX();
int optionY=e.getY();
if(point==null)
point=new Point(optionX,optionY);
else
point.setLocation(optionX,optionY);
//repaint();
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBorder(new LineBorder(Color.orange));
if(point!=null)
{
g.drawRect(point.x,point.y,WIDTH,HEIGHT);
g.setColor(Color.magenta);
g.fillRect(point.x+1,point.y+1,WIDTH+1,HEIGHT+1);
}
}
private Point point;
private static final int WIDTH=10;
private static final int HEIGHT=10;
}
package graphicstest;
/**
*
Title: MouseAction Test
*
Description:
*
Copyright: Copyright guanzhonj (c) 2002
*
Company: developer
* @author unascribed
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.swing.border.*;
public class Common {
public static void main(String [] args)
{
CommonFrame frame=new CommonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CommonFrame extends JFrame
{
public CommonFrame()
{
setTitle("Common");
setSize(400,300);
CommomPanel panel=new CommomPanel();
Container contentPane=getContentPane();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS));
contentPane.add(panel);
label= new JLabel("mouseAction");
contentPane.add(label);
}
private JLabel label;
}
class CommomPanel extends JPanel
{
public CommomPanel()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
int optionX=e.getX();
int optionY=e.getY();
if(point==null)
point=new Point(optionX,optionY);
else
point.setLocation(optionX,optionY);
//repaint();
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBorder(new LineBorder(Color.orange));
if(point!=null)
{
g.drawRect(point.x,point.y,WIDTH,HEIGHT);
g.setColor(Color.magenta);
g.fillRect(point.x+1,point.y+1,WIDTH+1,HEIGHT+1);
}
}
private Point point;
private static final int WIDTH=10;
private static final int HEIGHT=10;
}
|
在paintComponent中调用setBorder,而setBorder会调用repaint(), 又调用了paintComponent,所以形成一个循环了,相当于不断的调用repaint();