gpt4 book ai didi

java - JScrollPane 和 JViewport 最大尺寸小于内容的滚动限制

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:57 30 4
gpt4 key购买 nike

我有一个 JFrame,其中包含一个 JScrollPane,其中包含一个 JPanelJPanel 包含一堆 JTextArea。我正在向其中加载大量文本(大约 8k-10k 个字符)。

布局工作正常,但滚动有点滞后。

真正的问题是 JPanelJScrollPaneJViewport 似乎有 32767 大小的硬限制,所以当任何 JTextArea 比那个高,它不能再滚动以显示文本的最后 1/3。

您可以在下面看到该问题的一个最小示例。我使用了 NetBeans JFrame 设计器,因此它可能有点冗长,但我对默认设置所做的唯一更改是 JTextArea 的直接子级JPanel、滚动条策略和稍大的字体:

public class NewJFrame extends javax.swing.JFrame {

/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
jTextArea1 = new javax.swing.JTextArea();
jTextArea2 = new javax.swing.JTextArea();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N
jTextArea1.setRows(5);

jTextArea2.setColumns(20);
jTextArea2.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N
jTextArea2.setRows(5);
jTextArea2.setText("Some long text...");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jTextArea1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextArea2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextArea1, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
.addComponent(jTextArea2))
.addGap(0, 0, 0))
);

jScrollPane1.setViewportView(jPanel1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
);

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

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
final NewJFrame f = new NewJFrame();
/* 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 {
for(javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
if("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}catch(ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}catch(InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}catch(IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
f.setVisible(true);
}
});
StringBuilder txt = new StringBuilder();
for(int i=0; i<10000; i++)
txt.append(i).append("\n");
f.jTextArea1.setText(txt.toString());
txt = new StringBuilder();
txt.append("JTextArea height: ").append(f.jTextArea1.getHeight()).append('\n');
txt.append("JTextArea rows: ").append(f.jTextArea1.getRows()).append('\n');
txt.append("JScrollPane height:").append(f.jScrollPane1.getHeight()).append('\n');
txt.append("JViewport height:").append(f.jScrollPane1.getViewport().getHeight()).append('\n');
txt.append("JPanel height:").append(f.jPanel1.getHeight()).append('\n');
f.jTextArea2.setText(txt.toString());
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
// End of variables declaration
}

如果您运行它并滚动到底部,您会看到计数达到 10000,但它只达到 1637,并且您可以看到下一行的顶部像素几乎没有出现。

我已经在 JPanelJScrollPane 及其 上尝试了 setMaximumSizesetSize JViewport 但没有任何改变。我也有点困惑,即使有 10k 行文本,其中一些可以滚动到足以查看,getRows()getSize() 方法返回原始值。

当我想要一个大于 32767 的可滚动 JTextArea 时,处理这种情况的正确方法是什么?

最佳答案

您的示例未正确同步,因为它更新了 initial thread 上的 jTextArea2 .注意 JTextArea#setText()不再是线程安全的。下面的示例调用 EditorKit#read(),如建议的那样 here ,加载相同的 27 MB,176 K 行文件检查here .这需要几秒钟,大约是 JTable 方法的两倍,但滚动是可比的。

import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
* @see https://stackoverflow.com/a/25691384/230513
*/
public class Test {

private static final String NAME = "/var/log/install.log";

private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea text = new JTextArea(24, 32);
try {
text.read(new BufferedReader(new FileReader(NAME)), null);
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
f.add(new JScrollPane(text));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new Test().display();
});
}
}

关于java - JScrollPane 和 JViewport 最大尺寸小于内容的滚动限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25674947/

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