JFileChooser.showOpenDialog 未打开,并且没有抛出错误?

发布于 2024-09-26 20:46:01 字数 2783 浏览 5 评论 0原文

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

Okay, so I'm trying to make a hex editor, and I'm trying to make a load JMenuItem, but it's not working. The JFileChooser OpenDialog just doesn't show up, and no errors are being shown.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花之痕靓丽 2024-10-03 20:46:01

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

替换:

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

file.add(load);

You never add the JMenuItem with the listener, instead you create a new one.

Replace:

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

with

file.add(load);
旧话新听 2024-10-03 20:46:01

一切都在事件调度线程中完成吗?如果不是这样,你会得到像这样的小错误。

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

Is everything being done in the event dispatch thread? You'll get small errors like this if it isn't.

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

Also, look at http://www.fifesoft.com/hexeditor/ for a neat hex editor component under a BSD license :)

import javax.swing.SwingUtilities;

public static void main(String[] args){
    Runnable r = new Runnable() {
       public void run() {
            HexEditor app = new HexEditor();
       }
    };
    SwingUtilities.invokeLater(r);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文