我想将其添加到另一个JPanel中,但它在那里不可见。我的其他Jpanel被称为bottomPanel。 paintComponent应该显示在底部面板paintComponent不可见java
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();
}
}
你是如何加入面板父容器?这是如何获得在屏幕上显示? – MadProgrammer
bottomPanel是我希望显示的另一个面板。不是bottomPanel.add(new TestPane());够了吗?抱歉,我是新来的java – hello12345678
我们不需要屏幕截图,我们需要重现您的问题的代码。 –