如何根据JAVA摆动窗口的大小使元素自动调整尺寸?

时间:2017-05-11 13:59:59

标签: java swing intellij-idea

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

enter image description here

在改变其大小后,元素之间的差距开始扩大如下: enter image description here

如何让它们自动调整距离?

以下是我的代码:

  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
}

}

2 个答案:

答案 0 :(得分:3)

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

Java教程有关于布局管理器here

的整个部分

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方法中创建Layout并初始化main类。相反,首先需要初始化类然后调用方法来创建框架。

Layout l = new Layout();
l.createFrame();

这在上面的示例代码中显示。

答案 1 :(得分:0)

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

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

要使特定列的所有单元格都拉伸,您只需要在该列中设置一个单元格的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);