我正在尝试向我的窗口添加第二个 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)
(您已经注释掉了这部分代码,因此我无法判断您是否正在这样做,但是如果您不这样做,可能会给您带来问题)
关于java - BoxLayout 未添加另一个 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13505652/