如何使水平滚动条出现在包含JTextPane组件的JScrollPane中 [英] How to make the horizontal scroll bar appear in a JScrollPane that contains a JTextPane component

查看:181
本文介绍了如何使水平滚动条出现在包含JTextPane组件的JScrollPane中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法显示水平滚动条.我尝试过使用JTextPane.setSize(),JTextPane.setPreferredSize()和JTextPane的无大小方法.我也在使用JScrollPane.setPreferredSize().

I can't make the horizontal scrollbar appear. I've tried using JTextPane.setSize(), JTextPane.setPreferredSize() and no size method for JTextPane. I'm also using JScrollPane.setPreferredSize().

出现垂直滚动条,但没有水平滚动条.

The vertical scroll bar appears, but the horizontal one doesn't.

这里是一个例子:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test {

    private int         textPaneWidth    = 500;
    private int         textPaneHeigth   = 200;
    private int         scrollPaneWidth  = 100;
    private int         scrollPaneHeigth = 100;
    private JTextPane   textPane;
    private JButton     button;
    private JFrame      frame;
    private JScrollPane scrollPane;

    public static void main(String[] args) {

        Test gui = new Test();
        gui.go();

    }

    private void go() {

        frame       = new JFrame("Test");
        button      = new JButton("button");
        textPane    = new JTextPane();
        scrollPane  = new JScrollPane(textPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(new ButtonListener());
        textPane.setFont(new Font("Courier", Font.PLAIN, 12));

        // Sizes:
//        textPane.setSize(textPaneWidth, textPaneHeigth);                             // ???
//        textPane.setPreferredSize(new Dimension(textPaneWidth, textPaneHeigth));     // ???
        scrollPane.setPreferredSize(new Dimension(scrollPaneWidth, scrollPaneHeigth));

        frame.add(button);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);

    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
             textPane.setText("==================================================================== \n" + 
                         "==================================================================== \n" +
                         "==================================================================== \n" +
                         "==================================================================== \n" +
                         "==================================================================== \n");
        }

    }
}

按下按钮时,字符串将添加到JTextPane中.有垂直滚动,但没有水平滚动.

When the button is pressed, the string is added to the JTextPane. There is vertical scrolling, but no horizontal scrolling.

这是我在按下按钮后看到的:

This is what I'm seeing after pressing the button:

我在做什么错了?

推荐答案

似乎您需要扩展JTextPane以避免换行

seems like you need to extend JTextPane to avoid wrapping

https://forums.oracle.com/thread/1210688

class JTextWrapPane extends JTextPane {

    boolean wrapState = true;
    JTextArea j = new JTextArea();

    JTextWrapPane() {
        super();
    }

    public JTextWrapPane(StyledDocument p_oSdLog) {
        super(p_oSdLog);
    }


    public boolean getScrollableTracksViewportWidth() {
        return wrapState;
    }


    public void setLineWrap(boolean wrap) {
        wrapState = wrap;
    }


    public boolean getLineWrap(boolean wrap) {
        return wrapState;
    }
}  

//  instead of  private JTextPane jtp = new JTextPane( sdLog );
    private JTextWrapPane jtp = new JTextWrapPane( sdLog );
//and set LineWrap = (false):
        jtp.setLineWrap(false);

这篇关于如何使水平滚动条出现在包含JTextPane组件的JScrollPane中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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