jtextarea在setText()之后调整大小

时间:2014-05-30 14:19:17

标签: java swing jtextarea autoresize settext

我有显示jTextArea1的问题。加载文件后,textArea会调整大小,如下所示:

enter image description here

TextArea被添加到jScrollPane中。加载文件代码:

public class program extends javax.swing.JFrame {

private String textEncode;
...

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)    {                                         
JFileChooser fch = new JFileChooser();
int choose = fch.showOpenDialog(this);
if(choose == JFileChooser.APPROVE_OPTION) {
    String help = fch.getSelectedFile().getPath();
    jTextField2.setText(help);
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(help), "UTF-8"));
        String line;
        String readed = "";
        while((line = in.readLine()) != null) {
            readed = readed + line + "\n";
        }
        jTextArea1.setText(readed);
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this, "Nie znaleziono pliku", "Błąd wczytywania", JOptionPane.ERROR_MESSAGE);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(aes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(aes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

jTextArea1代码由NetBeans生成:

jTextArea1 = new javax.swing.JTextArea();
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setMaximumSize(new java.awt.Dimension(104, 64));
jScrollPane1.setViewportView(jTextArea1);

1 个答案:

答案 0 :(得分:0)

问题在于使用setMaximumSize。您还应该使用lineWrapwrapStyle