宽度不变,如何实现让jTextPane根据里面的文本内容自动调整高度。

解决方案 »

  1.   

    可以尝试一下放到JScrollPanel里去
    你是做javaSwing的吗?有时间可以交流交流。
      

  2.   

    也可以尝试使用JTextArea那个比这个方便多了
      

  3.   

    自己研究出来了,需要两次的getPreferredSize().
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package test1;/**
     *
     * @author netcenter
     */
    public class TestPane extends javax.swing.JFrame {    /**
         * Creates new form NewJFrame
         */
        public TestPane() {
            initComponents();
        }    /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {        jPanel1 = new javax.swing.JPanel();
           // jScrollPane1 = new javax.swing.JScrollPane();
            jTextPane1 = new javax.swing.JTextPane();
            jTextPane1.setContentType("text/html");
            jTextPane1.setSize(300, jTextPane1.getPreferredSize().height);
            jTextPane1.setText("发来的机会苏打粉阿斯蒂芬了阿萨德发生迪兰恒进as大法师打飞机阿拉法所拉动阿斯蒂芬" +
             "鸡丝豆腐阿道夫垃圾啊打飞机阿斯蒂芬了阿斯蒂芬as发烧发烧发生纠纷撒旦法阿斯蒂芬了阿斯蒂芬阿斯蒂芬房" +
             "价;阿斯蒂芬阿斯蒂芬阿斯蒂芬艾丝凡;阿斯蒂芬阿斯蒂芬");
            int hei = jTextPane1.getPreferredSize().height;
            jTextPane1.setSize(300, hei);        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            getContentPane().setLayout(null);        jPanel1.setLayout(null);      //  jScrollPane1.setViewportView(jTextPane1);        jPanel1.add(jTextPane1);
         //   jScrollPane1.setBounds(40, 50, 280, 60);        getContentPane().add(jPanel1);
            jPanel1.setBounds(0, 0, 400, 300);        pack();
        }// </editor-fold>    /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /*
             * Set the Nimbus look and feel
             */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /*
             * If Nimbus (introduced in Java SE 6) is not available, stay with the
             * default look and feel. For details see
             * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(TestPane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(TestPane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(TestPane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(TestPane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>        /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {
                    new TestPane().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextPane jTextPane1;
        // End of variables declaration
    }