[英]Why can't I paint onto my JPanel using Graphics?
当我尝试绘制扩展版的JPanel时,我一直遇到这个问题。 每次,我最终都找到一种使其工作的方法。 但是我想确切地知道为什么以这种方式向JPanel绘画不起作用?
public class MyPanel extends JPanel {
public MyPanel(){
init();
}
public void init(){
JFrame frame = new JFrame("");
frame.add(this);
this.setPreferredSize(new Dimension(100,100));
this.setBackground(Color.RED);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
Graphics g = this.getGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, 50, 50);
}
public static void main(String[] args) {
new MyPanel();
}
}
当我运行此代码时,我看到了setBackground(Color.RED)所期望的红色背景,但是看不到使用Graphics对象绘制的蓝色矩形。 即使我在init()的最后一行调用repaint(),它也不会显示。
该代码有什么问题,所以将来可以避免吗?
为什么不能使用“图形”在我的JPanel上绘画?
因为这不是正确的方法。 您应该重写paintComponent
方法:
public class MyPanel extends JPanel {
public MyPanel(){
init();
}
public void init(){
JFrame frame = new JFrame("");
frame.add(this);
this.setPreferredSize(new Dimension(100,100));
this.setBackground(Color.RED);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new MyPanel();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(0, 0, 50, 50);
}
}
如果不这样做,您的图形将仅显示一会儿,然后消失。 这样,无论何时调用repaint
方法(或应用程序自身重新绘制),该方法都会被调用,并且矩形将被填充。
Swing中的自定义绘制通常是通过重写从JComponent
扩展的类的paintComponent
方法来完成的
有关详细信息,请参见在自定义绘画中执行
基本上,问题是,Swing中的绘画是破坏性的过程。 每次发生油漆循环时,都应清洁并重新生成输出。 getGraphics
不可靠。 它可以返回null,并且您使用它paint
任何内容都将在下一个绘制周期中被过度绘制
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.