每次我运行代码时,其他字段的放置

时间:2019-01-25 11:51:15

标签: java swing layout layout-manager

我坚持申请。每次我运行代码时,我的字段例如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();
    }
}

我所拥有的:


First



Second


我除了:


Second


感谢您的帮助。问

2 个答案:

答案 0 :(得分:1)

调用setBounds()方法不会执行任何操作。布局管理器将根据布局管理器的规则覆盖大小/位置。对于GridLayout,将所有组件的大小调整为最大组件的首选大小,或者随着框架大小的变化,每个单元格将进行调整以填充框架中的可用空间。

创建JTextField时,代码应类似于:

public bool IsAvailable()
{
    return Application.Context.PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureSensorStepCounter) &&
        Application.Context.PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureSensorStepDetector);
}

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

然后,布局管理器可以更好地确定每个组件的大小

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

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

答案 1 :(得分:0)

在GUI组成的末尾调用pack(),以便一次确定的布局。

不幸的是,几何图形对textArea的依赖性。也将其放在布局中。

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