将标签与文本字段对齐

Aligning Labels with TextFields

出于某种原因,我无法让我的标签对象 (TermLabel,DefinitionLabel) 与我的文本字段对齐,这很奇怪,因为我使用的是 BoxLayout 并且应该将每个组件对齐。有什么我想念的吗?

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class AddNewCard extends JFrame {

    /*
     * produces another frame for adding a new card for the set
     */
    private JButton create;
    private JButton importDefinition;
    private CardSet cardSet;
    private JLabel termLabel = new JLabel("Term");
    private JTextField termField = new JTextField();
    private JLabel definitionLabel = new JLabel("Definition");

    private JTextArea definitionArea = new JTextArea();
    private JScrollPane scrollPane;
    private boolean editing = false;
    private String term;
    private String definition;
    private AddNewCard editFrame;

    /*
     * adding a new card
     */
    public AddNewCard(int x, int y) {
        super("Add New Card");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.add(new CardPanel());
        this.setSize(100,200);
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
    }


    public Dimension getPreferredSize() {
        return new Dimension(300,300);     
    }

    private class CardPanel extends JPanel {

        public CardPanel() {

            this.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
            this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
            termField.setMaximumSize(new Dimension(1000, 20));


            this.add(termLabel);
            this.add(termField);
            this.add(definitionLabel);

            definitionArea.setOpaque(true);
            definitionArea.setText(definition);
            definitionArea.setWrapStyleWord(true);
            definitionArea.setLineWrap(true);
            definitionArea.setEditable(true);
            definitionArea.setFocusable(true);
            scrollPane = new JScrollPane(definitionArea);
            this.add(scrollPane);

            createButtons(this);
        }


        private void createButtons(CardPanel panel) {
            if (!editing)
            create = new JButton("Create");
            else
            create = new JButton("Change");
            //importDefinition.addActionListener(new ButtonListener());
            create.addActionListener(new ButtonListener());
            create.setActionCommand("1");
            //importDefinition.setActionCommand("2");

            panel.add(create);
            //panel.add(importDefinition);


        }

    }
}

测试:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Test {
    public static void main(String[] arg) {
         SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createGUI(); 
                }
          });
    }

    public static void createGUI() {
        AddNewCard anc = new AddNewCard(100,100);
    }

}

首先,看一下 How to Use BoxLayout, in particular, the Features,它清楚地展示了您可能如何解决问题。

本质上,您需要将每个组件的 X 对齐设置为 Component.LEFT_ALIGNMENT

termLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
termField.setAlignmentX(Component.LEFT_ALIGNMENT);
definitionLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
//...
scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
//...
create.setAlignmentX(Component.LEFT_ALIGNMENT);

此外,比使用 setPreferredSize(或 getPreferredSize

更倾向于指定列(和行到文本区域)
private JTextField termField = new JTextField(10);
//...
private JTextArea definitionArea = new JTextArea(10, 20);

当 DPI 和字体大小不同时,您会获得更好的结果