自定义组件为0x0 px。
import java.awt.*;import javax.swing.*;public class Catch { public class MyGraphics extends JComponent { private static final long serialVersionUID = 1L; MyGraphics() { setPreferredSize(new Dimension(500, 100)); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.fillRect(200, 62, 30, 10); } } public void createGUI() { final Jframe frame = new Jframe(); JPanel panel = new JPanel(); panel.add(new MyGraphics()); frame.add(panel); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(Jframe.DISPOSE_ON_CLOSE); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { Catch GUI = new Catch(); GUI.createGUI(); } }); }}


