【问题标题】:Make a JtextPane looks like eBook使 JtextPane 看起来像电子书
【发布时间】:2012-02-06 13:35:56
【问题描述】:

我有一个带有 Netbeans 的文本编辑器,我在其中将文本加载到 JtextPane。如果文本太大,你可以在水平滚动的帮助下阅读它。有没有办法将文本分成 24 行的页面,例如,每个页面都可以在不滚动的情况下可见,并使用下一页按钮来更改页面(像电子书一样)?

【问题讨论】:

  • 是的,可能,注意 --> 亲手编写代码,不要使用内置调色板中的 JComponents

标签: java swing paging jtextpane


【解决方案1】:

使用 JTextArea 更容易做到这一点,因为您可以轻松地指定每次滚动到新页面时显示的行数。

基本的解决方案是在滚动窗格中添加一个文本区域,然后隐藏滚动条。然后,您可以使用垂直滚动条的默认操作为您进行滚动。下面的代码使用来自Action Map Action 博客条目的代码轻松创建可以添加到 JButton 的 Action:

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

public class TextAreaScroll extends JPanel
{
    private JTextArea textArea;

    public TextAreaScroll()
    {
        setLayout( new BorderLayout() );

        textArea = new JTextArea(10, 80);
        textArea.setEditable( false );

        JScrollPane scrollPane = new JScrollPane( textArea );
        scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_NEVER );
        add(scrollPane);

        JButton load = new JButton("Load TextAreaScroll.java");
        load.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileReader reader = new FileReader( "TextAreaScroll.java" );
                    BufferedReader br = new BufferedReader(reader);
                    textArea.read( br, null );
                    br.close();
                }
                catch(Exception e2) { System.out.println(e2); }
            }
        });
        add(load, BorderLayout.NORTH);

        //  Add buttons to do the scrolling

        JScrollBar vertical = scrollPane.getVerticalScrollBar();

        Action nextPage = new ActionMapAction("Next Page", vertical, "positiveBlockIncrement");
        nextPage.putValue(AbstractAction.MNEMONIC_KEY, KeyEvent.VK_N);
        JButton nextButton = new JButton(nextPage);

        Action previousPage = new ActionMapAction("Previous Page", vertical, "negativeBlockIncrement");
        previousPage.putValue(AbstractAction.MNEMONIC_KEY, KeyEvent.VK_N);
        JButton previousButton = new JButton(previousPage);

        JPanel south = new JPanel();
        add(south, BorderLayout.SOUTH);
        south.add( previousButton );
        south.add( nextButton );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TextAreaScroll());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

【讨论】:

  • 当我显示我选择的页面时,我想成为一个 Jtextpane 来做一些其他的事情。有没有办法做到这一点?
  • 我不知道你在问什么,但我会在 9 天内回复你,因为这个问题对你来说并不重要。
  • 我的意思是我希望所有页面都显示在 Jtextpane 中。有没有办法让带有 Paging[1] 的字符串 Paging 占据第一页,依此类推..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
相关资源
最近更新 更多