gpt4 book ai didi

java - 将标签与文本字段对齐

转载 作者:行者123 更新时间:2023-12-01 22:14:46 25 4
gpt4 key购买 nike

出于某种原因,我无法让我的标签对象(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 ,特别是 Features ,它清楚地展示了您如何解决问题。

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

BoxLayout

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 和字体大小不同时,您会获得更好的结果

关于java - 将标签与文本字段对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31308421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com