2010-10-10 125 views
1

好吧,所以我試圖做一個十六進制編輯器,我試圖做一個負載JMenuItem,但它不工作。 JFileChooser OpenDialog不顯示,並且沒有顯示錯誤。JFileChooser.showOpenDialog沒有打開,並且沒有錯誤被拋出?

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 
import java.util.Vector; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStream; 

public class HexEditor extends JFrame{ 
    JTextArea textArea; 
    JFileChooser chooser;// = new JFileChooser(); 
    FileInputStream fin; 
    JMenuBar menuBar; 
    JMenu file; 
     JMenuItem load; 

    public HexEditor(){ 
     super("Cypri's java hex editor"); 

     chooser = new JFileChooser(); 

     load = new JMenuItem("Load"); 
      load.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent event) { 


        try{ 

         openFile(); 
         fin = new FileInputStream(chooser.getSelectedFile()); 

         int ch; 
         StringBuffer strContent = new StringBuffer(""); 

         for(int i = 0; (ch = fin.read()) != -1; i++){ 
          String s = Integer.toHexString(ch); 

          if(s.length() < 2) 
           s = "0" + Integer.toHexString(ch); 

          if(i < 10) 
           strContent.append(" " + s.toUpperCase()); 

          else{ 
           strContent.append(" " + s.toUpperCase() + "\n"); 
           i = 0; 
          } 
         } 

         textArea.setText(strContent.toString()); 
         //textArea.setWrapStyleWord(true); 
         //textArea.setColumns(50); 
         //textArea.setRows(50); 
        } 

        catch(Exception e){ 
         e.printStackTrace(); 
        } 
       } 
      }); 

     file = new JMenu("File"); 
     file.add(new JMenuItem("Load")); 

     menuBar = new JMenuBar(); 
     menuBar.add(file); 

     textArea = new JTextArea(); 
     textArea.setSize(300,300); 
     textArea.setText("Hello\n"); 
     textArea.append(" world!"); 




     setSize(640, 480); 
     //getContentPane().setBackground(Color.); 
     getContentPane().setLayout(new BorderLayout()); 
     getContentPane().add(BorderLayout.NORTH, menuBar); 
     getContentPane().add(BorderLayout.WEST, textArea); 
     pack(); 
     setVisible(true); 
    } 

    public void openFile(){ 
     chooser.showOpenDialog(this); 
    } 

    public static void main(String[] args){ 
     HexEditor app = new HexEditor(); 
    } 
} 

回答

2

你永遠不與添加的JMenuItem聽衆,而是創建一個新的。

替換:

file.add(new JMenuItem("Load")); 

file.add(load); 
+0

這工作,我會盡快除了這個答案,只要我可以。謝謝。 :) – William 2010-10-10 21:03:18

+0

@William - 沒問題:) – willcodejavaforfood 2010-10-10 21:04:36

1

一切都在事件調度線程中完成嗎?如果不是,你會得到這樣的小錯誤。

http://download.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html

http://www.jguru.com/faq/view.jsp?EID=8963

而且,看http://www.fifesoft.com/hexeditor/一個整潔的十六進制編輯器組件在BSD許可下:)

import javax.swing.SwingUtilities; 

public static void main(String[] args){ 
    Runnable r = new Runnable() { 
     public void run() { 
      HexEditor app = new HexEditor(); 
     } 
    }; 
    SwingUtilities.invokeLater(r); 
} 
+1

無關,與他的問題:) – willcodejavaforfood 2010-10-10 20:58:41

+0

我不會說。他直接在主線上做東西,這是絕對的災難。 – 2010-10-10 20:59:58

+0

我會這麼說,但你是對的。你真的應該從美國東部時間開始。 – willcodejavaforfood 2010-10-10 21:08:01

相關問題