JTextPane文本在Mac OS中滚动时折叠

时间:2016-05-31 11:24:14

标签: java macos swing operating-system jtextpane

我正在创建一个简单的JTextPane并在视口设置后设置一个长文本,但是当我在Mac OS 10.11.5中运行程序时,我的JTextPane文本行相互折叠。当我慢慢滚动时,它不会发生,但是当我快速滚动时它会开始折叠。 Windows中没有出现此问题。这是我的示例源代码:

public class JTextPaneScroll extends javax.swing.JFrame {

    private String s = "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\n"
        + "\n For help formulating a clear, useful question, see: How do I ask a good question?\n"
        + "Also, edit your previous questions to improve formatting and clarity."
        + "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\n"
        + "\n For help formulating a clear, useful question, see: How do I ask a good question?\n"
        + "Also, edit your previous questions to improve formatting and clarity."
        + "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\n"
        + "\n For help formulating a clear, useful question, see: How do I ask a good question?\n"
        + "Also, edit your previous questions to improve formatting and clarity.";

    public JTextPaneScroll() {
        initComponents();
        setTextToPane();
    }
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          

    private void initComponents() {

        jspcomp = new javax.swing.JScrollPane();
        comp = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jspcomp.setViewportView(comp);

        getContentPane().add(jspcomp, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, 290, 180));

        pack();
    }// </editor-fold>                        

    private void setTextToPane() {
        try {
            comp.setText(s);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @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 {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JTextPaneScroll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JTextPaneScroll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JTextPaneScroll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JTextPaneScroll.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 JTextPaneScroll().setVisible(true);
            }
        });
    }

// Variables declaration - do not modify                     
    private javax.swing.JTextPane comp;
    private javax.swing.JScrollPane jspcomp;
// End of variables declaration                   
}

this is the sample image of issue

1 个答案:

答案 0 :(得分:3)

JTextPaneScrollable,因此您可以将其设置为您想要的任何首选尺寸,并仍然使用您想要的任何布局。默认情况下,组件会添加到BorderLayout.CENTER的{​​{1}}。您可以调整框架的大小以查看会发生什么。

JFrame

screenshot

textPane = new javax.swing.JTextPane() {
    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return new Dimension(290, 192);
    }
};