簡體   English   中英

如何對齊兩個不同組件的文本位置?

[英]How to align text position for two different components?

我正在嘗試在JTextArea和JButton中對齊文本的位置,但是使用我嘗試的所有內容,要么什么都沒有發生,要么對齊仍然略有偏離。

看起來是這樣的: 在此處輸入圖片說明 (通過突出顯示的選項,您可以看到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的垂直對齊方式,但是沒有任何改變。

如何在這些組件中對齊文本?

解決此問題的最快方法可能是在第一和第三列上添加一些填充以將所有文本設置為相同的高度。 參見Jpanel填充

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM