Java 更改JTextArea的大小
我正在尝试创建一个简单的GUI,其中两个JTextArea相互重叠。我已经能够做到这一点,但我希望底部JTextArea的大小大约是顶部的一半,但我还没有弄清楚如何做到这一点。我已经尝试过更改JTextArea中的行数,但没有成功。这是我的密码:Java 更改JTextArea的大小,java,swing,jtextarea,Java,Swing,Jtextarea,我正在尝试创建一个简单的GUI,其中两个JTextArea相互重叠。我已经能够做到这一点,但我希望底部JTextArea的大小大约是顶部的一半,但我还没有弄清楚如何做到这一点。我已经尝试过更改JTextArea中的行数,但没有成功。这是我的密码: public final class View extends JFrame implements ViewInterface { /** * Controller object. */ private Controller controller
public final class View extends JFrame implements ViewInterface {
/**
* Controller object.
*/
private Controller controller;
/**
* Constants
*/
private static final int LINES_IN_TOP_TEXT = 4,
LINE_LENGTHS_IN_TOP_TEXT = 8, LINES_IN_BOTTOM_TEXT = 1,
LINE_LENGTHS_IN_BOTTOM_TEXT = 8, ROWS_IN_THIS_GRID = 2,
COLUMNS_IN_THIS_GRID = 1;
/**
* Text areas.
*/
private final JTextArea topText;
private final JTextArea bottomText;
/**
* Default constructor.
*/
public View() {
/*
* Call JFrame superclass with title
*/
super("Two JTextAreas");
/*
* Create widgets
*/
this.topText = new JTextArea("", LINES_IN_TOP_TEXT,
LINE_LENGTHS_IN_TOP_TEXT);
this.bottomText = new JTextArea("", LINES_IN_BOTTOM_TEXT,
LINE_LENGTHS_IN_BOTTOM_TEXT);
/*
* Customize font of top text
*/
Font topFont = new Font("TimeRoman", Font.BOLD, 30);
this.topText.setFont(topFont);
this.topText.setForeground(Color.WHITE);
this.topText.setBackground(Color.RED);
/*
* Customize font of bottom text
*/
Font bottomFont = new Font("TimeRoman", Font.BOLD, 12);
this.bottomText.setFont(bottomFont);
this.bottomText.setForeground(Color.WHITE);
this.bottomText.setBackground(Color.RED);
/*
* Text areas should wrap lines, and outputText should be read-only
*/
this.topText.setEditable(false);
this.topText.setLineWrap(true);
this.topText.setWrapStyleWord(true);
this.bottomText.setEditable(false);
this.bottomText.setLineWrap(true);
this.bottomText.setWrapStyleWord(true);
/*
* Create scroll panes for the text areas
*/
JScrollPane topTextScrollPane = new JScrollPane(this.topText);
JScrollPane bottomTextScrollPane = new JScrollPane(this.bottomText);
/*
* Organize main window using grid layout
*/
this.setLayout(new GridLayout(ROWS_IN_THIS_GRID, COLUMNS_IN_THIS_GRID));
/*
* Add scroll panes and button panel to main window
*/
this.add(topTextScrollPane);
this.add(bottomTextScrollPane);
/*
* Start the main application window
*/
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
有人知道我怎么解决这个问题吗?谢谢 您可以使用Java:
或者使用setSize和setPreferedSize将JTextArea行或列设置为所需的首选大小。如果一个要有与另一个相同的高度,那么就给它一半的行数 此外,您的布局GridLayout将忽略行数,而是使其持有的所有组件大小相同,基本上是容纳所有组件的最大大小。而是使用性能更好的布局管理器,如BoxLayout。e、 g
// this.setLayout(new GridLayout(ROWS_IN_THIS_GRID, COLUMNS_IN_THIS_GRID));
setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
我认为这是一种方法,但更改底部文本区域的行似乎并没有改变结果GUI中的任何内容。@不接近四:不要使用网格布局,因为它会覆盖组件的首选大小!