gpt4 book ai didi

java - 如何对齐两个不同组件的文本位置?

转载 作者:行者123 更新时间:2023-12-01 08:57:16 26 4
gpt4 key购买 nike

我正在尝试对齐 JTextArea 和 JButton 中的文本位置,但经过我尝试的所有操作,要么没有任何反应,要么对齐仍然略有偏差。

如下所示: enter image description here(您可以通过突出显示的选项看到 JButton(中心)略低于两侧的两个 JTextArea。)

这是一些代码:

                categoryFile[i][j] = tempButton;
categoryFile[i][j].setBackground(Color.white);
categoryFile[i][j].setForeground(Color.black);
categoryFile[i][j].setOpaque(true);
categoryFile[i][j].setFocusable(false);
categoryFile[i][j].setBorderPainted(false);;
categoryFile[i][j].setVerticalAlignment(SwingConstants.TOP);
categoryFile[i][j].setPreferredSize(new Dimension(500,10));
categoryFile[i][j].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openPDFWithOptions(filePath,fileName);
}
});

JPanel listRow = new JPanel();
listRow.setBackground(Color.white);
listRow.setLayout(new BorderLayout());
listRow.setPreferredSize(new Dimension(800, 40));

JTextArea category = new JTextArea(fileElements[0]);
category.setEditable(false);
JTextArea parent = new JTextArea(fileElements[1]);
parent.setEditable(false);

listRow.add(parent,BorderLayout.WEST);
listRow.add(categoryFile[i][j],BorderLayout.CENTER);
listRow.add(category,BorderLayout.EAST);

categoryLists[i].add(listRow,c);

现在我正在使用categoryFile[i][j].setVerticalAlignment(SwingConstants.TOP)来更改JButton的位置,这几乎可以工作。我也尝试过更改 JTextAreas 的垂直对齐方式,但没有任何改变。

如何对齐这些组件中的文本?

最佳答案

解决此问题的最快方法可能是在第一列和第三列上添加一些填充,以将所有文本设置为相同的高度。请参阅Jpanel Padding

关于java - 如何对齐两个不同组件的文本位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41967537/

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