-2

我创建了一个这样的框架。但我不知道如何对齐。在此处输入图像描述

我希望 1.1 版在顶部居中,在下一行主题标签后跟主题文本框,在下一行正文标签后跟正文文本框。

当我输入更多时,在文本框中,它不会反弹到下一次。文本变为不可见但在同一行输入。我希望你能帮助我。对不起,我的英语不好。

4

1 回答 1

1

您需要更改布局管理器。

首先查看布局管理器的可视化指南使用布局管理器

就我个人而言,我推荐GridBagLayout它,它是默认库中最灵活但也是最复杂的布局管理器

您可能还会发现如何使用某些用途的滚动窗格

更新示例

查看如何使用 GridBagLayout了解更多详情

在此处输入图像描述

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout27 {

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

    public TestLayout27() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JLabel l1 = new JLabel("Timedoff Version 1.1", JLabel.CENTER);
            l1.setBackground(Color.red);
            l1.setForeground(Color.yellow);
            JLabel l2 = new JLabel("subject:");
            JTextField b = new JTextField("subject", 15);
            JLabel l3 = new JLabel("Body:");
            JTextArea a1 = new JTextArea("boby", 10, 20);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;

            add(l1, gbc);
            gbc.gridy++;
            add(l2, gbc);
            gbc.gridy++;
            add(b, gbc);
            gbc.gridy++;
            add(l3, gbc);
            gbc.gridy++;
            add(a1, gbc);
        }        
    }
}
于 2013-08-31T06:28:18.397 回答