Swing组件中,如要要让容器随着窗体发的变化而自适应反打或者缩小,可以将容器布局到Gridlayout中
代码如下:
JPanel panel_2 = new JPanel();
tabbedPane.addTab("tab2", null, panel_2, null);
panel_2.setLayout(new GridLayout(0, 2, 10, 0));
JPanel panel_3 = new JPanel();
panel_3.setBackground(Color.PINK);
panel_2.add(panel_3);
JPanel panel_4 = new JPanel();
panel_4.setBackground(Color.ORANGE);
panel_2.add(panel_4);
运行结果如下:
缩小的情况:
全屏的情况: