Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对齐两个不同组件的文本位置?_Java_User Interface - Fatal编程技术网

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

Java 如何对齐两个不同组件的文本位置?,java,user-interface,Java,User Interface,我试图在JTextArea和JButton中对齐文本的位置,但无论我做了什么尝试,要么什么都没有发生,要么对齐仍然有点偏离 以下是is的外观: 通过突出显示的选项,您可以看到JButton中心略低于两侧的两个JTextArea 下面是一些代码: 现在我正在使用categoryFile[I][j].setVerticalAlignmentSwingConstants.TOP来更改JButton的位置,这几乎可以正常工作。我也尝试过改变JTextAreas的垂直对齐方式,但没有任何改变 如何对齐这些

我试图在JTextArea和JButton中对齐文本的位置,但无论我做了什么尝试,要么什么都没有发生,要么对齐仍然有点偏离

以下是is的外观: 通过突出显示的选项,您可以看到JButton中心略低于两侧的两个JTextArea

下面是一些代码:

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


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

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

如果在不同的组件中使用不同的字体,则很难对齐不同组件的文本。另外,为什么要使用JTextArea?您是否预计该组件会有多行文本?如果不是的话,你应该改用JTextField。是的,所有不同的文本显示方式都让我困惑,所以我选择了一种。我也没有注意到不同的字体,谢谢。
                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);