【问题标题】:Other placement of fields everytime i run the code每次我运行代码时的其他字段位置
【发布时间】:2019-06-19 06:14:53
【问题描述】:

我的申请卡住了。每次我运行我的代码时,我的字段例如fromTextField 一次宽度为 180 其他时间我的宽度 private static final Integer widthField = 232


代码:


package gui;

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

public class MailGui extends JFrame {
    private static final Integer widthField = 232;

private MailGui() {
        JPanel northPanel = new JPanel();
        northPanel.setLayout(new GridLayout(5, 5));

        JLabel fromLabel = new JLabel("From: ", SwingConstants.LEFT);
        JLabel passwdLabel = new JLabel("Password: ", SwingConstants.LEFT);
        JLabel toLabel = new JLabel("To: ", SwingConstants.LEFT);
        JLabel subjectLabel = new JLabel("Subject: ", SwingConstants.LEFT);
        JLabel textLabel = new JLabel("Content: ", SwingConstants.LEFT);

        JTextField fromTextField = new JTextField();

        JTextField toTextField = new JTextField();
        JPasswordField passwdPasswordField = new JPasswordField();
        JTextField subjectTextField = new JTextField();
        JTextArea textArea = new JTextArea(8, 30);

        JButton sendButton = new JButton("Send");

        textArea.setLineWrap(true);

        northPanel.add(fromLabel);
        northPanel.add(fromTextField);

        northPanel.add(passwdLabel);
        northPanel.add(passwdPasswordField);

        northPanel.add(toLabel);
        northPanel.add(toTextField);

        northPanel.add(subjectLabel);
        northPanel.add(subjectTextField);

        northPanel.add(textLabel);
        northPanel.add(textArea);

        this.add(northPanel, BorderLayout.NORTH);

        JScrollPane scrollPane = new JScrollPane(textArea);
        this.add(scrollPane, BorderLayout.CENTER);

        JPanel southPanel = new JPanel();
        southPanel.add(sendButton);

        add(southPanel, BorderLayout.SOUTH);

        this.pack();

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);


        fromTextField.setBounds(textArea.getX() + 100, 0, widthField, 19);
        passwdPasswordField.setBounds(textArea.getX() + 100, 19, widthField, 19);
        toTextField.setBounds(textArea.getX() + 100, 38, widthField, 19);
        subjectTextField.setBounds(textArea.getX() + 100, 57, widthField,         19);

    }

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

我有什么:



或者



我除了:



感谢您的每一个帮助。问。

【问题讨论】:

  • 该代码中有一些可疑且不是最佳的代码,但我运行了 6 次并得到了same result every time
  • 那是有线的……那我该怎么办呢?有什么建议吗?
  • 首先: 1) 在 EDT 上启动 GUI。 2) 不要尝试为组件设置明确的尺寸或位置或位置。而是使用布局和布局填充来获得所需的效果。 3) 不要将GridLayout 用于标签和字段组合。网格布局将使所有组件具有相同的宽度。相反,可以为该部分使用GridBagLayout。 ..

标签: java swing layout layout-manager


【解决方案1】:

调用 setBounds() 方法什么都不做。布局管理器将根据布局管理器的规则覆盖大小/位置。在 GridLayout 的情况下,所有组件的大小都将调整为最大组件的首选大小,或者随着框架大小的变化,每个单元格都会调整以填充框架中的可用空间。

当你创建一个 JTextField 时,代码应该是这样的:

JTextField textField = new JTextField(15);

这将允许文本字段确定其自己的首选大小,以根据文本字段的字体显示 15 个“W”字符。

那么布局管理器可以更好地确定每个组件的大小

如果您希望标签和文本字段的宽度不同,则需要使用不同的布局管理器。可能是一个 GridBagLayout。阅读How to Use GridBagLayout 上的 Swing 教程以获取更多信息和示例。

请注意,教程示例向您展示了如何在事件调度线程 (EDT) 上创建 GUI。

【讨论】:

    【解决方案2】:

    在 GUI 组合结束时调用 pack() 以便以明确的方式对其进行一次布局。

    几何体对 textArea 的依赖是不幸的。把它也放在布局中。

    有一些不错的 GUI 编辑器,带有更复杂的布局管理器。

    【讨论】:

      猜你喜欢
      • 2020-04-12
      • 2018-05-29
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多