java - 当文本超出窗口宽度时,JScrollbar 不会激活

标签 java swing jscrollpane jtextpane

这是我的问题:我有一个 JTextPane,我将其添加到了 JScrollpane 中。现在,当我写入此 JTextPane 的 StyledDocument 并且一行比 JTextPane 的宽度长时,它会在新行中显示该行的其余部分,而不是激活滚动条,以便我可以简单地滚动到该行的末尾。

这是我的意思的图像:

enter image description here

在这张图片中,所有文本都应该在一行中,因此底部的水平滚动条应该处于 Activity 状态。

有人知道如何实现这个目标吗?

提前致谢!

<小时/>

编辑1:

这是我目前的代码:

import java.awt.BorderLayout;

public class MainFrame extends JFrame
{
    private ArrayList<RssFeed> rssFeeds = new ArrayList<RssFeed>();

    private JPanel contentPane;
    private JTextPane newsPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        MainFrame frame = new MainFrame();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        });
    }

    /**
     * Create the frame.
     */
    public MainFrame() {
        setMinimumSize(new Dimension(450, 300));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        newsPane = new JTextPane();
        newsPane.setEditable(false);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.add(newsPane);
        scrollPane.setViewportView(newsPane);
        contentPane.add(scrollPane, BorderLayout.CENTER);

        final StyledDocument doc = newsPane.getStyledDocument();

        JPanel updateButton = new JPanel();
        FlowLayout fl_updateButton = (FlowLayout) updateButton.getLayout();
        fl_updateButton.setAlignment(FlowLayout.RIGHT);
        contentPane.add(updateButton, BorderLayout.SOUTH);

        JButton addUrlButton = new JButton("URL hinzuf\u00FCgen...");
        addUrlButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str = JOptionPane.showInputDialog(MainFrame.this, "Bitte eine neue URL eingeben:", "Adresse der URL");
                if (str == null || str.length() == 0) {
                    JOptionPane.showMessageDialog(MainFrame.this, "Keine URL eingegeben.", "Fehler", JOptionPane.ERROR_MESSAGE);
                } else if (!str.endsWith(".xml") && !str.endsWith(".rss")) {
                    System.out.println(str);
                    JOptionPane.showMessageDialog(MainFrame.this, "Die URL muss mit .xml oder .rss enden.", "Fehler", JOptionPane.ERROR_MESSAGE);
                } else {
                    try {
                        RssFeed feed = new RssFeed(str);
                        rssFeeds.add(feed);
                        insertStringToDoc(feed.toString() + "\n", doc, doc.getLength());
                    } catch (XMLStreamException e1) {
                        JOptionPane.showMessageDialog(MainFrame.this, "Fehler beim Parsen des Rss-Feeds.", "Fehler", JOptionPane.ERROR_MESSAGE);
                    } catch (IOException e1) {
                        JOptionPane.showMessageDialog(MainFrame.this, "Fehler beim Lesen der Daten aus der URL.", "Fehler", JOptionPane.ERROR_MESSAGE);
                    } catch (RssException e1) {
                        JOptionPane.showMessageDialog(MainFrame.this, e1.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
    });

    updateButton.add(addUrlButton);

    JButton deactivateSchedulerButton = new JButton("Scheduler deaktivieren");
    updateButton.add(deactivateSchedulerButton);

    JButton btnNewButton_2 = new JButton("Update");
    updateButton.add(btnNewButton_2);
}

private void insertStringToDoc(String str, StyledDocument doc, int offset) {
    //puts text into the given document...
}

}

最佳答案

scrollPane.setViewportView(textPane);

如果滚动是垂直的,这就是您要寻找的。当您书写超出滚动 Pane 的边框时,滚动条将被激活。这是一个例子:

 import javax.swing.JFrame;
 import javax.swing.JScrollPane;
 import javax.swing.JTextPane;

public class Example extends JFrame {
    public Example()    {
        JScrollPane scrollPane = new JScrollPane();
        JTextPane textPane = new JTextPane();

        scrollPane.setViewportView(textPane);
        add(scrollPane);

        setTitle("Example");
        setSize(300, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }
}

关于java - 当文本超出窗口宽度时,JScrollbar 不会激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21075307/

相关文章:

java - mySQL 到 PHP 到 JSON : String Cannot be Converted to JSONObject

java - 如何使用 Netbean 的 GUI Builder 单独调整表列的大小?

java - 在 JSpinner 中禁用数字分组

Java - 打开 JScrollPane,并将 JScrollBar 设置为右侧

Java - JScrollPane 与 JTable 未显示

java - 请解释一下 Spring Boot 中的 Pre-flight Filter 代码

java - JLayeredPane:深度和位置之间的功能区别是什么?

java - 如何在 JGraphx 中制作平行四边形顶点?

java - 将 JScrollPane 添加到 JList

Java 对象序列化性能技巧