JFileChooser.showOpenDialog不能打开,并且没有错误被抛出? [英] JFileChooser.showOpenDialog not opening, and no error being thrown?

查看:239
本文介绍了JFileChooser.showOpenDialog不能打开,并且没有错误被抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在尝试编写一个十六进制编辑器,并且正在尝试创建一个负载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;

公共类HexEditor扩展JFrame {
JTextArea textArea;
JFileChooser chooser; // = new JFileChooser();
FileInputStream fin;
JMenuBar menuBar;
JMenu文件;
JMenuItem加载;

public HexEditor(){
super(Cypri的java十六进制编辑器);

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。 ()< 2)
s =0+ Integer.toHexString(ch);

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

else {
strContent.append(+ s.toUpperCase()+\\\
);
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 \\\
);
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();



$ div $解析方案

你不要添加JMenuItem和监听器,而是创建一个新的。



替换:

  file.add(new JMenuItem(Load)); 

with

  file.add(负载); 


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

解决方案

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

Replace:

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

with

file.add(load);

这篇关于JFileChooser.showOpenDialog不能打开,并且没有错误被抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆