gpt4 book ai didi

java - JTextPane 文本在 Mac 操作系统中滚动时折叠

转载 作者:行者123 更新时间:2023-12-01 21:30:01 26 4
gpt4 key购买 nike

我正在创建一个简单的 JTextPane 并在视口(viewport)设置后设置长文本,但是当我在 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

最佳答案

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

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

关于java - JTextPane 文本在 Mac 操作系统中滚动时折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37544381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com