如何阻止 JFrame/JPanel 自动格式化组件之间的距离?

How do I stop JFrame/JPanel from auto-formatting the distances between my components?

所以我正在编写一个使用 GUI 的程序。 GUI 是从 Java 中的 JFrame 和 JPanel 构建的。我有一个 JTextArea() 和一个 JButton(),它们出现在左侧的 JTextArea 旁边。我还有一个图像,我使用下面的方法调用在本地导入("f" 是我的 JFrame 的变量名称):

f.setIconImage(ImageIO.read(new File("image.png")));

我不介意允许用户调整 JFrame 的大小,但我不喜欢的是 JFrame 会自动重新格式化我的 JFrame 上的组件——特别是我的图像和 JTextArea() 之间的距离。如果可能的话,我希望无论用户将 JFrame 调整到多大,我的图像和 JTextArea 之间的距离都保持不变。如果有帮助,这是我的其余代码:

public class GUI {
private JFrame f;
private JPanel p;
private JButton b1;
private JLabel lab;
private JTextArea tf;

private static final String FileName = "schedule.txt";
private static final String FileName2 = "schedule2.txt";

public void show()
{
    f = new JFrame("Scheduler");
    f.setSize(600, 400);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    p = new JPanel();
    //p.setPreferredSize(new Dimension(200, 400));

    b1 = new JButton("Run");

    p.add(b1);

    f.add(p);
    f.add(p, BorderLayout.SOUTH);
    f.add(new JLabel(new ImageIcon("image.png")));
    try {
        f.setIconImage(ImageIO.read(new File("image.png")));
    } catch(Exception z){
        System.out.println("Trouble reading file");
    }

    tf = new JTextArea();
    tf.setPreferredSize(new Dimension(300, 200));

    p.add(tf);

    b1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String text = tf.getText();
            try {
                FileWriter fw = new FileWriter(FileName);
                fw.write(text);
                fw.close();
                parseInfo();
            }
            catch(Exception ex)
            {

            }
        }
    });
    f.setVisible(true);

}

您 "might" 可以通过多种方式实现这一目标,但基本答案是,您需要选择一个或多个更能满足您需求的布局管理器。

例如,使用 GridBagLayout,您可以执行类似...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

public class JavaApplication1 {

    public static void main(String[] args) {
        new JavaApplication1();
    }

    public JavaApplication1() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(8, 8, 8, 8);

            JLabel label = new JLabel("Image placeholder") {
                // This is done only for demonstration purposes
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(128, 128);
                }
            };
            label.setBorder(new LineBorder(Color.DARK_GRAY));

            add(label, gbc);

            gbc.gridy++;
            gbc.fill = GridBagConstraints.BOTH;
            // This will cause the text area to occupy all the avalilable free space
            //gbc.weightx = 1;
            //gbc.weighty = 1;
            JTextArea ta = new JTextArea(10, 20);
            add(new JScrollPane(ta), gbc);

            JButton btn = new JButton("Run");
            gbc.gridy++;
            // Reset the constraints
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 0;
            gbc.weighty = 0;
            add(btn, gbc);
        }

    }
}

查看 Laying Out Components Within a Container 了解更多详细信息和想法

建议...

您的代码中有一些东西 "off",但是这...

tf = new JTextArea();
tf.setPreferredSize(new Dimension(300, 200));

p.add(tf);

可能是最引人注目的。

一般来说,JTextArea 将受益于包裹在 JScrollPane 中。此外,由于文本在多个平台上呈现的方式不一致,你应该避免使用 setPreferredSize(事实上,你应该避免在一般情况下使用它),而是依赖于 rows, columns 构造函数,它将根据当前字体和图形属性进行计算。