Java Swing BoxLayout:在多面板设置中调整面板之间的 space?
Java Swing BoxLayout: Adjusting space between panels in a multi-panel setup?
我正在编写这个 swing 应用程序,我正在使用 BoxLayout 格式的多个面板,但似乎面板之间的空白 space 在它们之间被分割,看起来真的丑陋的。您如何调整和自定义面板之间放置多少 space?抱歉,如果这是 repost;我找不到更旧的 post.
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
class gui extends JFrame implements ActionListener{
JFrame frame = new JFrame("Ark Admin Spawn Commands");
JPanel panel = new JPanel();
JComboBox dinos;
JComboBox corruptedDinos;
public JSlider dinoLevel;
JLabel input;
JLabel dino;
JLabel title;
gui(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();
JPanel panelThree = new JPanel();
mainPanel.add(panelOne);
mainPanel.add(panelTwo);
mainPanel.add(panelThree);
title = new JLabel("Spawning Dinos");
String[] dinoNames= {"The Island:"};
String[] corruptedDinoNames = {"Extinction:"};
dinos = new JComboBox(dinoNames);
corruptedDinos = new JComboBox(corruptedDinoNames);
dinoLevel = new JSlider(JSlider.HORIZONTAL, 1, 600, 400);
dinoLevel.setMajorTickSpacing(20);
dinoLevel.setPaintTicks(true);
input = new JLabel("Select Level: ");
event e = new event();
dinoLevel.addChangeListener(e);
dinos.addActionListener(this);
corruptedDinos.addActionListener(this);
panelOne.add(title);
panelTwo.add(input);
panelTwo.add(dinoLevel);
panelThree.add(dinos);
panelThree.add(corruptedDinos);
this.getContentPane().add(mainPanel);
this.setTitle("Ark Admin Spawn Commands");
this.setSize(600, 600);
this.setVisible(true);
}
public static void main (String args[]) {
new gui();
}
public class event implements ChangeListener{
@Override
public void stateChanged(ChangeEvent e) {
int value = dinoLevel.getValue();
input.setText("Level: " + value);
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
but it seems to be that the empty space between the panels is being divided up between them
正确。 BoxLayout
将尝试为所有组件分配额外的 space。
但是,它会遵守每个面板的最大尺寸。
因此,为了防止面板高度增加,您可以使用如下代码:
JPanel panelOne = new JPanel()
{
@Override
public dimension getMaximumSize()
{
Dimension d = getPreferredSize()
d.width = Integer.MAX_VALUE;
return d;
}
};
或者因为框架的默认布局管理器是BorderLayout
,你可以只使用:
//this.getContentPane().add(mainPanel);
add(mainPanel, BorderLayout.PAGE_START); // getContentPane() is not needed
BorderLayout 的 PAGE_START 尊重组件的首选高度。
我正在编写这个 swing 应用程序,我正在使用 BoxLayout 格式的多个面板,但似乎面板之间的空白 space 在它们之间被分割,看起来真的丑陋的。您如何调整和自定义面板之间放置多少 space?抱歉,如果这是 repost;我找不到更旧的 post.
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
class gui extends JFrame implements ActionListener{
JFrame frame = new JFrame("Ark Admin Spawn Commands");
JPanel panel = new JPanel();
JComboBox dinos;
JComboBox corruptedDinos;
public JSlider dinoLevel;
JLabel input;
JLabel dino;
JLabel title;
gui(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();
JPanel panelThree = new JPanel();
mainPanel.add(panelOne);
mainPanel.add(panelTwo);
mainPanel.add(panelThree);
title = new JLabel("Spawning Dinos");
String[] dinoNames= {"The Island:"};
String[] corruptedDinoNames = {"Extinction:"};
dinos = new JComboBox(dinoNames);
corruptedDinos = new JComboBox(corruptedDinoNames);
dinoLevel = new JSlider(JSlider.HORIZONTAL, 1, 600, 400);
dinoLevel.setMajorTickSpacing(20);
dinoLevel.setPaintTicks(true);
input = new JLabel("Select Level: ");
event e = new event();
dinoLevel.addChangeListener(e);
dinos.addActionListener(this);
corruptedDinos.addActionListener(this);
panelOne.add(title);
panelTwo.add(input);
panelTwo.add(dinoLevel);
panelThree.add(dinos);
panelThree.add(corruptedDinos);
this.getContentPane().add(mainPanel);
this.setTitle("Ark Admin Spawn Commands");
this.setSize(600, 600);
this.setVisible(true);
}
public static void main (String args[]) {
new gui();
}
public class event implements ChangeListener{
@Override
public void stateChanged(ChangeEvent e) {
int value = dinoLevel.getValue();
input.setText("Level: " + value);
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
but it seems to be that the empty space between the panels is being divided up between them
正确。 BoxLayout
将尝试为所有组件分配额外的 space。
但是,它会遵守每个面板的最大尺寸。
因此,为了防止面板高度增加,您可以使用如下代码:
JPanel panelOne = new JPanel()
{
@Override
public dimension getMaximumSize()
{
Dimension d = getPreferredSize()
d.width = Integer.MAX_VALUE;
return d;
}
};
或者因为框架的默认布局管理器是BorderLayout
,你可以只使用:
//this.getContentPane().add(mainPanel);
add(mainPanel, BorderLayout.PAGE_START); // getContentPane() is not needed
BorderLayout 的 PAGE_START 尊重组件的首选高度。