简体   繁体   English

Java Swing UI布局

[英]Java Swing UI Layout

I am creating a basic user interface in Swing and was hoping for some help. 我正在Swing中创建一个基本的用户界面,希望获得一些帮助。 Below is a screenshot of what I am trying to achieve: 以下是我要实现的屏幕截图:

在此处输入图片说明

My code currently is as follows: 我的代码当前如下:

package testui;

import java.awt.Container;
import javax.swing.*;

public class TestUI{

    private JTextField outputArea = new JTextField();
    private JTextField errorReportArea = new JTextField();

    private JPanel inputPanel = new JPanel();

    private JLabel nameLabel = new JLabel("Item Name");
    private JLabel numberLabel = new JLabel("Number of units (or Volume in L)");
    private JLabel priceLabel = new JLabel("Price per unit (Or L) in pence");

    private JTextField nameField = new JTextField(10);
    private JTextField numberField = new JTextField(10);
    private JTextField priceField = new JTextField(10);

    private JButton addVolumeButton = new JButton("Add by Volume");
    private JButton addNumberButton = new JButton("Add by number of units");         

    public TestUI() {
        JFrame frame = new JFrame("Fuel Station");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        outputArea.setEditable(false);
        errorReportArea.setEditable(false);

        inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));
        inputPanel.add(nameLabel);
        inputPanel.add(nameField);
        inputPanel.add(numberLabel);
        inputPanel.add(numberField);
        inputPanel.add(priceLabel);
        inputPanel.add(priceField);
        inputPanel.add(addVolumeButton);
        inputPanel.add(addNumberButton);

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        contentPane.add(outputArea);
        contentPane.add(errorReportArea);
        contentPane.add(inputPanel);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        TestUI test1 = new TestUI();
    }

}

Which looks like this: 看起来像这样:

在此处输入图片说明

So what I would like to do is set a specific size for the top two JTextFields, as the top one will contain multiple lines of text, and the one below will contain just one line of text. 所以我想为顶部的两个JTextField设置一个特定的大小,因为顶部的JTextField将包含多行文本,而下面的JTextField将仅包含一行文本。 I am unsure how to do this without using setSize, as I have been told it is bad coding practice to use this. 我不确定在不使用setSize的情况下如何执行此操作,因为有人告诉我,使用此功能是不好的编码做法。

I would also like to add some padding between the JLabels, JTextFields and JButtons in the bottom JPanel. 我还想在底部JPanel的JLabel,JTextField和JButton之间添加一些填充。

If anyone could give me some suggestions on resizing these components I would be most grateful 如果有人可以给我一些调整这些组件大小的建议,我将不胜感激

  • Since you want your textfields to be multilined, use JTextAreas . 由于您希望文本字段是多行的,因此请使用JTextAreas JTextFields are single lined only. JTextFields仅是单行的。
  • Your components are right next to each other which isn't the same look as your intended outcome. 您的组件彼此相邻,外观与预期结果不同。 There may be some method that gives your components some breathing room before you would call frame.pack() 在调用frame.pack()之前,可能有一些方法为组件提供了喘息的空间。
  • Look for any method that can make a component fill the total amount of room it's given; 寻找可以使组件填充其给定空间总量的任何方法; especially when you want something to fill a large chunk of space. 尤其是当您想要某物填充大量空间时。
  • You can set the number of columns instead of using setSize() for your JTextFields/JTextAreas. 您可以设置列数,而不是对JTextFields / JTextAreas使用setSize() Just saying. 只是说。
  • Reviewing all of Java's Layout Managers would help you get a grasp of the capabilities and use cases for each layout manager 复习所有Java的布局管理器将帮助您掌握每个布局管理器的功能和用例

There are a few layout managers that are flexible enough to perform this, such as Mig, Gridbag, and SpringLayout . 有一些布局管理器足够灵活地执行此操作,例如Mig,Gridbag和SpringLayout

In your case, you'd have the following constraints: 就您而言,您将受到以下限制:

outputarea - south border constrained to be ###px from the north border of the contentPane outputarea south边界限制为距contentPane北部边界### px

errorReportArea - north border constrained to be 0px from outputarea 's south, and south border constrained to be 0px from inputPanel 's north. errorReportArea north边界距离outputarea的南部为0px, south边界距离inputPanel的北部为0px。

inputPanel - north border constrained to be ##px from the south border of the contentPane . inputPanel north边界限制为距contentPane的南边界## px。

GUI builders such as WindowBuilder will allow you to do this pretty quickly. 诸如WindowBuilder之类的 GUI构建器将使您能够快速完成此操作。 You just drop in the layout onto the contentPane and then set the constraints. 您只需将布局放到contentPane上,然后设置约束即可。

If you have to use a box layout look at the glue and rigidArea methods in Box . 如果您必须使用Box布局,请查看Box中的粘胶方法和initialArea方法。 If you can use other layouts, go with those suggested by the other answers. 如果可以使用其他布局,请使用其他答案建议的布局。

I have created a solution with the MigLayout manager. 我已经使用MigLayout管理器创建了一个解决方案。

Here are some recommendations: 以下是一些建议:

  • Put application code outside the constructor; 将应用程序代码放在构造函数之外; in the solution, the code is placed in the initUI() method. 在解决方案中,代码被放置在initUI()方法中。
  • The application should be started on EDT by calling the EventQueue.invokeLater() . 应通过调用EventQueue.invokeLater()在EDT上启动应用程序。 (See the main() method of the provided solution.) (请参阅提供的解决方案的main()方法。)
  • Use a modern, flexible layout manager: MigLayout , GroupLayout , or FormLayout . 使用现代,灵活的布局管理器: MigLayoutGroupLayoutFormLayout Take some time to study them to fully understand the layout management process. 请花一些时间研究它们,以充分了解布局管理过程。 It is important have a good understanding of this topic. 对此主题有个很好的了解是很重要的。
  • Shorten the labels; 缩短标签; use more descriptive tooltips instead. 请改用更具描述性的工具提示。
package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;

public class MigLayoutSolution extends JFrame {

    private JTextArea outputArea;
    private JTextField errorReportField;

    private JLabel nameLabel;
    private JLabel numberLabel;
    private JLabel priceLabel;

    private JTextField nameField;
    private JTextField numberField;
    private JTextField priceField;

    private JButton addVolumeButton;
    private JButton addNumberButton;

    public MigLayoutSolution() {

        initUI();
    }

    private void initUI() {

        setLayout(new MigLayout());

        outputArea = new JTextArea(10, 20);
        errorReportField = new JTextField(15);

        nameLabel = new JLabel("Item name");
        numberLabel = new JLabel("# of units");
        numberLabel.setToolTipText("Number of units (or Volume in L)");
        priceLabel = new JLabel("Price per unit");
        priceLabel.setToolTipText("Price per unit (Or L) in pence");
        nameField = new JTextField(10);
        numberField = new JTextField(10);
        priceField = new JTextField(10);

        addVolumeButton = new JButton("AddVol");
        addVolumeButton.setToolTipText("Add by Volume");
        addNumberButton = new JButton("AddNum");
        addNumberButton.setToolTipText("Add by number of units");

        add(new JScrollPane(outputArea), "grow, push, wrap");
        add(errorReportField, "growx, wrap");
        add(nameLabel, "split");
        add(nameField);
        add(numberLabel);
        add(numberField);
        add(priceLabel);
        add(priceField);
        add(addVolumeButton);
        add(addNumberButton);

        pack();

        setTitle("Fuel station");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MigLayoutSolution ex = new MigLayoutSolution();
                ex.setVisible(true);
            }
        });
    }
}

在此处输入图片说明

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

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