gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:10:14 27 4
gpt4 key购买 nike

这是我真正想放在面板上的内容:

第一个逻辑 block :

radio button 1       text field     icon button

radio button 2       text field     icon button

check box

第二个逻辑 block :

Label       Spinner

        Button

我的第一个决定是制作垂直盒布局,并为每个逻辑 block 放置两个水平盒布局。但问题是这些 block ,选择什么样的布局来描述这个结构?我不喜欢 GridBagLayout - 它非常复杂且难以理解,尤其是当代码不是您自己的时候。目前我看到可以使用 Flow Layout 和 Grid Layout。但是网格布局,例如,将按钮拉伸(stretch)到一个单元格的宽度,如果一个按钮只有它的图标,那么它看起来很奇怪。

希望你能给我一些建议。

最佳答案

对于第一种情况,您可以在 JPanel 上使用一个简单的 GridLayout,其中 3 行 每行都有一个单独的 JPanelFlowLayout 有约束,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();
}
});
}
}

输出:

FIRST BLOCK

对于第二种情况,只需将前两个组件添加到具有默认布局的 JPanel。对于第三个组件,只需将组件添加到具有 GridBagLayoutJPanel 上,没有任何限制。

编辑#1:

或者您可以将这种方法用于您的第二个 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();
}
});
}
}

关于java - 为应用程序选择什么样的 swing 布局结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14603145/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com