如何使元素根据JavaSwing中窗口的大小自动调整大小?

如何使元素根据JavaSwing中窗口的大小自动调整大小?,java,swing,intellij-idea,Java,Swing,Intellij Idea,我使用的IDE是Intellij。 在这里,我创建了一个转换货币的小程序。 我使用BorderLayout作为根面板,使用flowLayout作为底部按钮。对于西面板和东面板,我使用了GridLayout(Intellij)。 当我运行程序时,它可以正常显示如下: 更改其大小后,元素之间的间距开始如下扩展: 如何让他们自动调整距离 这是我的密码: import javax.swing.*; import java.awt.event.ActionEvent; import jav

我使用的IDE是Intellij。 在这里,我创建了一个转换货币的小程序。 我使用BorderLayout作为根面板,使用flowLayout作为底部按钮。对于西面板和东面板,我使用了GridLayout(Intellij)。 当我运行程序时,它可以正常显示如下:

更改其大小后,元素之间的间距开始如下扩展:

如何让他们自动调整距离

这是我的密码:

  import javax.swing.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;

  /**
   * Created by Bob on 2017/5/11.
   */



 public class layout {

private JPanel converterRootPanel;
private JPanel westPanel;
private JLabel selectNationPanel;
private JLabel currencyToConvett;
private JLabel currencyConverted;
private JComboBox currencyType;
private JTextField input;
private JTextField output;
private JPanel eastPanel;
private JPanel southPanel;
private JButton convertButton;
private JButton clearButton;
private JLabel convertToLabel;
private JComboBox convertType;
private JPanel northPanel;
public int selection1;
public int selection2;
public Double toConvert;
public double[][] rate1={{0,0.1335,0.1449,16.5172,163.4922},{7.4927,0,1.0857,123.7900,
        1225.0380},{6.9029,0.9382,0,114.01,1129.19},{0.06053,0.00808,0.008771,0,9.9043},{0.006112,
        0.0008158,0.0008856,0.101,0}};

public layout() {
    currencyType.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            selection1 = currencyType.getSelectedIndex();
        }
    });
    convertType.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            selection2 = convertType.getSelectedIndex();
        }
    });

    convertButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(selection1==selection2){
                JOptionPane.showConfirmDialog(null, "You have to choose different currency types!", "Error Alert", JOptionPane.CANCEL_OPTION);
            }
            output.setText("");
            toConvert = Double.parseDouble(input.getText().toString());
            Double convertResult = toConvert*rate1[selection1][selection2];
            output.setText(convertResult.toString());
        }
    });
    clearButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            output.setText("");
            input.setText("");
            convertType.setSelectedIndex(0);
            currencyType.setSelectedIndex(0);
        }
    });
}

public static void main(String[] args) {
    JFrame frame = new JFrame("layout");
    frame.setContentPane(new layout().converterRootPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    //layout lay = new layout();
}

private void createUIComponents() {
    // TODO: place custom component creation code here
}

}

您要做的是通过布局管理器完成。标准库中有几个用于Java的库,还有其他自定义库,如MigLayout

Java教程有一整节是关于布局管理器的


GridBagLayout
的一个基本示例如下

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

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

public class Basic {
    JFrame frame;
    JPanel panel;
    JLabel label;
    JButton button;

    public void createAndRun() {
        frame = new JFrame("Basic Example");

        setUp();
        frame.getContentPane().add(panel);

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

    private void setUp() {
        panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        label = new JLabel("I am a JLabel");
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 0.5;
        c.weighty = 0;
        panel.add(label, c);

        button = new JButton("I am a JButton");
        c.gridx = 0;
        c.gridy = 1;
        c.weighty = 0.5;
        panel.add(button, c);
    }

    public static void main(String[] args) {
        Basic b = new Basic();
        b.createAndRun();
    }
}
然而,正如教程所说的那样

“GridBagLayout是Java平台提供的最灵活、最复杂的布局管理器之一。”

因此,如果您在
GridBagLayout
方面遇到问题,那么您可能需要事先查看其他布局管理器


最后,我想提出一些改进代码的方法

最吸引我注意的是这句台词

frame.setContentPane(new layout().converterRootPanel);
我建议不要创建
JFrame
并在
main
方法中初始化
Layout
类。相反,应该先初始化类,然后调用一个方法来创建框架

Layout l = new Layout();
l.createFrame();
这在上面的示例代码中显示。

A使用GridBagConstraints的
weightx
weighty
属性来确定额外空间的分配方式。GridBagLayout使用列中所有单元格的最大权重X来确定该列中所有单元格的实际水平权重,同样,行中所有单元格的最大权重决定该行的垂直权重。如果所有列的权重X为零,则它们都水平居中。如果所有行的权重均为零,则它们都垂直居中

通常,一个好的设计是让输入字段水平拉伸,而标签始终保持相同的大小。您可能希望行始终具有相同的垂直间距,并且所有额外的空间都显示在整个行集合的上方或下方

要拉伸特定列的所有单元格,只需设置该列中一个单元格的
weightx

JPanel buttonPanel = new JPanel();
buttonPanel.add(convertButton);
buttonPanel.add(clearButton);

converterRootPanel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_END;

// First row

converterRootPanel.add(selectNationPanel, gbc);

gbc.weightx = 1;

gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
converterRootPanel.add(currencyType, gbc);

gbc.weightx = 0;
gbc.insets.top = 3;

// Second row

gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
converterRootPanel.add(convertToLabel, gbc);

gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
converterRootPanel.add(convertType, gbc);

// Third row

gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
converterRootPanel.add(currencyToConvett, gbc);

gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
converterRootPanel.add(input, gbc);

// Fourth row

gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
converterRootPanel.add(currencyConverted, gbc);

gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
converterRootPanel.add(output, gbc);

// Button row

gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
converterRootPanel.add(buttonPanel, gbc);

组合框似乎从未在显示的代码中创建过。此外,似乎还有许多不必要的废物。为了更快地获得更好的帮助,发布一个或…但是基本上,有很多方法可以让组合框和文本字段扩展到可用的宽度。其一是将这些组件中的每一个放置在
中心约束处的
边界布局
,而标签则放置在
西部
。不幸的是,这将不会对齐标签的右边缘。相反,您可能希望使用单个
GridBagLayout
GroupLayout
。后者通常只能由IDE中的GUI设计师使用,但GBL可以使用适当的
GridBagConstraint
来扩展输入组件的宽度。正如Andrew所说,发布一个日志是值得的,这样您可以更快地获得所需的帮助。然而,正如我的回答所说,你所追求的是一个新的目标。如果您想让我进一步澄清我的答案,或者需要帮助理解其中的某些部分,请告诉我我推荐Oracles教程/Java文档: