繁体   English   中英

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

[英]How to Layout these components on JPanel?

我制作了一个小型的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);
    }

}

组件都彼此相邻出现,这不是我期望的那样,但是无论我尝试哪种布局(除非我做错了),它都不会改变。

我是否必须重写某些内容?

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

您可以根据需要实现的方式使用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);
    }

}

有关更多详细信息,请参见在容器布置组件

我也劝阻您不要直接从JFrame进行扩展,而是从JPanel进行扩展,这将使您的代码脱钩并提供更好的可重用性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM