Java - 如何使用 BoxLayout 更改组件高度?
Java - How to change components height using BoxLayout?
所以,我有一个小型 GUI 程序,我决定使用 BoxLayout 从上到下显示组件。一切正常,但我无法更改 JButton 的高度。我尝试了很多东西,比如 setPreferredSize() 但后来我也遇到了宽度不正确的问题。使用 setMaximumSize() 设置我想要的宽度,但高度仍然没有改变。也许你们中的一些人可以帮助我 :) 谢谢
public class SimpleSkinViewer extends JPanel implements ActionListener{
private final Dimension boxDimension = new Dimension(320, 320);
private final Dimension buttonDimension = new Dimension(320, 60);
private final Dimension spaceDimension = new Dimension(0, 5);
private JLabel imagebox;
private JButton loadButton;
private JButton changeButton;
private JButton downloadButton;
public SimpleSkinViewer() {
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
imagebox = new JLabel("");
imagebox.setIcon(new ImageIcon(loadImage("http://skins.minecraft.net/MinecraftSkins/AvarionDE.png")));
loadButton = new JButton("Load Skin");
changeButton = new JButton("Change Skin");
downloadButton = new JButton("Download");
//add listeners
loadButton.addActionListener(this);
changeButton.addActionListener(this);
downloadButton.addActionListener(this);
//dimensions
imagebox.setMaximumSize(boxDimension);
loadButton.setMaximumSize(buttonDimension);
changeButton.setMaximumSize(buttonDimension);
downloadButton.setMaximumSize(buttonDimension);
add(imagebox);
add(Box.createRigidArea(spaceDimension));
add(loadButton);
add(Box.createRigidArea(spaceDimension));
add(changeButton);
add(Box.createRigidArea(spaceDimension));
add(downloadButton);
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
//and other stuff.....
public static void main (String[] args) {
JFrame frame = new JFrame("Avarion's Simple Skin Viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SimpleSkinViewer());
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
你需要Box.createVerticalGlue()
改变
add(changeButton);
add(Box.createRigidArea(spaceDimension));
和
add(changeButton);
add(Box.createVerticalGlue());
然后您可以使用 .setPreferredSize(new Dimension(x,y));
,按钮将适应您的布局
When a BoxLayout lays out components from top to bottom, it tries to
size each component at the component's preferred height.
For a top-to-bottom box layout, the preferred width of the container
is that of the maximum preferred width of the children. If the
container is forced to be wider than that, BoxLayout attempts to size
the width of each component to that of the container's width (minus
insets). If the maximum size of a component is smaller than the width
of the container, then X alignment comes into play.
因此,您可以同时设置 maximumSize
和 preferredSize
以获得所需的大小。
loadButton.setMaximumSize(buttonDimension);
loadButton.setPreferredSize(buttonDimension);
所以,我有一个小型 GUI 程序,我决定使用 BoxLayout 从上到下显示组件。一切正常,但我无法更改 JButton 的高度。我尝试了很多东西,比如 setPreferredSize() 但后来我也遇到了宽度不正确的问题。使用 setMaximumSize() 设置我想要的宽度,但高度仍然没有改变。也许你们中的一些人可以帮助我 :) 谢谢
public class SimpleSkinViewer extends JPanel implements ActionListener{
private final Dimension boxDimension = new Dimension(320, 320);
private final Dimension buttonDimension = new Dimension(320, 60);
private final Dimension spaceDimension = new Dimension(0, 5);
private JLabel imagebox;
private JButton loadButton;
private JButton changeButton;
private JButton downloadButton;
public SimpleSkinViewer() {
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
imagebox = new JLabel("");
imagebox.setIcon(new ImageIcon(loadImage("http://skins.minecraft.net/MinecraftSkins/AvarionDE.png")));
loadButton = new JButton("Load Skin");
changeButton = new JButton("Change Skin");
downloadButton = new JButton("Download");
//add listeners
loadButton.addActionListener(this);
changeButton.addActionListener(this);
downloadButton.addActionListener(this);
//dimensions
imagebox.setMaximumSize(boxDimension);
loadButton.setMaximumSize(buttonDimension);
changeButton.setMaximumSize(buttonDimension);
downloadButton.setMaximumSize(buttonDimension);
add(imagebox);
add(Box.createRigidArea(spaceDimension));
add(loadButton);
add(Box.createRigidArea(spaceDimension));
add(changeButton);
add(Box.createRigidArea(spaceDimension));
add(downloadButton);
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
//and other stuff.....
public static void main (String[] args) {
JFrame frame = new JFrame("Avarion's Simple Skin Viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SimpleSkinViewer());
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
你需要Box.createVerticalGlue()
改变
add(changeButton);
add(Box.createRigidArea(spaceDimension));
和
add(changeButton);
add(Box.createVerticalGlue());
然后您可以使用 .setPreferredSize(new Dimension(x,y));
,按钮将适应您的布局
When a BoxLayout lays out components from top to bottom, it tries to size each component at the component's preferred height.
For a top-to-bottom box layout, the preferred width of the container is that of the maximum preferred width of the children. If the container is forced to be wider than that, BoxLayout attempts to size the width of each component to that of the container's width (minus insets). If the maximum size of a component is smaller than the width of the container, then X alignment comes into play.
因此,您可以同时设置 maximumSize
和 preferredSize
以获得所需的大小。
loadButton.setMaximumSize(buttonDimension);
loadButton.setPreferredSize(buttonDimension);