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

时间:2017-01-31 21:10:54

标签: java user-interface

我试图在JTextArea和JButton中对齐文本的位置,但是对于我尝试的所有内容,要么没有任何反应,要么对齐仍然稍微偏离。

以下是: enter image description here (您可以看到突出显示的选项,JButton(中心)略低于两侧的两个JTextAreas。)

以下是一些代码:

                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)来更改ALMOST工作的JButton的位置。我也试过改变JTextAreas的垂直对齐方式,但没有改变。

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

1 个答案:

答案 0 :(得分:1)

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