繁体   English   中英

如何在Swing中左右对齐组件?

[英]How do I left and right align components in Swing?

我有一个看似简单的问题。 我有一些标签,我想在左边对齐但是当我调整大小时,它们会开始向中间漂移。 这将取消我计划添加的其他组件的对齐。 我该怎么办才能让他们保持在左边?

例

这是简短的代码,不知道我的问题在这里:

package com.protocase.notes.views;

import com.protocase.notes.model.Note;
import com.protocase.notes.model.User;
import java.awt.Component;
import java.util.Date;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;

/**
 * @author dah01
 */
public class NotesPanel extends JPanel{

    public NotesPanel(Note note){
        this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        JLabel creatorLabel = new JLabel("Note by "+note.getCreator()+ " @ "+note.getDateCreated());
        creatorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        creatorLabel.setHorizontalAlignment(JLabel.LEFT);


        JTextArea notesContentsArea = new JTextArea(note.getContents());
        notesContentsArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(notesContentsArea);

        JLabel editorLabel = new JLabel(" -- Last edited by "+note.getLastEdited() +" at "+note.getDateModified());
        editorLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        editorLabel.setHorizontalAlignment(JLabel.LEFT);

        this.add(creatorLabel);
        this.add(scrollPane);
        this.add(editorLabel);
        this.setBorder(new BevelBorder(BevelBorder.RAISED));
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("Notes Panel");
        Note note = new Note();
        User user = new User();
        user.setFirstName("d");
        user.setLastName("h");
        user.setUserID("dah01");
        note.setCreator(user);
        note.setLastEdited(user);
        note.setDateCreated(new Date());
        note.setDateModified(new Date());
        note.setContents("A TEST CONTENTS");
        NotesPanel np = new NotesPanel(note);

        JScrollPane scroll = new JScrollPane(np);
        frame.setContentPane(scroll);
        np.setVisible(true);
        frame.setVisible(true);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

如果要对齐面板中的内容,则必须对齐所有内容。 您忘记了对齐JScrollPane 如果将此行添加到代码中,则应为您修复对齐:

    scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);

你的新构造函数会是什么样子:

public NotesPanel(Note note){
    this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
    JLabel creatorLabel = new JLabel("Note by "+note.getCreator()+ " @ "+note.getDateCreated());
    creatorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
    creatorLabel.setHorizontalAlignment(JLabel.LEFT);


    JTextArea notesContentsArea = new JTextArea(note.getContents());
    notesContentsArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(notesContentsArea);
    scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);

    JLabel editorLabel = new JLabel(" -- Last edited by "+note.getLastEdited() +" at "+note.getDateModified());
    editorLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
    editorLabel.setHorizontalAlignment(JLabel.LEFT);

    this.add(creatorLabel);
    this.add(scrollPane);
    this.add(editorLabel);
    this.setBorder(new BevelBorder(BevelBorder.RAISED));
}

根据您的意见 ,我建议您使用其他layout ,我会推荐MigLayout

在这里你使用MigLayout

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextArea;


public class MigLayoutDemo {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new MigLayoutDemo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MigLayoutDemo() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        frame.setContentPane(contentPane);
        contentPane.setLayout(new MigLayout("", "[grow]", "[][grow][]"));

        JLabel lblLabel = new JLabel("Label 1");
            lblLabel.setBorder(BorderFactory.createLineBorder(Color.red));
        contentPane.add(lblLabel, "cell 0 0,alignx left");

        JTextArea textArea = new JTextArea();
        contentPane.add(textArea, "cell 0 1,grow");

        JLabel lblLabel_1 = new JLabel("Label 2");
            lblLabel_1.setBorder(BorderFactory.createLineBorder(Color.red));
        contentPane.add(lblLabel_1, "cell 0 2,alignx left");
        frame.setVisible(true);
    }

}

输出:

在此输入图像描述

如您所见,标有红色边框的 labels未向中间拉伸,它们左对齐。

使用BorderLayout绝对可以解决您的问题。

this.setLayout(new BorderLayout());

this.add(creatorLabel, BorderLayout.NORTH);
this.add(scrollPane);
this.add(editorLabel, BorderLayout.SOUTH);

JLabel对齐

或者,如果UI中显示的组件多于示例代码中显示的组件,则仍可以使用GridBagLayout 我知道没有多少人喜欢使用这个,因为它非常冗长,但在我看来,它是最强大的摇摆布局管理器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM