【问题标题】:align Label and Text Area in Java Swing在 Java Swing 中对齐标签和文本区域
【发布时间】:2013-09-03 21:46:40
【问题描述】:

我创建了一个这样的框架。但我不知道如何对齐。

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

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

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 你想了解的类型是LayoutManager :-)

标签: java swing user-interface awt layout-manager


【解决方案1】:

您需要更改布局管理器。

首先查看A Visual Guide to Layout ManagersUsing Layout Managers

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

您还可以找到How to use Scroll Panes 的一些用途

更新示例

查看How to use 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);
        }        
    }
}

【讨论】:

  • 谢谢@MadProgrammer。它真的对我有帮助..但我尝试添加一个按钮 JButton but=new JButton("send");在最后一行我添加了 gbc.gridy++;添加(但是,gbc); ...整个结构发生了变化。当我删除该按钮时,我回到原来的.. 我怎样才能添加按钮???
猜你喜欢
  • 2010-12-22
  • 2018-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
相关资源
最近更新 更多