gpt4 book ai didi

java - 具有固定宽度的 JTextPane 首选大小

转载 作者:搜寻专家 更新时间:2023-11-01 02:51:46 26 4
gpt4 key购买 nike

我有一个问题,关于如何轻松计算给定固定宽度的 JTextPane 的首选大小/高度。到目前为止我所做的是将我的 JTextPane 放在 JScrollPane 中,每当我更新文本时,我都会更新 JScrollPane 的大小。现在,这很有效(虽然我发现我的代码有点扭曲,但它有效)但是当我添加一个新行时,我更新滚动 Pane 高度的代码需要调用两次:一次是立即,第二次是 调用稍后。我正在寻找一种避免 invokeLater() 的方法。任何事情都可以,包括在组件上分派(dispatch)事件、覆盖 Swing 方法等...

这是说明这一切的片段:

import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Test {

private static MyEventQueue queue;

private static final class MyEventQueue extends EventQueue {

public boolean log = false;

@Override
protected void dispatchEvent(AWTEvent event) {
if (log) {
System.err.println(event.getClass().getName() + " " + event.getSource());
}
super.dispatchEvent(event);
}
}

public static class WrapApp extends JFrame {
JTextPane edit = new JTextPane() {
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}

};
private JScrollPane comp;

protected void updateVPSize() {
updateSize(true);
}

protected void updateSize(boolean repeat) {
edit.setSize(150, 1000);
Dimension size = edit.getPreferredScrollableViewportSize();
System.err.println("Before " + size);
size.width = 150;
comp.setSize(size);
if (repeat) {
queue.log = true;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
queue.log = false;
updateSize(false);
}
});
}
}

public WrapApp() {
super("Forced wrap/no wrap example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
edit.getDocument().addDocumentListener(new DocumentListener() {

@Override
public void removeUpdate(DocumentEvent e) {
updateVPSize();
}

@Override
public void insertUpdate(DocumentEvent e) {
updateVPSize();
}

@Override
public void changedUpdate(DocumentEvent e) {
updateVPSize();
}
});
comp = new JScrollPane(edit) {
@Override
public void setSize(int width, int height) {
super.setSize(width, height);
};
};
comp.setBorder(null);
comp.setLocation(0, 0);
comp.setViewportBorder(null);
comp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
edit.setText("Some long text that needs to be wrapped on several lines.\n\nBut this is not the end of it, it can go on and on and on and on...Some long text that needs to be wrapped on several lines.\n\nBut this is not the end of it, it can go on and on and on and on...");
getContentPane().add(comp);
setSize(300, 700);
setLocationRelativeTo(null);
updateVPSize();
}
}

public static void main(String[] args) {
Toolkit.getDefaultToolkit().getSystemEventQueue().push(queue = new MyEventQueue());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
WrapApp m = new WrapApp();
m.setVisible(true);

}
});
}

}

真的不介意那里剩下的所有东西,我尝试了很多东西

最佳答案

关于java - 具有固定宽度的 JTextPane 首选大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9872466/

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