为应用程序选择什么样的swing布局结构?

时间:2013-01-30 11:40:25

标签: java swing layout layout-manager jcomponent

以下是我实际想要放在面板上的内容:

第一个逻辑块:

  

单选按钮1文本字段图标按钮

     

单选按钮2文本字段图标按钮

     

复选框

第二个逻辑块:

  

Label Spinner

     

按钮

我的第一个决定是制作垂直框布局并为每个逻辑块放置两个水平框布局。但问题在于这些块,选择哪种布局来描述这种结构?我不喜欢GridBagLayout - 它非常复杂且难以理解,特别是当代码不是你的时候。目前我看到可以使用Flow Layout和Grid Layout。但是,例如,网格布局将按钮拉伸到单元格的宽度,如果按钮仅带有图标,那么它看起来很奇怪。

希望你能告诉我一些事情。

1 个答案:

答案 0 :(得分:6)

对于第一种情况,您可以在GridLayout JPanel上使用简单3 RowsJPanel每个FlowLayout都有FLowLayout.LEFT import java.awt.*; import javax.swing.*; public class ExampleLayout { private void displayGUI() { JFrame frame = new JFrame("Example Layout"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setLayout(new GridLayout(0, 1, 5, 5)); JPanel topPanel = new JPanel(); topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); JRadioButton rbut1 = new JRadioButton("RadioButton 1", false); JTextField tfield1 = new JTextField(10); JButton button1 = new JButton("Button 1"); topPanel.add(rbut1); topPanel.add(tfield1); topPanel.add(button1); JPanel middlePanel = new JPanel(); middlePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); JRadioButton rbut2 = new JRadioButton("RadioButton 2", false); JTextField tfield2 = new JTextField(10); JButton button2 = new JButton("Button 2"); middlePanel.add(rbut2); middlePanel.add(tfield2); middlePanel.add(button2); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); JCheckBox cbox = new JCheckBox("CheckBox 1", false); bottomPanel.add(cbox); contentPane.add(topPanel); contentPane.add(middlePanel); contentPane.add(bottomPanel); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String... args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ExampleLayout().displayGUI(); } }); } } 有约束JPanel JPanel 1}}。看看这个代码示例:

GridBagLayout

输出:

FIRST BLOCK

对于第二种情况,只需将前两个组件添加到具有默认布局的import java.awt.*; import javax.swing.*; public class ExampleLayout { private void displayGUI() { JFrame frame = new JFrame("Example Layout"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout(5, 5)); JPanel basePanel = new JPanel(); basePanel.setLayout(new GridLayout(0, 1, 5, 5)); JPanel topPanel = new JPanel(); //topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); JLabel label1 = new JLabel("Label 1", JLabel.CENTER); JRadioButton rbut1 = new JRadioButton("RadioButton 1", false); topPanel.add(label1); topPanel.add(rbut1); JPanel middlePanel = new JPanel(); middlePanel.setLayout(new GridBagLayout()); JButton button1 = new JButton("Button 1"); middlePanel.add(button1); basePanel.add(topPanel); basePanel.add(middlePanel); contentPane.add(basePanel, BorderLayout.PAGE_START); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String... args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ExampleLayout().displayGUI(); } }); } } 即可。对于第三个组件,只需将组件添加到{{1}} {{1}},但没有约束。

编辑#1:

或者您可以使用此方法,用于第二个块

{{1}}