如何在JPanel上布局这些组件?

时间:2016-04-12 17:37:43

标签: java swing layout-manager

我制作了一个小型GUI应用程序,目前只有表示层。它构造了基本的GUI,(但还没有添加逻辑)。我在布置控件/组件(例如文本字段和按钮)时遇到了问题。

以下是代码:

Main.java

public class Main {

    public static void main(String[] args) {
    // Make a new Client (TempConverter application)
    Client client = new Client();
    }

}

Client.java

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Client extends JFrame{

    private JPanel panel;
    private JTextField inputTextBox;
    private JTextField outputTextBox;
    private JButton convertButton;

    public Client(){
        panel = new JPanel();
        inputTextBox = new JTextField(6);
        outputTextBox = new JTextField(6);
        convertButton = new JButton("Convert!");
        ConstructGUI();
    }

    private void ConstructGUI(){
        this.setTitle("Temerature Converter");
        this.setSize(300, 400);
        PanelLayout();
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void PanelLayout(){
        this.add(panel);
        panel.add(inputTextBox);
        panel.add(outputTextBox);
        panel.add(convertButton);
    }

}

所有组件都显示在彼此旁边,并不是我所期望的,但无论我尝试了什么布局(除非我做错了),它都不会改变。

我是否必须覆盖一些东西?

2 个答案:

答案 0 :(得分:1)

您可以使用BoxLayout将它们叠放在一起。

private void PanelLayout(){
    this.add(panel);

    //next three lines aligning the components horizontally
    inputTextBox.setAlignmentX(Component.CENTER_ALIGNMENT);
    outputTextBox.setAlignmentX(Component.CENTER_ALIGNMENT);
    convertButton.setAlignmentX(Component.CENTER_ALIGNMENT);
    //aligning horizontally end. If you don't want the align them horizontally just remove these three lines.

    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(Box.createVerticalGlue());//remove this line if you don't want to center them vertically
    panel.add(inputTextBox);
    panel.add(outputTextBox);
    panel.add(convertButton);
    panel.add(Box.createVerticalGlue());//remove this line if you don't want to center them vertically
}

答案 1 :(得分:1)

您可以使用GridBagLayoutGridLayout,具体取决于您希望实现的目标......

GridBagLayout

public class Client extends JFrame {

    private JPanel panel;
    private JTextField inputTextBox;
    private JTextField outputTextBox;
    private JButton convertButton;

    public Client() {
        panel = new JPanel(new GridBagLayout());
        inputTextBox = new JTextField(6);
        outputTextBox = new JTextField(6);
        convertButton = new JButton("Convert!");
        ConstructGUI();
    }

    private void ConstructGUI() {
        this.setTitle("Temerature Converter");
        PanelLayout();
    }

    private void PanelLayout() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        this.add(panel);
        panel.add(inputTextBox, gbc);
        panel.add(outputTextBox, gbc);
        panel.add(convertButton, gbc);
    }

}

有关详细信息,请查看Laying Out Components Within a Container

我也建议您不要直接从JFrame扩展,而是从JPanel扩展而来,它将解耦您的代码并提供更好的可重用性,以及其他方面