Java 未排列Swing Box布局组件
当我使用Java 未排列Swing Box布局组件,java,swing,layout-manager,boxlayout,Java,Swing,Layout Manager,Boxlayout,当我使用FlowLayout时,组件达到限制时会移动到新行。我想要完全一样的,但是垂直的。做了一点研究,我发现我想做的应该是使用BoxLayout来完成。但是我的框布局只使用一列 我希望第二个面板检测到其空间不足以显示主面板中的所有组件,并将其放置在第二列中 下面是重现问题的代码 import javax.swing.*; public class TestingMain { static JFrame frame; public static JPanel create
FlowLayout
时,组件达到限制时会移动到新行。我想要完全一样的,但是垂直的。做了一点研究,我发现我想做的应该是使用BoxLayout
来完成。但是我的框布局只使用一列
我希望第二个面板检测到其空间不足以显示主面板中的所有组件,并将其放置在第二列中
下面是重现问题的代码
import javax.swing.*;
public class TestingMain {
static JFrame frame;
public static JPanel createUI() {
JPanel UIPanel = new JPanel();
UIPanel.setLayout(new BoxLayout(UIPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel();
JPanel secondPanel = new JPanel();
firstPanel = new JPanel();
firstPanel.setLayout(new BoxLayout(firstPanel, BoxLayout.Y_AXIS));
firstPanel.setBorder(BorderFactory.createTitledBorder("Box 1"));
JCheckBox ch1 = new JCheckBox("checkbox!");
JCheckBox ch2 = new JCheckBox("checkbox!");
JCheckBox ch3 = new JCheckBox("checkbox!");
JCheckBox ch4 = new JCheckBox("checkbox!");
JCheckBox ch5 = new JCheckBox("checkbox!");
JCheckBox ch6 = new JCheckBox("checkbox!");
JCheckBox ch7 = new JCheckBox("checkbox!");
firstPanel.add(ch1);
firstPanel.add(ch2);
firstPanel.add(ch3);
firstPanel.add(ch4);
firstPanel.add(ch5);
firstPanel.add(ch6);
firstPanel.add(ch7);
secondPanel.setBorder(BorderFactory.createTitledBorder("Box 2"));
secondPanel.setLayout(new BoxLayout(secondPanel, BoxLayout.Y_AXIS));
JCheckBox alarmLimitedBatt = new JCheckBox("checkbox!");
JCheckBox alarmNotEnoughBatt = new JCheckBox("checkbox!");
JCheckBox alarmLowBatt = new JCheckBox("checkbox!");
JCheckBox alarmCriticalBatt = new JCheckBox("checkbox!");
JCheckBox stateOfCharge = new JCheckBox("checkbox!");
JCheckBox sleepState = new JCheckBox("checkbox!");
secondPanel.add(alarmLimitedBatt);
secondPanel.add(alarmNotEnoughBatt);
secondPanel.add(alarmLowBatt);
secondPanel.add(alarmCriticalBatt);
secondPanel.add(stateOfCharge);
secondPanel.add(sleepState);
UIPanel.add(firstPanel);
UIPanel.add(secondPanel);
return UIPanel;
}
private static void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("BoxLayout Issue");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JPanel newContentPane = createUI();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.setSize(800,250);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
JDK中没有这样做的布局管理器。您可以尝试使用
VericalFlowLayout
,这是一种基于FlowLayout的定制。请参阅:如果您只想让secondPanel
位于firstPanel
的右侧,请使用UIPanel.setLayout(新的BoxLayout(UIPanel,BoxLayout.X_轴))代码>旁注:嘿,我爱你@camickr。非常感谢:-)JDK中没有这样的布局管理器。您可以尝试使用VericalFlowLayout
,这是一种基于FlowLayout的定制。请参阅:如果您只想让secondPanel
位于firstPanel
的右侧,请使用UIPanel.setLayout(新的BoxLayout(UIPanel,BoxLayout.X_轴))代码>旁注:嘿,我爱你@camickr。非常感谢:-)