Java BoxLayout不添加另一个JPanel
我正在尝试向我的窗口添加第二个Java BoxLayout不添加另一个JPanel,java,swing,jpanel,boxlayout,Java,Swing,Jpanel,Boxlayout,我正在尝试向我的窗口添加第二个JPanel,它使用BoxLayout。由于某种原因,我覆盖的JPanel之外的所有内容都拒绝显示 代码如下: public void initialize() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Polygon Viewer"); frame.setContentPane(makeGUI(frame)); frame
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;
}
public void initialize()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame=新的JFrame(“多边形查看器”);
setContentPane(makeGUI(frame));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架尺寸(600700);
frame.setLocationRelativeTo(空);
frame.setresizeable(false);
frame.setVisible(true);
}
公共JPanel makeGUI(最终JFrame)
{
JPanel gui=新的JPanel();
setLayout(新的BoxLayout(gui,BoxLayout.PAGE_轴));
类GraphPaint扩展了JPanel
{
公共组件(图形g)
{
//很多图形的东西
}
}
GraphPaint mainG=新的GraphPaint();
维护设置最小尺寸(新尺寸(600600));
维护设置最大尺寸(新尺寸(600600));
维护设置首选尺寸(新尺寸(600600));
gui.add(mainG);
//这里以外的一切都拒绝出现在窗户上
JPanel lowerBar=新的JPanel();
lowerBar.setLayout(新的BoxLayout(lowerBar,BoxLayout.LINE_轴));
降低基准设置最小尺寸(新尺寸(600100));
降低基准设置首选尺寸(新尺寸(600100));
下半身立根(颜色:红色);
添加(lowerBar);
JPanel data=新的JPanel();
data.setLayout(新的BoxLayout(数据,BoxLayout.PAGE_轴));
JLabel区域=新JLabel(“区域:”);
数据。添加(区域);
JLabel periodium=新的JLabel(“periodium:+shape.periodium());
数据。添加(周长);
返回gui;
}
我一定是把
BoxLayout
设置搞砸了,或者BoxLayout
可以使用BoxLayout
不包含其他JPanel
?您从未将数据
面板添加到gui
中
另外,请确保您正在调用
super.paintComponent(g)
(您已经注释掉了该部分代码,因此我无法判断您是否正在执行此操作,但如果不执行此操作,可能会导致问题)别担心,我正在调用它。另外,lowerBar
是否应该在窗口底部显示为红色框?当我运行代码时,它不会。感谢您捕获数据
。另外,数据
是要进入下栏
。实际上,我只是在数据
修复的情况下运行了代码,它工作了。我仍然不明白为什么红色背景没有出现。它只显示在两个JLabel
s的上方和下方。BoxLayout
是否正在调整lowerBar
的大小,或者是其他什么?BoxLayout
按照您添加组件的顺序排列组件,因为您首先添加lowerBar
,它会首先出现