2012-02-06 10 views
0

私はNetbeansを持つテキストエディタを持っています。ここで、テキストをJtextPaneにロードします。テキストが大きすぎると、横スクロールの助けを借りて読むことができます。たとえば、24行のページにテキストを分割し、スクロールせずにすべてのページを表示し、ページを変更するために次のページボタンを使用する方法はありますか? eBookのように)?JtextPaneをeBookのようにする

+0

はいている可能性が、予告 - >構築されたからJComponentのを使用していない、あなたの手でコードを書きました-in palette – mKorbel

答えて

1

新しいページにスクロールするたびに表示する行数を簡単に指定できるため、JTextAreaを使用する方が簡単です。

基本的な解決策は、スクロールバーを非表示にするよりもスクロールペインにテキスト領域を追加することです。次に、垂直スクロールバーのデフォルトのアクションを使用して、スクロールを行います。以下のコードは、簡単にあなたがのJButtonに追加できるアクションを作成するためにAction Map 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(); 
      } 
     }); 
    } 
} 
+0

シンプルで素敵な+1 – mKorbel

+0

私はページを表示するときに、私はJtextpaneになりたいと思っています。それを行う方法はありますか? – drew

+0

私はあなたが何を求めているのか分かりませんが、質問はあなたにとってそれほど重要ではないので、9日後に私はあなたに戻ってきます。 – camickr

関連する問題