2012-02-06 58 views
0

我与Netbeans的文本编辑器,我打开一个文本JTextPane中。如果文字太大,你可以在水平滚动的帮助下阅读它。有没有办法将文本分成24行的页面,例如,每个页面都可以看到而不需要滚动,并使用下一页按钮来改变页面像电子书一样)?让JTextPane中看起来像电子书

+0

是的,这有可能,通知 - >通过你的手写代码,请不要使用内置JComponents -in调色板 – mKorbel 2012-02-06 16:37:53

回答

1

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

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

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

简单又好的+1 – mKorbel 2012-02-08 18:32:24

+0

当我显示我选择我想成为一个JTextPane中做一些其他的事情的页面。有没有办法做到这一点? – drew 2012-02-16 00:18:37

+0

我不知道你在问什么,但我会在9天内回复你,因为这个问题对你来说并不重要。 – camickr 2012-02-16 04:30:32

相关问题