【发布时间】: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