繁体   English   中英

JFileChooser.showOpenDialog无法打开,并且没有引发错误?

[英]JFileChooser.showOpenDialog not opening, and no error being thrown?

好的,所以我试图创建一个十六进制编辑器,并且试图加载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();
    }
}

永远不要将JMenuItem与侦听器一起添加,而是创建一个新的JMenuItem。

更换:

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

file.add(load);

事件分发线程中的所有事情都完成了吗? 如果不是这样,您会收到类似这样的小错误。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM