【问题标题】:JTextPane text collapsing on scroll in Mac osJTextPane 文本在 Mac os 中滚动时折叠
【发布时间】:2016-09-29 09:19:11
【问题描述】:

我正在创建一个简单的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                   
}

【问题讨论】:

  • getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); AbsoluteLayout 实际上只是一种表示“无布局”的奇特方式。 Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
  • 我也尝试过使用 BorderLayout,也尝试过设置样式文档。但仍然面临同样的问题。
  • “用 BorderLayout 也试过了” 我准备帮助修复那个版本。它在哪里?
  • 我的意思是当我遇到这个问题时,我认为可能是因为布局管理器,所以我设置了布局。我在面板中添加了 jscrollpane 并为该面板设置了 BorderLayout。但它不起作用。然后我采取了不同的方法,我为 jtextpane 设置样式文档并使用“doc.insertString(0, content, attribute);”在文档中插入字符串,我再次遇到了这个文本折叠问题。
  • “我的意思是..” 的意思是如果null布局中断,我无法提供任何东西.我非常关心使用BorderLayout 的版本,足以尝试帮助修复它。

标签: java macos swing operating-system jtextpane


【解决方案1】:

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

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

import java.awt.Dimension;

public class JTextPaneScroll extends javax.swing.JFrame {

    private javax.swing.JTextPane textPane;
    private javax.swing.JScrollPane scrollPane;

    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"
        + "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.\n"
        + "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\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.\n"
        + "Wait! Some of your past questions have not been well-received, and you're in danger of being blocked from asking any more.\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.\n";

    public JTextPaneScroll() {
        initComponents();
        setTextToPane();
    }

    private void initComponents() {
        scrollPane = new javax.swing.JScrollPane();
        textPane = new javax.swing.JTextPane() {
            @Override
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(290, 192);
            }
        };
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        scrollPane.setViewportView(textPane);
        add(scrollPane);
        pack();
    }

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

    public static void main(String args[]) {
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JTextPaneScroll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JTextPaneScroll().setVisible(true);
            }
        });
    }
}

【讨论】:

  • 在滚动时仍然面临同样的问题。 @Catalina Island,问题出现在垂直快速滚动上。即使使用“getPreferredScrollableViewportSize()”滚动也不起作用,不显示文本折叠问题。请在 Mac OS 10.11 或更高版本上运行此程序并快速上下滚动,您会看到问题。
  • 不知何故我想出了解决方案。我添加了 scrollPane.addMouseWheelListener(new MouseWheelListener() { @Override public void mouseWheelMoved(MouseWheelEvent mwe) { comp.updateUI(); } });现在快速滚动我没有面临文本折叠问题。我认为必须有其他更好的解决方案而不是更新 UI,因为它可能会损害性能。但目前它解决了我在 mac 中的问题。
  • @user3098231:我同意updateUI() 不是必需的。在我的装有 10.11.5 和 Java 8 的 Mac 上,鼠标滚轮可以在没有监听器的情况下工作。用您的新minimal reproducible example 更新您的问题,我会试一试。
  • 我上面粘贴的程序是最小且完整的,如果你运行你可以在 mac 上看到问题,当你向上和向下滚动一点时。
  • 是的,我也看到了。除了更改布局外,我不知道有更好的方法来解决它。我的示例使用框架的默认值,BorderLayout.CENTER
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-05
  • 2016-03-06
  • 1970-01-01
  • 2015-11-17
相关资源
最近更新 更多