4

我有一个看起来很简单的问题。我有一些我想向左对齐的标签,但是当我调整大小时,它们开始向中间漂移。这会打乱我计划添加的其他组件的对齐方式。我该怎么做才能让它们保持在左侧?

例子

这是简短而简单的代码,不确定我的问题出在哪里:

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);
    }
}
4

3 回答 3

7

如果要对齐面板中的内容,则必须对齐所有内容。你忘了对齐你的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));
}
于 2012-08-23T17:58:25.133 回答
4

根据您的评论,我建议您与其他人一起去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,标有红色边框的没有拉伸到中间,它们是左对齐的。

于 2012-08-23T16:58:48.280 回答
1

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

this.setLayout(new BorderLayout());

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

JLabel 对齐

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

于 2012-08-23T17:57:32.010 回答