0

Java/Swing でテキスト エディタ タイプのアプリケーションを作成しようとしています。FileChooser が動作しており、ファイルの内容をコンソールに出力できます。ファイルを JEditorPane にロードしたい

setText() を呼び出すと、テキストの値が更新されます (getText の結果を出力できますが、実際の EditorPane は更新されません)。カプセル化された JScrollPane である JEditorPane で repaint/revalidate を呼び出してみましたが、テキストを setText に送信したものに更新するものは何もありません。

何か不足していますか?

PS JEditorPane は JScrollPane 内にラップされ、文字列を JEditorPane の setText メソッドに渡すメソッドが mainEditor にあります。

      if (r == JFileChooser.APPROVE_OPTION) 
      {
          FileInputStream fis;
          BufferedReader br;
          try
            {
                fis = new FileInputStream( 
                      chooser.getSelectedFile() ) ;
                br  = new BufferedReader( 
                      new InputStreamReader( fis ) ) ;
                String read ;
                StringBuffer text = new StringBuffer() ;
                while( ( read = br.readLine() ) != null ) 
                {
                   text.append( read ).append( "\n" ) ;
                }
                Main.frame.mainEditor.setText( text.toString() ) ;
                Main.frame.mainEditor.revalidate();
            }
            catch( IOException e1 ) 
            {
                JOptionPane.showMessageDialog( this , 
                    "Error in File Operation" ,
                    "Error in File Operation" , 
                    JOptionPane.INFORMATION_MESSAGE) ;
            }             
      }
4

2 に答える 2

4

Stringを参照に形成し、それをthen callFileに変換します。 URLsetPage(URL)

例については、こちらを参照してください。

于 2012-03-28T11:24:24.880 に答える
3

JTextComponents ファミリの InputStream の組み込みメソッドを使用する

JTextCompoents#read();

JTextComponents#write();

于 2012-03-28T11:29:04.490 に答える