gpt4 book ai didi

java GUI布局建议

转载 作者:行者123 更新时间:2023-12-02 02:19:52 35 4
gpt4 key购买 nike

public class AFS {
public JPanel afs(final Fields input){
JPanel titlePanel = new JPanel();

//Title fields
JLabel afs = new JLabel("Statement", Label.LEFT);

Label mm = new Label("month ", Label.LEFT);
Label my = new Label("Year ", Label.LEFT);

//first line
titlePanel.add(afs);
titlePanel.add(mm);
titlePanel.add(input.MENTRY);
titlePanel.add(my);
titlePanel.add(input.YENTRY);
titlePanel.setPreferredSize(null);

//Left Panels
JPanel sb = new JPanel();
JPanel entry = new JPanel();
entry.setLayout(new BoxLayout(entry, BoxLayout.Y_AXIS));
entry.setAlignmentX(Component.LEFT_ALIGNMENT);
entry.add(new Label("Service "));
entry.add(input.s);
entry.add(new Label("Amount "));
entry.add(input.a);
entry.add(new Label("Counter "));
entry.add(input.o);
entry.add(new Label("Division "));
entry.add(input.d);

sb.add(entry);


JPanel holderPanel = new JPanel();
holderPanel.setLayout(new BoxLayout(holderPanel, BoxLayout.Y_AXIS));
holderPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
holderPanel.add(titlePanel);
holderPanel.add(sb);

JButton start = new JButton("Save Current");
start.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
ScriptAction action = new ScriptAction();
action.saveAll(input,1);
}
});
holderPanel.add(start);
return holderPanel;
}

我有一个类似于上面代码的简短版本。当前的布局如下所示: enter image description here

但我希望布局看起来像(已编辑油漆)。

enter image description here

我尝试使用 gridLayout 进行交换,它将显示 2 行,但 gridlayout 仍会将所有内容对齐在中心(包括标题和页眉)。此外,按钮跨度将横跨整个底部。我想知道是否有任何建议的方法可以做到这一点?

最佳答案

您需要使用 layout managers 的组合达到所需的输出:

enter image description here enter image description here调整大小之前/调整大小之后

在本例中,有 3 个主要部分:

  • 顶部 Pane (使用 Box 来对齐左侧的一些文本和右侧的一些文本)
  • 中间 Pane (使用 GridBagLayout 来定位组件,如图所示,也许具有适当插入的 GridLayout 也可以工作)
  • 底部 Pane (使用默认的 JPanel 布局:FlowLayout)

顶部 Pane 也使用 2 个 JPanel,第一个单独用于标签 Statement,另一个与 FlowLayout 右对齐对于其他 4 个组件,按照 this answer BoxLayout 不尊重我们 JTextField 的首选大小。因此,解决方法是将它们包装在另一个 JPanel 中,然后将该 JPanelStatement 标签一起包装。

中间 Pane 也出现了类似的问题,它需要使用 2 个 JPanel:一个用于包含在另一个更大的 Pane 中的字段,该 Pane 容纳它,并且 JButton 位于底部(保存当前)。我们可以通过添加带有 gridx = 2JButton 和带有 countergridy = 2 来实现类似的输出和 gridx = 3gridx = 4 上的 division 标签和字段(而不是 2 & 3),但是然后,我们需要添加 gbc.insets 以将插图添加到具有高值的顶部和底部...这取决于您使用哪一个:)

产生上述输出的代码如下:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FormSample {
private JFrame frame;
private JPanel topRightPane;
private JPanel centerPane;
private JPanel centerWithButtonPane;
private JPanel buttonsPane;

private JTextField monthField;
private JTextField yearField;

private JTextField serviceField;
private JTextField amountField;
private JTextField counterField;
private JTextField divisionField;

private static final int LEFT_MARGIN = 50; //Increase / Decrease to add extra space between components
private static final int RIGHT_MARGIN = LEFT_MARGIN;

//Change insets accordingly to add extra space between components (top, left, bottom, right)
private static final Insets leftInsets = new Insets(0, LEFT_MARGIN, 0, 0);
private static final Insets rightInsets = new Insets(0, 0, 0, RIGHT_MARGIN);
private static final Insets defaultInsets = new Insets(0, 0, 0, 0);

private JButton saveCurrentButton;
private JButton saveAllButton;
private JButton resetButton;

public static void main(String[] args) {
SwingUtilities.invokeLater(new FormSample()::createAndShowGui);
}

private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());

monthField = new JTextField(10);
yearField = new JTextField(10);

serviceField = new JTextField(10);
amountField = new JTextField(10);
counterField = new JTextField(10);
divisionField = new JTextField(10);

saveCurrentButton = new JButton("Save Current");
saveAllButton = new JButton("Save all");
resetButton = new JButton("Reset");

buttonsPane = new JPanel();

topRightPane = new JPanel();
topRightPane.setLayout(new FlowLayout(FlowLayout.RIGHT));

topRightPane.add(new JLabel("Month"));
topRightPane.add(monthField);
topRightPane.add(new JLabel("Year"));
topRightPane.add(yearField);

centerWithButtonPane = new JPanel();
centerWithButtonPane.setLayout(new BoxLayout(centerWithButtonPane, BoxLayout.PAGE_AXIS));

Box box = Box.createHorizontalBox();
box.add(new JLabel("Statement"));
box.add(Box.createHorizontalGlue());
box.add(topRightPane);

centerPane = new JPanel();
centerPane.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = defaultInsets;
centerPane.add(new JLabel("Service"), gbc);

gbc.gridx = 1;
gbc.insets = rightInsets;
centerPane.add(serviceField, gbc);

gbc.gridx = 2;
gbc.insets = leftInsets;
centerPane.add(new JLabel("Counter"), gbc);

gbc.gridx = 3;
gbc.insets = defaultInsets;
centerPane.add(counterField, gbc);

gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = defaultInsets;
centerPane.add(new JLabel("Amount"), gbc);

gbc.gridx = 1;
gbc.insets = rightInsets;
centerPane.add(amountField, gbc);

gbc.gridx = 2;
gbc.insets = leftInsets;
centerPane.add(new JLabel("Division"), gbc);

gbc.gridx = 3;
gbc.insets = defaultInsets;
centerPane.add(divisionField, gbc);

saveCurrentButton.setAlignmentX(Component.CENTER_ALIGNMENT); //Force centered alignment for our JButton
centerWithButtonPane.add(centerPane);
centerWithButtonPane.add(saveCurrentButton);

buttonsPane.add(saveAllButton);
buttonsPane.add(resetButton);

frame.add(box, BorderLayout.NORTH);
frame.add(centerWithButtonPane, BorderLayout.CENTER);
frame.add(buttonsPane, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

另外,请遵循 @SergiyMedvynskyy 给出的关于不要混合使用 AWT 和 Swing 组件(即 JTextFieldTextField)的建议,并且仅使用 Swing 组件,因为 AWT 组件存在错误。

关于java GUI布局建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48666635/

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