Mic*_*ian 5 java swing jtextarea
当我运行下面的示例代码时,JTextArea的宽度是固定的(100px),而其高度是动态调整的,因为我在其中键入了一些文本.
例如,我从这开始:
--------------------
| some text |
--------------------
Run Code Online (Sandbox Code Playgroud)
当我输入更多文本时,JTextArea的高度会扩展,以便在保留宽度的同时适合内容:
--------------------
| some text, some |
| other longer text|
| etc... |
--------------------
Run Code Online (Sandbox Code Playgroud)
如何加倍JTextArea的宽度?当我这样做时,通过改变preferredSize高度不再是动态的.
public class TestTextArea extends JFrame {
public static void main(String[] args) {
new TestTextArea().setVisible(true);
}
public TestTextArea() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0,0,800,600);
setContentPane(createPane());
}
protected Container createPane() {
JTextArea textArea = createTextArea();
// ------------------------------------------
// UNCOMMENT TO DOUBLE THE WIDTH OF JTextArea
// Dimension oldPrefSize = textArea.getPreferredSize();
// Dimension newPrefSize = new Dimension(oldPrefSize.width * 2, oldPrefSize.height);
// textArea.setPreferredSize(newPrefSize);
JPanel pane = new JPanel(new FlowLayout());
pane.add(textArea);
return pane;
}
protected JTextArea createTextArea() {
JTextArea textArea = new JTextArea();
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
return textArea;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11526 次 |
| 最近记录: |