Faced with the problem of using BoxLayout
面对使用BoxLayout的问题
In my example, I try to decrease the height of the text field and change the width of the buttons (as shown in green marker in the picture). I know about the techniques setPrefferedSize () and setMaximumSize (), but it did not work as it should. The line add(Box.createHorizontalGlue ()) also did not help.
在我的示例中,我尝试降低文本字段的高度并更改按钮的宽度(如图中的绿色标记所示)。我知道setPrefferedSize()和setMaximumSize()技术,但它没有按预期工作。行添加(Box.createHorizontalGlue())也没有帮助。
Thanks for any idea
谢谢你的任何想法
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());
}
}
3 个解决方案
#1
23
First you have to realize that component position and size in Java Swing depends on Layout manager (if layout manager is set) not on the component itself. The component requests the manager for size.
首先,您必须意识到Java Swing中的组件位置和大小取决于布局管理器(如果设置了布局管理器),而不是组件本身。该组件向经理请求大小。
For this case I would use different layout - combination of GridLayout and BorderLayout is enough and very simple and straightforward. But if want use BoxLayout, then...
对于这种情况,我会使用不同的布局 - GridLayout和BorderLayout的组合足够且非常简单和直接。但如果想使用BoxLayout,那么......
-
Documentation says:
文件说:
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.
BoxLayout关注组件的请求最小,首选和最大大小。在对布局进行微调时,可能需要调整这些尺寸。 ...例如,按钮的最大尺寸通常与其首选尺寸相同。如果您希望在有额外空间时将按钮绘制得更宽,则需要更改其最大尺寸。
-
Then set components maximum size:
c.setMaximumSize(new Dimension(Integer.MAX_VALUE, c.getMinimumSize().height));
(c
meansbutton
,label
andtextField
in your example)然后设置组件最大大小:c.setMaximumSize(new Dimension(Integer.MAX_VALUE,c.getMinimumSize()。height)); (c表示示例中的按钮,标签和textField)
Edit 1:
编辑1:
Here is working source code:
这是工作源代码:
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());
}
}
Edit 2:
编辑2:
If you want laid out Button 4 at the bottom of right column add this line add(Box.createVerticalGlue());
between add(textField);
and button = new JButton("Button 4");
.
如果你想在右栏的底部布置按钮4,请添加此行add(Box.createVerticalGlue()); add(textField)之间;和button = new JButton(“Button 4”);.
#2
3
As a quick remedy, you can use nested layouts, in the sense, that on the right side, create a JPanel
with BorderLayout
, put a JPanel(say compPanel)
at the CENTER
and a JPanel(say buttonPanel)
at PAGE_END
location. Now use a new JPanel(say panel)
with GridLayout
and put all the components on it, and place this compPanel
inside centerPanel
. Place JButton(button4)
inside buttonPanel
as is.
作为一种快速补救措施,您可以使用嵌套布局,在右侧,使用BorderLayout创建JPanel,在CENTER放置JPanel(比如compPanel),在PAGE_END位置放置JPanel(比如buttonPanel)。现在使用带有GridLayout的新JPanel(比较面板)并将所有组件放在其上,并将此compPanel放在centerPanel中。将JButton(button4)放在buttonPanel中。
BoxLayout
on the contrary, respects the preferred size of a given JComponent
, which is usually calculated based on the content the JComponent
holds or given explicity, hence components do not tend to align well with respect to other given components.
相反,BoxLayout尊重给定JComponent的首选大小,该JComponent通常基于JComponent保持或明确的内容计算,因此组件不倾向于与其他给定组件良好对齐。
Here is the working example :
这是工作示例:
import java.awt.*;
import javax.swing.*;
public class Testy extends JPanel {
private JPanel panel;
private JPanel buttonPanel;
public Testy() {
setLayout(new BorderLayout(5, 5));
JPanel compPanel = new JPanel();
panel = new JPanel(new GridLayout(6, 1, 5, 5));
JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
button = new JButton("Button 2");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
//label.setMaximumSize(...);
panel.add(label);
JTextField textField = new JTextField();
//textField.setPreferredSize(...);
//textField.setMaximumSize(...);
panel.add(textField);
compPanel.add(panel);
buttonPanel = new JPanel();
button = new JButton("Button 4");
buttonPanel.add(button);
add(compPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
}
private void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel centerPanel = new JPanel();
frame.getContentPane().setLayout(new BorderLayout(5, 5));
centerPanel.setBackground(Color.DARK_GRAY);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(this, BorderLayout.LINE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Testy().constructGUI();
}
});
}
}
OUTPUT :
输出:
#3
1
This should get close, based on your draw, just need to work on that component below the JLabel (using setPreferredSize()):
这应该接近,根据您的绘制,只需要在JLabel下面处理该组件(使用setPreferredSize()):
JPanel main = new JPanel(new GridLayout(1, 2));
JPanel left = new JPanel();
//left.setPreferredSize(some size);
JPanel right = new JPanel(new GridLayout(6, 1));
//right.setPreferredSize(some size);
right.add(new JButton("Button 1"));
//...
right.add(new JButton("Button 4"));
main.add(left);
main.add(right);
#1
23
First you have to realize that component position and size in Java Swing depends on Layout manager (if layout manager is set) not on the component itself. The component requests the manager for size.
首先,您必须意识到Java Swing中的组件位置和大小取决于布局管理器(如果设置了布局管理器),而不是组件本身。该组件向经理请求大小。
For this case I would use different layout - combination of GridLayout and BorderLayout is enough and very simple and straightforward. But if want use BoxLayout, then...
对于这种情况,我会使用不同的布局 - GridLayout和BorderLayout的组合足够且非常简单和直接。但如果想使用BoxLayout,那么......
-
Documentation says:
文件说:
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.
BoxLayout关注组件的请求最小,首选和最大大小。在对布局进行微调时,可能需要调整这些尺寸。 ...例如,按钮的最大尺寸通常与其首选尺寸相同。如果您希望在有额外空间时将按钮绘制得更宽,则需要更改其最大尺寸。
-
Then set components maximum size:
c.setMaximumSize(new Dimension(Integer.MAX_VALUE, c.getMinimumSize().height));
(c
meansbutton
,label
andtextField
in your example)然后设置组件最大大小:c.setMaximumSize(new Dimension(Integer.MAX_VALUE,c.getMinimumSize()。height)); (c表示示例中的按钮,标签和textField)
Edit 1:
编辑1:
Here is working source code:
这是工作源代码:
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());
}
}
Edit 2:
编辑2:
If you want laid out Button 4 at the bottom of right column add this line add(Box.createVerticalGlue());
between add(textField);
and button = new JButton("Button 4");
.
如果你想在右栏的底部布置按钮4,请添加此行add(Box.createVerticalGlue()); add(textField)之间;和button = new JButton(“Button 4”);.
#2
3
As a quick remedy, you can use nested layouts, in the sense, that on the right side, create a JPanel
with BorderLayout
, put a JPanel(say compPanel)
at the CENTER
and a JPanel(say buttonPanel)
at PAGE_END
location. Now use a new JPanel(say panel)
with GridLayout
and put all the components on it, and place this compPanel
inside centerPanel
. Place JButton(button4)
inside buttonPanel
as is.
作为一种快速补救措施,您可以使用嵌套布局,在右侧,使用BorderLayout创建JPanel,在CENTER放置JPanel(比如compPanel),在PAGE_END位置放置JPanel(比如buttonPanel)。现在使用带有GridLayout的新JPanel(比较面板)并将所有组件放在其上,并将此compPanel放在centerPanel中。将JButton(button4)放在buttonPanel中。
BoxLayout
on the contrary, respects the preferred size of a given JComponent
, which is usually calculated based on the content the JComponent
holds or given explicity, hence components do not tend to align well with respect to other given components.
相反,BoxLayout尊重给定JComponent的首选大小,该JComponent通常基于JComponent保持或明确的内容计算,因此组件不倾向于与其他给定组件良好对齐。
Here is the working example :
这是工作示例:
import java.awt.*;
import javax.swing.*;
public class Testy extends JPanel {
private JPanel panel;
private JPanel buttonPanel;
public Testy() {
setLayout(new BorderLayout(5, 5));
JPanel compPanel = new JPanel();
panel = new JPanel(new GridLayout(6, 1, 5, 5));
JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
button = new JButton("Button 2");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
panel.add(button);
JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
//label.setMaximumSize(...);
panel.add(label);
JTextField textField = new JTextField();
//textField.setPreferredSize(...);
//textField.setMaximumSize(...);
panel.add(textField);
compPanel.add(panel);
buttonPanel = new JPanel();
button = new JButton("Button 4");
buttonPanel.add(button);
add(compPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
}
private void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel centerPanel = new JPanel();
frame.getContentPane().setLayout(new BorderLayout(5, 5));
centerPanel.setBackground(Color.DARK_GRAY);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(this, BorderLayout.LINE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Testy().constructGUI();
}
});
}
}
OUTPUT :
输出:
#3
1
This should get close, based on your draw, just need to work on that component below the JLabel (using setPreferredSize()):
这应该接近,根据您的绘制,只需要在JLabel下面处理该组件(使用setPreferredSize()):
JPanel main = new JPanel(new GridLayout(1, 2));
JPanel left = new JPanel();
//left.setPreferredSize(some size);
JPanel right = new JPanel(new GridLayout(6, 1));
//right.setPreferredSize(some size);
right.add(new JButton("Button 1"));
//...
right.add(new JButton("Button 4"));
main.add(left);
main.add(right);