在两个JPanel上设置组件

时间:2018-02-22 22:22:58

标签: java swing jpanel

我对理解Java布局有一点问题。我使用GridBagLayout将组件定位在两个单独的JPanel上,然后将面板添加到JFrame,如下所示:

    upperContainer.setLayout(new GridBagLayout());
    lowerContainer.setLayout(new GridBagLayout());
    getContentPane().add(upperContainer, BorderLayout.NORTH);       
    getContentPane().add(lowerContainer, BorderLayout.CENTER);

现在,该程序完全符合我的预期,但不是我想要的。它将ùpperContainer(及其所有组件)置于lowerContainer之上,但这两个组件的组件之间存在巨大差距。见下面的截图。我用箭头标出了间隙。我希望lowerContainer上的组件位置更高,以便看起来更好。 这是一段代码(仅适用于lowerContainer): 编辑:我在两个面板上都使用了.createEtchedBorder功能,因此可以更好地看到面板的边框。

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.EtchedBorder;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;

    public class ConvertAllSystem extends JFrame{

    private JLabel labelDistance, labelWeight; 
    private JTextArea distanceOutput, weightOutput;
    private JTextField distanceInput, weightInput;
    private JButton convert, convert1; 
    private JComboBox fromListDistance, toListDistance, fromListWeight, toListWeight; 

    private String[] convertFromDistance = {"From (distance)...", "Mile", "Knot", "Kilometer", "Centimeter", "Meter", "Inch", "Yard"}; 
    private String[] convertToDistance = {"To (distance)...", "Mile", "Knot", "Kilometer", "Centimeter", "Meter", "Inch", "Yard"}; 
    private String[] convertFromWeight = {"From (weight)...", "Pound", "Kilogram", "Celcius", "Fahrenheit", "Gallon", "Liter"}; 
    private String[] convertToWeight = {"To (weight)...", "Pound", "Kilogram", "Celcius", "Fahrenheit", "Gallon", "Liter"}; 

    private Font font;
    JSeparator lineBetween;

    //Initialize all GUI components 
    private void createAllComponents() {

    JPanel upperContainer = new JPanel();
    JPanel lowerContainer = new JPanel();
    upperContainer.setLayout(new GridBagLayout());
    lowerContainer.setLayout(new GridBagLayout());
    getContentPane().add(upperContainer, BorderLayout.NORTH);       
    getContentPane().add(lowerContainer, BorderLayout.CENTER);
    Border outputCenter;    

     GridBagConstraints c = new GridBagConstraints();

    font = new Font("Times New Roman", Font.PLAIN, 20); 
    labelDistance = new JLabel("Convert distance unit");
    labelDistance.setFont(font);
    labelWeight = new JLabel("Convert weight unit");
    labelWeight.setFont(font);      

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.anchor = GridBagConstraints.PAGE_START;
    c.insets = new Insets(10,0,10,0);
    upperContainer.add(labelDistance, c);

    fromListDistance = new JComboBox<String>(convertFromDistance);
    c.gridy = 1;
    c.weightx = 0.5;
    c.gridwidth = 1;
    c.ipady = 5;
    c.ipadx = 30;
    c.insets = new Insets(0,20,0,5);
    c.anchor = GridBagConstraints.LINE_START;
    upperContainer.add(fromListDistance, c);

    toListDistance = new JComboBox<String>(convertToDistance);
    c.gridx = 1;
    c.weightx = 1.0;
    c.insets = new Insets(0,0,0,0);
    upperContainer.add(toListDistance, c);

    distanceInput = new JTextField("Enter value here...");
    distanceInput.setPreferredSize(new Dimension(150,30));;
    distanceInput.setEditable(true);
    distanceInput.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
            distanceInput.setText("");}});
    c.gridx = 2;
    c.ipady = 1;
    c.ipadx = 5;
    c.weightx = 5.0;
    upperContainer.add(distanceInput, c);

    convert = new JButton("Convert");
    c.gridx = 3;
    c.gridy = 1;
    c.weightx = 0.5;
    c.ipady = 20;
    c.ipadx = 50;
    c.gridwidth = GridBagConstraints.REMAINDER;
    upperContainer.add(convert, c);

    distanceOutput = new JTextArea(5,30);
    distanceOutput.setEditable(false);
    distanceOutput.setText("                                            Output");
    distanceOutput.setFont(new Font("Serif", Font.BOLD, 15));
    outputCenter = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
    distanceOutput.setBorder(outputCenter);
    c.gridx = 0;
    c.gridy = 2;
    c.weightx = 0.5;
    c.ipady = 1;
    c.ipadx = 5;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.insets = new Insets(30,0,0,0);
    c.anchor = GridBagConstraints.CENTER;
    upperContainer.add(distanceOutput, c);

    c.gridx = 0;
    c.gridy = 0;
    c.weightx =0.5;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.anchor = GridBagConstraints.PAGE_START;
    c.insets = new Insets(0,0,0,0);
    lowerContainer.add(labelWeight, c);

    fromListWeight = new JComboBox<String>(convertFromWeight);
    c.gridy = 1;
    c.weightx = 1.5;
    c.gridwidth = 1;
    c.ipady = 5;
    c.ipadx = 30;
    c.insets = new Insets(0,20,0,5);
    c.anchor = GridBagConstraints.LINE_START;
    lowerContainer.add(fromListWeight, c);

    toListWeight = new JComboBox<String>(convertToWeight);
    c.gridx = 1;
    c.weightx = 1.0;
    c.insets = new Insets(0,0,0,0);
    c.gridwidth = 1;
    lowerContainer.add(toListWeight, c);

    weightInput = new JTextField("Enter value here...");
    weightInput.setPreferredSize(new Dimension(150,30));;
    weightInput.setEditable(true);
    weightInput.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
            weightInput.setText("");}});
    c.gridx = 2;
    c.ipady = 2;
    c.ipadx = 10;
    c.weightx = 5.0;
    lowerContainer.add(weightInput, c);

    convert1 = new JButton("Convert");
    c.gridx = 3;
    c.gridy = 1;
    c.weightx = 0.5;
    c.ipady = 20;
    c.ipadx = 50;
    c.insets = new Insets(0,0,0,0);
    c.gridwidth = GridBagConstraints.REMAINDER;
    lowerContainer.add(convert1, c);

    weightOutput = new JTextArea(5,30);
    weightOutput.setEditable(false);
    weightOutput.setText("                                            Output");
    weightOutput.setFont(new Font("Serif", Font.BOLD, 15));
    weightOutput.setBorder(outputCenter);
    c.gridx = 0;
    c.gridy = 5;
    c.weightx = 0.5;
    c.ipady = 1;
    c.ipadx = 5;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.insets = new Insets(30,30,0,0);
    c.anchor = GridBagConstraints.CENTER;
    lowerContainer.add(weightOutput, c);

}

//Constractor
public ConvertAllSystem() {
    createAllComponents();
    setDefaultLookAndFeelDecorated(true);
    setTitle("Converting App"); 
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(700, 700);
    setResizable(true); 
    setLocationRelativeTo(null); 
    setVisible(true);
}
public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch(Exception e) {
        e.printStackTrace();

    }
    SwingUtilities.invokeLater(() -> {
        new ConvertAllSystem(); 
    });

}

}

截图: enter image description here

1 个答案:

答案 0 :(得分:1)

我怀疑那里有你的问题:

    setSize(700, 700);

通过人工调整GUI的大小,您可以添加BorderLayout将填充空格的额外空间。

将其更改为:

    // setSize(700, 700);
    pack();

让布局和组件自行调整大小

另请注意,如果您要创建两个类似的JPanel,一个在另一个之上,请不要将它们放入使用BorderLayout的容器中,而是使用容器中的GridLayout(0, 1):< / p>

    JPanel upperContainer = new JPanel();
    JPanel lowerContainer = new JPanel();
    upperContainer.setLayout(new GridBagLayout());
    lowerContainer.setLayout(new GridBagLayout());
    // getContentPane().add(upperContainer, BorderLayout.NORTH);
    // getContentPane().add(lowerContainer, BorderLayout.CENTER);
    getContentPane().setLayout(new GridLayout(0, 1));
    getContentPane().add(upperContainer);
    getContentPane().add(lowerContainer);