限制 UI 大小的组件(不使用 setBounds() 方法)
Limit UI size components (not using setBounds() method)
目前,这是我的UI:
JTextField 和JComboBox 太大了,占了这么多space。
理想情况下,我希望这是我的 UI:
讽刺的是,我不知道为什么下面的"Start"按钮是常规大小,没有占用太多space。这就是我想要完成的。我正在使用 2 个面板,inputPanel 包含包含所有元素的 boxPanel。
这是我的代码:
public class Selection {
private JFrame mainFrame;
final CardLayout cardLayout = new CardLayout();
final JPanel mainPanel = new JPanel(cardLayout);
//inputPanel
final public ImageIcon goalPic = new ImageIcon(getClass().getResource("/images/goal.png"));
final public ImageIcon selEmployeePic = new ImageIcon(getClass().getResource("/images/selEmployee.png"));
JLabel goalPicL = new JLabel(goalPic);
JLabel selEmployeePicL = new JLabel(selEmployeePic);
JPanel inputPanel, boxPanel;
JLabel goalLbl = new JLabel("Input Goal"), selEmployeeLbl = new JLabel("Select Employee");
JTextField goal = new JTextField();
JComboBox selEmployeeCombo = new JComboBox();
JButton start = new JButton("Start");
public Selection(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
mainFrame = new JFrame("Input");
mainFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(1000, 750);
mainFrame.setLocationRelativeTo(null);
//input Panel
inputPanel = new JPanel();
inputPanel.setBackground(Color.white);
inputPanel.setLayout(new BorderLayout());
boxPanel = new JPanel();
boxPanel.setBackground(Color.white);
boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.Y_AXIS));
goalPicL.setToolTipText("Input Goal");
goalPicL.setAlignmentX(Component.CENTER_ALIGNMENT);
goalLbl.setFont(new Font("Calibri", Font.PLAIN, 20));
goalLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
goalTextField.setFont(new Font("Calibri", Font.PLAIN, 20));
goalTextField.setAlignmentX(Component.CENTER_ALIGNMENT);
employeeCombo = new JComboBox();
employeeCombo.setFont(new Font("Calibri", Font.PLAIN, 18));
employeeCombo.setSelectedIndex(-1);
employeePicL.setToolTipText("Select Employee");
emploeePicL.setAlignmentX(Component.CENTER_ALIGNMENT);
employeeLbl.setFont(new Font("Calibri", Font.PLAIN, 20));
employeeLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
employeeCombo.setAlignmentX(Component.CENTER_ALIGNMENT);
start.setFont(new Font("Calibri", Font.PLAIN, 20));
start.setAlignmentX(Component.CENTER_ALIGNMENT);
boxPanel.add(goalPicL);
boxPanel.add(goalLbl);
boxPanel.add(goal);
boxPanel.add(employeePicL);
boxPanel.add(employeeLbl);
boxPanel.add(employeeCombo);
boxPanel.add(start);
inputPanel.add(boxPanel, BorderLayout.CENTER);
mainPanel.add(inputPanel, "Input");
mainFrame.add(mainPanel);
}
/**
* @return main frame of Selection Page
*/
public JFrame getMainFrame() {
return mainFrame;
}
}
主要 Class :
public class main {
public static void main (String[] asdf){
Selection sel = new Selection();
sel.getMainFrame().setVisible(true);
}
}
编辑:不是 setBounds() 方法,因为它不起作用
我更改了如下代码。
goal.setMaximumSize(new Dimension(200, 25));
employeeCombo.setMaximumSize(new Dimension(200, 25));
而且效果很好。
目前,这是我的UI:
JTextField 和JComboBox 太大了,占了这么多space。
理想情况下,我希望这是我的 UI:
讽刺的是,我不知道为什么下面的"Start"按钮是常规大小,没有占用太多space。这就是我想要完成的。我正在使用 2 个面板,inputPanel 包含包含所有元素的 boxPanel。
这是我的代码:
public class Selection {
private JFrame mainFrame;
final CardLayout cardLayout = new CardLayout();
final JPanel mainPanel = new JPanel(cardLayout);
//inputPanel
final public ImageIcon goalPic = new ImageIcon(getClass().getResource("/images/goal.png"));
final public ImageIcon selEmployeePic = new ImageIcon(getClass().getResource("/images/selEmployee.png"));
JLabel goalPicL = new JLabel(goalPic);
JLabel selEmployeePicL = new JLabel(selEmployeePic);
JPanel inputPanel, boxPanel;
JLabel goalLbl = new JLabel("Input Goal"), selEmployeeLbl = new JLabel("Select Employee");
JTextField goal = new JTextField();
JComboBox selEmployeeCombo = new JComboBox();
JButton start = new JButton("Start");
public Selection(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
mainFrame = new JFrame("Input");
mainFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(1000, 750);
mainFrame.setLocationRelativeTo(null);
//input Panel
inputPanel = new JPanel();
inputPanel.setBackground(Color.white);
inputPanel.setLayout(new BorderLayout());
boxPanel = new JPanel();
boxPanel.setBackground(Color.white);
boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.Y_AXIS));
goalPicL.setToolTipText("Input Goal");
goalPicL.setAlignmentX(Component.CENTER_ALIGNMENT);
goalLbl.setFont(new Font("Calibri", Font.PLAIN, 20));
goalLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
goalTextField.setFont(new Font("Calibri", Font.PLAIN, 20));
goalTextField.setAlignmentX(Component.CENTER_ALIGNMENT);
employeeCombo = new JComboBox();
employeeCombo.setFont(new Font("Calibri", Font.PLAIN, 18));
employeeCombo.setSelectedIndex(-1);
employeePicL.setToolTipText("Select Employee");
emploeePicL.setAlignmentX(Component.CENTER_ALIGNMENT);
employeeLbl.setFont(new Font("Calibri", Font.PLAIN, 20));
employeeLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
employeeCombo.setAlignmentX(Component.CENTER_ALIGNMENT);
start.setFont(new Font("Calibri", Font.PLAIN, 20));
start.setAlignmentX(Component.CENTER_ALIGNMENT);
boxPanel.add(goalPicL);
boxPanel.add(goalLbl);
boxPanel.add(goal);
boxPanel.add(employeePicL);
boxPanel.add(employeeLbl);
boxPanel.add(employeeCombo);
boxPanel.add(start);
inputPanel.add(boxPanel, BorderLayout.CENTER);
mainPanel.add(inputPanel, "Input");
mainFrame.add(mainPanel);
}
/**
* @return main frame of Selection Page
*/
public JFrame getMainFrame() {
return mainFrame;
}
}
主要 Class :
public class main {
public static void main (String[] asdf){
Selection sel = new Selection();
sel.getMainFrame().setVisible(true);
}
}
编辑:不是 setBounds() 方法,因为它不起作用
我更改了如下代码。
goal.setMaximumSize(new Dimension(200, 25));
employeeCombo.setMaximumSize(new Dimension(200, 25));
而且效果很好。