且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将paintComponent()添加到JPanel

更新时间:2023-12-05 18:03:34

  1. 不要使用 JPanel#paint ,建议使用 JPanel#paintComponent ,请检查
  1. Don't use JPanel#paint, it is recommended that use JPanel#paintComponent, check out Performing Custom Painting
  2. Make use of appropriate layout managers. The problem you having comes from two issues. The first is, you panel has no preferred size. Override the getPreferredSize method of Class2 and return an appropriate size of the layout manager.

...

JPanel p4 = new JPanel(); // Default layout manager is FlowLayout
Class2 c2o = new Class2();
// c2o has not size (0x0 by default)...
p4.add(c2o);
p4.setBounds(0,115,500,55);
f.add(p4);