Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JPanel上布局这些组件?_Java_Swing_Layout Manager - Fatal编程技术网

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

Java 如何在JPanel上布局这些组件?,java,swing,layout-manager,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(); } } i

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

代码如下:

Main.java

public class Main {

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

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

}
Client.java

public class Main {

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

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

}
所有组件都彼此相邻,这并不是我所期望的,但无论我尝试了什么布局(除非我做错了),它都不会改变


我是否需要重写某些内容?

您可以使用
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
}

您可以使用
GridBagLayout
GridBagLayout
,具体取决于您想要实现的功能

请查看以了解更多详细信息


我也不鼓励您直接从
JFrame
进行扩展,但是相反,从
JPanel
开始扩展,它将使您的代码解耦,并提供更好的可重用性,除此之外,您希望的设计是什么?将它们堆叠在一起:)谢谢您的回复!