2016-05-31 6 views
0

Je crée un JTextPane simple et en définissant un texte long après le paramètre viewport, mais lorsque j'exécute un programme sous Mac OS 10.11.5, mes lignes de texte JTextPane se replient les unes avec les autres. Quand je défile lentement, ça n'arrive pas, mais quand je défile un peu vite, ça commence à s'effondrer. Ce problème ne se produit pas dans Windows. Voici mon exemple de code source:JTextPane texte se repliant sur défilement dans Mac OS

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

+0

'getContentPane() setLayout (nouveau org.netbeans.lib.awtextra.AbsoluteLayout());.' 'Le AbsoluteLayout' est vraiment juste une façon de dire « non disposition'. Les interfaces graphiques Java doivent fonctionner sur différents systèmes d'exploitation, taille d'écran, résolution d'écran, etc., en utilisant différents PLAF dans différents environnements locaux. En tant que tels, ils ne sont pas propices à la mise en page pixel parfait. Utilisez plutôt des gestionnaires de disposition, ou [des combinaisons de ceux-ci] (http://stackoverflow.com/a/5630271/418556) avec un remplissage de mise en page et des bordures pour [espace blanc] (http://stackoverflow.com/a/17874718/ 418556). –

+0

j'ai essayé cela aussi avec BorderLayout, et ai également essayé avec placer le document stylé. mais toujours confronté au même problème. – user3098231

+0

* "essayé aussi avec BorderLayout" * Je serais prêt à aider à corriger cette version. Où est-ce? –

Répondre

3

JTextPane est Scrollable, de sorte que vous pouvez faire une taille préférée que vous voulez et toujours utiliser une mise en page que vous voulez. Les composants sont ajoutés au BorderLayout.CENTER du JFrame par défaut. Vous pouvez redimensionner le cadre pour voir ce qui se passe.

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

screenshot

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); 
      } 
     }); 
    } 
} 
+0

toujours confronté au même problème sur le défilement. @Catalina Island, Le problème survient lors d'un défilement rapide vertical. Même si vous utilisez "getPreferredScrollableViewportSize()", le défilement ne fonctionne pas si le problème d'effondrement du texte ne s'affiche pas. S'il vous plaît exécuter ce programme sur Mac OS 10.11 ou plus tard et faites défiler vers le haut et vers le bas peu rapide, vous verrez le problème. – user3098231

+0

en quelque sorte j'ai compris la solution. J'ai ajouté scrollPane.addMouseWheelListener (nouveau MouseWheelListener() { @Override mouseWheelMoved public void (MouseWheelEvent MWe) { comp.updateUI();} }); Maintenant, maintenant sur le défilement rapide, je ne suis pas confronté à un problème d'effondrement de texte. Je pense qu'il doit y avoir une autre meilleure solution au lieu de mettre à jour l'interface utilisateur car cela peut nuire aux performances. mais actuellement, il a résolu mon problème dans mac. – user3098231

+0

@ user3098231: Je suis d'accord que 'updateUI()' ne devrait pas être nécessaire. Sur mon Mac avec 10.11.5 et JavA 8, la roulette de la souris fonctionne sans écouteur. Mettez à jour votre question avec votre nouveau [mcve], et je vais essayer. –