gpt4 book ai didi

java - 如何使用 BoxLayout 设置容器内的组件大小

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:27:46 25 4
gpt4 key购买 nike

我在使用 BoxLayout 时遇到问题。

在我的示例中,我尝试降低文本字段的高度并更改按钮的宽度(如底部图片中的绿色标记所示)。我知道 setPreferredSize()setMaximumSize() 技术,但它没有正常工作。 add(Box.createHorizo​​ntalGlue()) 行也没有帮助。

感谢您的任何想法。


public class Testy extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
constructGUI();
}
});
}

private static void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.DARK_GRAY);
centerPanel.setPreferredSize(new Dimension(100, 400));
frame.add(centerPanel, BorderLayout.CENTER);

Testy eastPanel = new Testy();
frame.add(eastPanel, BorderLayout.EAST);

frame.pack();
frame.setVisible(true);
}

public Testy() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);

button = new JButton("Button 2");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);

button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);

JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
//label.setMaximumSize(...);
add(label);

JTextField textField = new JTextField();
//textField.setPreferredSize(...);
//textField.setMaximumSize(...);
add(textField);

button = new JButton("Button 4");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);

//add(Box.createHorizontalGlue());
}
}

picture

最佳答案

首先,您必须认识到 Java Swing 中的组件位置和大小取决于布局管理器(如果设置了布局管理器)而不是组件本身。该组件向管理器请求大小。

对于这种情况,我会使用不同的布局 - GridLayout 和 BorderLayout 的组合就足够了,而且非常简单明了。但是如果想使用BoxLayout,那么...

  1. 文档说:

    BoxLayout pays attention to a component's requested minimum, preferred, and maximum sizes. While you are fine-tuning the layout, you might need to adjust these sizes. ... For example, a button's maximum size is generally the same as its preferred size. If you want the button to be drawn wider when additional space is available, then you need to change its maximum size.

  2. 然后设置组件的最大尺寸:c.setMaximumSize(new Dimension(Integer.MAX_VALUE, c.getMinimumSize().height));(c 表示buttonlabeltextField 在你的例子中)

编辑 1:

这是工作源代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

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;
import javax.swing.WindowConstants;

public class Testy extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
constructGUI();
}
});
}

private static void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.DARK_GRAY);
centerPanel.setPreferredSize(new Dimension(100, 400));
frame.add(centerPanel, BorderLayout.CENTER);

Testy eastPanel = new Testy();
frame.add(eastPanel, BorderLayout.EAST);

frame.pack();
frame.setVisible(true);
}

public Testy() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);

button = new JButton("Button 2");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);

button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);

JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
label.setMaximumSize(new Dimension(Integer.MAX_VALUE, label.getMinimumSize().height));
add(label);

JTextField textField = new JTextField();
//textField.setPreferredSize(...);
textField.setMaximumSize(new Dimension(Integer.MAX_VALUE, textField.getMinimumSize().height));
add(textField);

button = new JButton("Button 4");
//button.setPreferredSize(...);
button.setMaximumSize(new Dimension(Integer.MAX_VALUE, button.getMinimumSize().height));
add(button);

// add(Box.createVerticalGlue());
}
}

Screenshot

编辑 2:

如果你想在右列底部布置按钮 4,请在 add(textField);button = new JButton("Button 4");.

关于java - 如何使用 BoxLayout 设置容器内的组件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18405660/

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