1

好的,所以我正在尝试制作一个十六进制编辑器,并且我正在尝试制作一个加载 JMenuItem,但它不起作用。JFileChooser OpenDialog 只是没有显示,也没有显示任何错误。

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();
    }
}
4

2 回答 2

2

您永远不会将 JMenuItem 添加到侦听器中,而是创建一个新的。

代替:

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

file.add(load);
于 2010-10-10T20:58:25.097 回答
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);
}
于 2010-10-10T20:55:11.157 回答