我想创建 2 个嵌套边框布局,其中内部边框布局将面板保持在其中心。该面板应具有垂直流动,特定组件应增长并一起使用所有可用空间。最后这个面板应该被包裹起来JScrollPane
,所以当不是所有的东西都可以以默认大小显示时,它会得到一个垂直滚动条。类似于以下内容:
+-------------------------------+
| |
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | | |
| | growy | |
| | | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | | |
| | growy | |
| | | |
| +-----------------------+ |
| |
+-------------------------------+
这是我走了多远:
提供以下SSCCE:
public class SimpleMain {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//createAndShowMigGUI();
createAndShowSwingGUI();
}
});
}
private static void createAndShowSwingGUI() {
JFrame frame = new JFrame("Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Frame */
frame.setLayout(new BorderLayout());
/* Outer Panel */
JPanel outerPanel = new JPanel(new BorderLayout());
outerPanel.add(new JTextField("inner north"), BorderLayout.NORTH);
outerPanel.add(new JTextField("inner south"), BorderLayout.SOUTH);
outerPanel.add(new JTextField("inner west"), BorderLayout.WEST);
outerPanel.add(new JTextField("inner east"), BorderLayout.EAST);
/* Text Area */
JTextArea area = new JTextArea("aHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd AskjdhuyawduyawdHJAskjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd Askjdhuyawduyawd");
area.setLineWrap(true);
area.setWrapStyleWord(true);
/* Elements Container */
JPanel innerPanel = new JPanel(new GridBagLayout());
/* Grid Bag Constraints */
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.HORIZONTAL;
cons.weightx = 1;
cons.gridx = 0;
/* Elements */
innerPanel.add(area, cons); // This component should use all available Y-space
innerPanel.add(new JRadioButton("Test hello"), cons); // Remain as-is
innerPanel.add(new JTextField("some text field..."), cons); // Remain as-is
innerPanel.add(new JTextField("some text field PUSH..."), cons); // This component should use all available Y-space
/* Add Inner Panel To Outer Panel */
outerPanel.add(new JScrollPane(innerPanel), BorderLayout.CENTER);
/* Add outer elements */
frame.getContentPane().add(new JTextField("outer north"), BorderLayout.NORTH);
frame.getContentPane().add(new JTextField("outer south"), BorderLayout.SOUTH);
frame.getContentPane().add(new JTextField("outer west"), BorderLayout.WEST);
frame.getContentPane().add(new JTextField("outer east"), BorderLayout.EAST);
frame.getContentPane().add(outerPanel, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setVisible(true);
frame.setLocation(40, 40);
}
}