2012-01-31 3 views
1

전에이 문제에 대해 질문했지만 SCCE로 새 스레드를 시작하기로 결정했지만 성공적으로 컴파일되지만 이미지는 삽입되지 않습니다. 누구나 왜이 문제를 해결할 수 있습니까? 고마워, 크리스. 나는 이미지가 발견되지 않기 때문에 코드가 실패 추측 만 할 수 있습니다,이 작업 예에JTextPane에 이미지 삽입 오류

package mathnotesplus; 

import java.awt.Dimension; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.SwingUtilities; 
import javax.swing.JPanel; 
import javax.swing.JTextPane; 

import javax.swing.JFrame; 

import javax.swing.JTree; 
import javax.swing.tree.DefaultMutableTreeNode; 

import javax.swing.*; 
import javax.swing.tree.*; 
import javax.swing.event.*; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import javax.swing.text.*; 
/** 
* 
* @author ChrisCates 
*/ 
public class MathNotesPlus extends JFrame implements TreeSelectionListener { 

    /** 
    * @param args the command line arguments 
    */ 
    public MathNotesPlus() { 
     setTitle("Math Notes Plus"); 
     setSize(800, 600); 
     initUI(); 
    } 


    JPanel panel; 
    JTextPane textpane; 
    JTree navigation; 
    StyledDocument document; 

    public void initUI(){ 
     //The panel. 
     panel = new JPanel(); 
     //Textpane and JTree 
     textpane = new JTextPane(); 
     navigation = new JTree(); 
     navigation.addTreeSelectionListener(this); 
     //Preferred Resolution Size 
     navigation.setPreferredSize(new Dimension(100, 600)); 
     textpane.setPreferredSize(new Dimension(700, 600)); 

     //Insertion of image into the document. 
     try { 
      document = (StyledDocument)textpane.getDocument(); 
      Style style = document.addStyle("StyleName", null); 
      StyleConstants.setIcon(style, new ImageIcon("sigma.png")); 
      document.insertString(document.getLength(), "ignored text", style); 
     } catch (BadLocationException e){ 
      System.err.println("ERROR"); 
     } 

     //Putting everything into the program. 
     panel.add(navigation); 
     panel.add(textpane);   
     add(panel); 
     pack(); 


    } 

    public static void main(String[] args) { 
     // TODO code application logic here 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      MathNotesPlus app = new MathNotesPlus(); 
       app.setVisible(true); 
      } 
     }); 
    } 

    @Override 
    public void valueChanged(TreeSelectionEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 
} 

답변

2

자료 (코드의 아주 가까운 변형) 작동 : 여기

는 전체 코드입니다 .

import java.awt.Dimension; 
import java.awt.Image; 
import javax.swing.*; 
import javax.swing.event.*; 
import java.awt.event.*; 
import javax.swing.text.*; 
import java.net.URL; 
import javax.imageio.ImageIO; 

public class MathNotesPlus extends JFrame { 

    JPanel panel; 
    JTextPane textpane; 
    StyledDocument document; 

    public MathNotesPlus() { 
     setTitle("Math Notes Plus"); 
     setSize(800, 600); 
     initUI(); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    } 

    public void initUI(){ 
     //The panel. 
     panel = new JPanel(); 
     //Textpane 
     textpane = new JTextPane(); 
     textpane.setPreferredSize(new Dimension(700, 600)); 

     //Insertion of image into the document. 
     try { 
      Image image = ImageIO.read(new URL(
       "http://pscode.org/media/stromlo1.jpg")); 

      document = (StyledDocument)textpane.getDocument(); 
      Style style = document.addStyle("StyleName", null); 
      StyleConstants.setIcon(style, new ImageIcon(image)); 
      document.insertString(document.getLength(), "ignored text", style); 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 

     //Putting everything into the program. 
     panel.add(textpane); 
     add(panel); 
     pack(); 
    } 

    public static void main(String[] args) { 
     // TODO code application logic here 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      MathNotesPlus app = new MathNotesPlus(); 
       app.setVisible(true); 
      } 
     }); 
    } 
} 
+0

BTW - JTree가 SSCCE에 중복 된 것처럼 보입니다. 내가 놓친 것 (예외를 던지는 것 외에)을 했습니까? –