强制JScrollPane中的JEditorPane缩小+重新包装 [英] Force JEditorPane within JScrollPane to shrink + re-wrap

查看:70
本文介绍了强制JScrollPane中的JEditorPane缩小+重新包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JScrollPane扩展其子级JEditorPane时出现问题,但在再次减小其大小时会强制使用水平滚动条(而不是强制JEditorPane重新计算包装).

Having an issue with a JScrollPane expanding its child JEditorPane just fine but forcing horizontal scroll bars when resizing it down again (instead of forcing the JEditorPane to recalculate wrapping).

基本代码流程如下:

JFrame f = new JFrame();
JEditorPane jep = new JEditorPane();
JScrollPane jsp = new JScrollPane(jep);
f.add(jsp);

推荐答案

这是一个hack,但是我能找到的最好方法(不使用丑陋的ScrollPaneManager s)是在JScrollPane上实现ComponentListener每次调整子组件的大小时,都要重新调整大小.

It's a hack, but the best way I could find (without using ugly ScrollPaneManagers) was to implement a ComponentListener on the JScrollPane to resize the child component whenever it was resized.

jsp.addComponentListener(new ComponentListener() {
    @Override
    public void componentShown(ComponentEvent e) {}

    @Override
    public void componentResized(ComponentEvent e) {
        Dimension jspSize = ((JScrollPane)e.getComponent()).getViewport().getSize();
        jep.setBounds(0, 0, jspSize.width, jspSize.height);
    }

    @Override
    public void componentMoved(ComponentEvent e) {}

    @Override
    public void componentHidden(ComponentEvent e) {}
});

这篇关于强制JScrollPane中的JEditorPane缩小+重新包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆