(转载)swing jtextarea 自动行高

Shaka 1月前 ⋅ 55 阅读
fileNameField = new JTextArea();
fileNameField.setEnabled(false);
fileNameField.setDisabledTextColor(Color.BLACK);
//红色划重点自动行高
//fileNameField.setLineWrap(true) 允许折行
fileNameField.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
try {
Rectangle rect = fileNameField.modelToView(fileNameField.getText().length());
fileNameField.setBounds(10, 10, 200,Math.max(30, rect.y + rect.height));
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
});
JScrollPane fileNameJsp=new JScrollPane(fileNameField);
fileNameJsp.setPreferredSize(new Dimension(160, 155));
fileNameJsp.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
fileNameJsp.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: