2012-10-04 33 views
1

我在網絡代碼中發現:http://java-sl.com/tip_autoreplace_smiles.html我編輯,以我自己的需要,來看看:如何將我自己的代碼插入Netbeans Gui Builder?

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.io.IOException; 
import javax.swing.*; 
import javax.swing.event.DocumentEvent; 
import javax.swing.event.DocumentListener; 
import javax.swing.text.*; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import javax.imageio.ImageIO; 

public class Emotes extends JEditorPane { 

    public Emotes() { 
     super(); 
     this.setEditorKit(new StyledEditorKit()); 
     this.initListener(); 
    } 

    private void replace (String text, StyledDocument doc, ImageIcon icon, int start, String what) throws BadLocationException 
    { 
     int i = text.indexOf(what); 
     while(i >= 0) 
     { 
      final SimpleAttributeSet attrs = new SimpleAttributeSet(doc.getCharacterElement(start+i).getAttributes()); 
      if (StyleConstants.getIcon(attrs) == null) 
      { 
       StyleConstants.setIcon(attrs, icon); 
       doc.remove(start+i, what.length()); 
       doc.insertString(start+i,what, attrs); 
      } 
      i = text.indexOf(what, i+what.length()); 
     } 
    } 

    private void initListener() { 
     getDocument().addDocumentListener(new DocumentListener(){ 
      @Override 
      public void insertUpdate(DocumentEvent event) { 
       final DocumentEvent e=event; 
       SwingUtilities.invokeLater(new Runnable() { 
        @Override 
        public void run() { 
         if (e.getDocument() instanceof StyledDocument) { 
          try { 
           StyledDocument doc=(StyledDocument)e.getDocument(); 
           int start= Utilities.getRowStart(Emotes.this,Math.max(0,e.getOffset()-1)); 
           int end=Utilities.getWordStart(Emotes.this,e.getOffset()+e.getLength()); 
           String text=doc.getText(start, end-start); 


           replace(text, doc, createEmoticon("wink.png"), start, ";)"); 
           replace(text, doc, createEmoticon("wink.png"), start, "<wink>"); 
           replace(text, doc, createEmoticon("sad.png"), start, ":("); 
           replace(text, doc, createEmoticon("sad.png"), start, "<sad>"); 
           replace(text, doc, createEmoticon("smile.png"), start, ":)"); 
           replace(text, doc, createEmoticon("smile.png"), start, "<smile>"); 

          } catch (BadLocationException e1) { 
           e1.printStackTrace(); 
          } 
         } 
        } 
       }); 
      } 
      @Override 
      public void removeUpdate(DocumentEvent e) { 
      } 
      @Override 
      public void changedUpdate(DocumentEvent e) { 
      } 
     }); 
    } 

    static ImageIcon createEmoticon(String icon) 
    { 
     BufferedImage img = null; 
     try 
     { 
      img = ImageIO.read(new File(icon)); 
      Graphics g = img.getGraphics(); 
      ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     }catch (IOException ex) 
     { 
     } 
     return new ImageIcon(img); 
    } 
} 

偉大的工程。但是,我在NetBeans中創建的一個新的GUI(JDialog的窗口)與JEditorPane中,在這裏我想使用此功能的JDialog窗口:

enter image description here

但它不希望在所有的工作。當我鍵入「:)」它不會在圖像中改變。

只是試圖運行代碼:

public class NewJDialog extends javax.swing.JDialog { 

    /** Creates new form NewJDialog */ 
    public NewJDialog(java.awt.Frame parent, boolean modal) { 
     super(parent, modal); 
     initComponents(); 
    } 

    /** This method is called from within the constructor to 
    * initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jScrollPane1 = new javax.swing.JScrollPane(); 
     jEditorPane1 = new Emotes(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

     jScrollPane1.setViewportView(jEditorPane1); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addContainerGap() 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE) 
        .addContainerGap())) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(51, 51, 51) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addContainerGap(20, Short.MAX_VALUE))) 
     ); 

     pack(); 
    }// </editor-fold> 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the dialog */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       NewJDialog dialog = new NewJDialog(new javax.swing.JFrame(), true); 
       dialog.addWindowListener(new java.awt.event.WindowAdapter() { 

        @Override 
        public void windowClosing(java.awt.event.WindowEvent e) { 
         System.exit(0); 
        } 
       }); 
       dialog.setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JEditorPane jEditorPane1; 
    private javax.swing.JScrollPane jScrollPane1; 
    // End of variables declaration 
} 

而且我用的表情是:enter image description hereenter image description hereenter image description here

+0

請註明詳細的一定程度的提問 – Abubakkar

回答

2

的應用程序被編譯成一個.jar文件。正常文件I/O然後不需要(當前目錄根據呼叫而變化)。拍攝的圖像了JAR /類路徑爲:

 img = ImageIO.read(Emotes.class.getResourceAsStream(icon)); 
+1

我測試的代碼,我看到了同樣的問題。首先,createEmoticon(String圖標)總是在「return new ImageIcon(img);」行中給出NullPointerException。然後,我創建了一個名爲「smile.gif」的圖像,將其放入我的文件系統中,並將用於創建文件的路徑更改爲指向我的新圖像。另外,我評論了所有生成與「smile.gif」不同的圖像的行,並且它工作正常。我想她在嘗試使用File實例從.jar中加載圖像時出現錯誤。我認爲你的回答會解決問題 – Roger

+0

@JoopEggen:你說的對,這是路徑問題。它現在正常工作,cheerz!:) – Katie

相關問題