我有这个问题困扰了我好几天.我正在制作一个特殊的油漆程序.我制作了一个JPanel,并添加了使用paint(..)方法绘制的自定义jComponents.问题是,每当我调整窗口大小时,所有添加的组件"消失"(或者只是不绘画),所以我最终得到一个空框架.
我也注意到使用这种方法时有一些奇怪的挥杆行为.我在描述此问题的代码中添加了注释.
package simple;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class SimpleFrame extends JFrame {
JPanel paintArea;
SimpleCanvas c1;
SimpleCanvas c2;
ArrayList<SimpleCanvas> list;
public static void main(String[] args) {
SimpleFrame frame = new SimpleFrame();
}
public SimpleFrame() {
super("Test");
setSize(600,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//The panel to which my SimpleCanvas objects are added
paintArea = new JPanel();
paintArea.setPreferredSize(new Dimension(600, 500));
paintArea.addMouseListener(new paintAreaMouseEvents());
getContentPane().add(paintArea, BorderLayout.CENTER);
setVisible(true);
paintArea.setVisible(true);
//A list to hold all the objects together
list = new ArrayList<SimpleCanvas>(10);
//The same …Run Code Online (Sandbox Code Playgroud)