Java 使JtextPane看起来像电子书
我有一个带有Netbeans的文本编辑器,在这里我将文本加载到JtextPane。如果文本太大,你可以借助水平滚动来阅读。例如,有没有办法将文本分成24行的页面,这样每一页都可以在不滚动的情况下看到,并使用下一页按钮来更改页面(就像电子书一样) 使用JTextArea更容易做到这一点,因为您可以轻松指定每次滚动到新页面时要显示的行数 基本解决方案是在滚动窗格中添加一个文本区域,然后隐藏滚动条。然后,您可以使用垂直滚动条的默认操作来执行滚动。下面的代码使用博客条目中的代码轻松创建可以添加到JButton的操作:Java 使JtextPane看起来像电子书,java,swing,paging,jtextpane,Java,Swing,Paging,Jtextpane,我有一个带有Netbeans的文本编辑器,在这里我将文本加载到JtextPane。如果文本太大,你可以借助水平滚动来阅读。例如,有没有办法将文本分成24行的页面,这样每一页都可以在不滚动的情况下看到,并使用下一页按钮来更改页面(就像电子书一样) 使用JTextArea更容易做到这一点,因为您可以轻松指定每次滚动到新页面时要显示的行数 基本解决方案是在滚动窗格中添加一个文本区域,然后隐藏滚动条。然后,您可以使用垂直滚动条的默认操作来执行滚动。下面的代码使用博客条目中的代码轻松创建可以添加到JBut
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();
}
});
}
}
是的,这是可能的,注意-->手工编写的代码,不要使用内置选项板中的JComponents当我显示我选择的页面时,我想成为一个Jtextpane来做其他事情。有办法吗?我不知道你在问什么,但我会在9天后给你回复,因为这个问题对你来说并不重要。我的意思是我希望所有页面都显示在Jtextpane中。有没有办法让字符串分页,分页[1]占据第一页,依此类推。。