我正在尝试向我的窗口添加第二个JPanel
,该窗口使用 BoxLayout
.出于某种原因,除了我被覆盖的JPanel
之外,一切都拒绝出现。
代码如下:
public void initialize()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Polygon Viewer");
frame.setContentPane(makeGUI(frame));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,700);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
public JPanel makeGUI(final JFrame frame)
{
JPanel gui = new JPanel();
gui.setLayout(new BoxLayout(gui,BoxLayout.PAGE_AXIS));
class GraphPaint extends JPanel
{
public void paintComponent(Graphics g)
{
// Lots of graphics stuff
}
}
GraphPaint mainG = new GraphPaint();
mainG.setMinimumSize(new Dimension(600,600));
mainG.setMaximumSize(new Dimension(600,600));
mainG.setPreferredSize(new Dimension(600,600));
gui.add(mainG);
// Everything beyond here refuses to show up in the window
JPanel lowerBar = new JPanel();
lowerBar.setLayout(new BoxLayout(lowerBar,BoxLayout.LINE_AXIS));
lowerBar.setMinimumSize(new Dimension(600,100));
lowerBar.setPreferredSize(new Dimension(600,100));
lowerBar.setBackground(Color.RED);
gui.add(lowerBar);
JPanel data = new JPanel();
data.setLayout(new BoxLayout(data,BoxLayout.PAGE_AXIS));
JLabel area = new JLabel("Area: <insert area here>");
data.add(area);
JLabel perimeter = new JLabel("Perimeter: " + shape.perimeter());
data.add(perimeter);
return gui;
}
我一定搞砸了BoxLayout
设置,或者BoxLayout
不能使用BoxLayout
包含其他JPanel
?
您永远不会将data
面板添加到gui
。
另外,请确保您正在调用super.paintComponent(g)
(您已经注释掉了代码的那部分,所以我无法判断您是否在这样做,但如果您不这样做,这可能会给您带来问题)