限制UI大小组件(不使用setBounds()方法)

时间:2016-06-20 07:39:49

标签: java user-interface boxlayout

目前,这是我的用户界面: JTextField和JComboBox太大了,占用了太多空间。 enter image description here

理想情况下,我希望这是我的用户界面: Ideal

具有讽刺意味的是,我不知道为什么下面的“开始”按钮是常规尺寸,而不占用太多空间。这就是我想要完成的。我使用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;
}   

}

对于主类:

public class main {
public static void main (String[] asdf){
    Selection sel = new Selection();
    sel.getMainFrame().setVisible(true);
}    

}

编辑:不是setBounds()方法,因为它不起作用

1 个答案:

答案 0 :(得分:1)

我改变了下面的代码。

goal.setMaximumSize(new Dimension(200, 25));
employeeCombo.setMaximumSize(new Dimension(200, 25));

它运作正常。

enter image description here