【发布时间】:2017-04-06 01:11:51
【问题描述】:
我想将它添加到另一个 JPanel 中,但它在那里不可见。我的另一个Jpanel 叫做bottomPanel。 paintComponent 应该显示在底部面板中
bottomPanel.setLayout(null);
TestPane tp = new TestPane();
bottomPanel.add(tp);
我已经扩展了 Jpanel。
public class TestPane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int width = getWidth() - 100;
int height = getHeight() - 100;
int x = (getWidth() - width) / 2;
int y = (getHeight() - height) / 2;
g2d.setColor(Color.RED);
g2d.drawRect(x, y, width, height);
g2d.dispose();
}
}
【问题讨论】:
-
如何将面板添加到父容器中?这是如何在屏幕上显示的?
-
bottomPanel 是另一个我希望在其中显示的面板。不是 bottomPanel.add(new TestPane());够了吗?抱歉,我是 java 新手
-
我们不需要屏幕截图,我们需要重现您的问题的代码。
-
我已经添加了完整的代码
-
bottomPanel.setLayout(null);... 说得够多了
标签: java swing jpanel paintcomponent