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

dav*_*nes 2 java swing alignment

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

例

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Nic*_*ppe 5

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

    scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
Run Code Online (Sandbox Code Playgroud)

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

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));
}
Run Code Online (Sandbox Code Playgroud)