2016-06-20 11 views
1

Derzeit ist dies meine UI: Die JTextField und JComboBox sind zu groß, so viel Platz einnehmen. enter image description hereLimit UI Größe Komponenten (nicht mit SetBounds() -Methode)

Idealerweise würde ich dies gerne meine UI sein: Ideal

Ironischer, ich weiß nicht, warum die Schaltfläche „Start“ unten reguläre Größe ist, nicht zu viel Platz wegnimmt. Das möchte ich erreichen. Ich verwende 2 Panels, inputPanel enthält das BoxPanel, das alle Elemente enthält.

ist hier mein Code:

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; 
} 

}

Für Hauptklasse:

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

}

EDIT: Nicht setBounds() Methode, weil es nicht funktioniert

+1

Können Sie einen funktionierenden Code schreiben? Ich kann mit der Lösung helfen. – Beniton

+0

Bearbeiten von gegebenem Code zu einem Arbeitscode. Vielen Dank! – beyonchayyy

+0

@Beniton, hoffe, dass der oben editierte Code hilfreich ist. Ich weiß nicht, wie ich die Bilder schicken kann, aber hoffe das ist gut :) – beyonchayyy

Antwort

1

Ich habe den Code wie folgt geändert.

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

Und es funktioniert gut.

enter image description here

+0

Halleluja für Leute wie dich! – beyonchayyy