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

时间:2014-01-12 13:39:17

标签: java swing jscrollpane jtextpane

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

这是我的意思的图像:

enter image description here

在这张图片中,所有文字应该只在一行中,因此底部的水平滚动条应该是活动的。

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

提前致谢!


EDIT1:

这是我目前的代码:

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...
}

}

1 个答案:

答案 0 :(得分:1)

scrollPane.setViewportView(textPane);

如果滚动是垂直的,那就是你要找的东西。当您在滚动窗格边框之外书写时,会激活滚动条。这是一个例子:

 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();
    }
}