选择JMenu时,JFileChooser不会出现

时间:2015-02-08 07:26:30

标签: java swing jfilechooser jmenu

我是Java的新手,所以我真的需要帮助。我希望我的JMenu仅在我点击它时才使用文件选择器打开文件,但发生的事情是它没有出现。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Myprog extends JFrame {
JMenuBar hard= new JMenuBar();
JMenu file = new JMenu("FILE");
JMenu open = new JMenu("Open");
JMenu histo = new JMenu("History");
JMenuItem exit = new JMenuItem("Exit");
JMenu view = new JMenu("VIEW");
JMenu descript = new JMenu("Description");
JMenu spm = new JMenu("Specs/Model/Version");
JMenu extra = new JMenu("EXTRAS");
JMenu trans = new JMenu("Transaction");
JMenu calcu = new JMenu("Calculator");
JMenu qoutes = new JMenu("Quotes");
JMenuItem game = new JMenuItem("Games");
JMenu about = new JMenu("ABOUT");
JMenuItem java = new JMenuItem("JAVA");
JMenuItem sirjet = new JMenuItem("References");
JMenuItem akoto = new JMenuItem("MyProfile");
JMenuItem mouse = new JMenuItem("Mouse");
JMenuItem cpu = new JMenuItem("CPU");
JMenuItem cam = new JMenuItem("Camera");
JMenuItem key = new JMenuItem("Keyboard");
JMenuItem mem = new JMenuItem("Memory");
JMenuItem mic = new JMenuItem("Microphone");
JMenuItem print= new JMenuItem("Printer");
JMenuItem scan = new JMenuItem("Scanner");
JMenuItem speak = new JMenuItem("Speaker");
JMenuItem mon = new JMenuItem("Monitor");
JFileChooser choosyAko  = new JFileChooser(".");


JLabel direct = new JLabel(" ");
JLabel filename = new JLabel(" ");


public Myprog(){
super("My Program(Hardware)");
setVisible(true);
setBounds(250,250,700,700);
setBackground(Color.GREEN);
direct.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);


setJMenuBar(hard);
hard.add(file);
file.setMnemonic('F');
file.add(open);
open.setMnemonic('O');
file.add(histo);
histo.setMnemonic('H');
histo.add(cam);
histo.add(cpu);
histo.add(key);
histo.add(mem);
histo.add(mic);
histo.add(mon);
histo.add(mouse);
histo.add(print);
histo.add(scan);
histo.add(speak);
file.add(exit);
exit.setMnemonic('X');
hard.add(view);
view.setMnemonic('V');
view.add(descript);
descript.setMnemonic('D');
view.add(spm);
spm.setMnemonic('S');
hard.add(extra);
extra.setMnemonic('E');
extra.add(trans);
trans.setMnemonic('T');
extra.add(calcu);
calcu.setMnemonic('C');
extra.add(qoutes);
qoutes.setMnemonic('Q');
extra.add(game);
game.setMnemonic('G');
hard.add(about);
about.setMnemonic('B');
about.add(java);
java.setMnemonic('J');
about.add(sirjet);
sirjet.setMnemonic('N');
about.add(akoto);
add(direct, BorderLayout.NORTH);
add(filename, BorderLayout.SOUTH);
add(choosyAko,BorderLayout.CENTER);

open.addActionListener(new open());
exit.addActionListener(new exit());
}
class open implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
        {
    JFileChooser choosyAko  = new JFileChooser(".");
    JLabel direct = new JLabel(" ");
    JLabel filename = new JLabel(" ");
    choosyAko.setControlButtonsAreShown(false);
        }
    }
class exit implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
        {
    System.exit(0);
        }
    }
public static void main(String []args)
{ 
Myprog frame = new Myprog();
}
}

2 个答案:

答案 0 :(得分:0)

您已初始化JFileChooser,但您没有显示该对话框。您必须调用适当的方法来显示:

  • int returnVal = chooser.showOpenDialog(parent);
  • int returnVal = chooser.showSaveDialog(parent);

答案 1 :(得分:0)

在选择打开菜单后,我稍微更改了文件以显示文件打开对话框。 你用什么开发?试试NetBeans吧。使用IDE创建有用的用户界面要容易得多。

以下是我根据您的代码所做的事情:

package hw;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class Myprog extends JFrame {

    JMenuBar hard = new JMenuBar();
    JMenu file = new JMenu("FILE");
    JMenuItem open = new JMenuItem("Open");
    JMenu histo = new JMenu("History");
    JMenuItem exit = new JMenuItem("Exit");
    JMenu view = new JMenu("VIEW");
    JMenu descript = new JMenu("Description");
    JMenu spm = new JMenu("Specs/Model/Version");
    JMenu extra = new JMenu("EXTRAS");
    JMenu trans = new JMenu("Transaction");
    JMenu calcu = new JMenu("Calculator");
    JMenu qoutes = new JMenu("Quotes");
    JMenuItem game = new JMenuItem("Games");
    JMenu about = new JMenu("ABOUT");
    JMenuItem java = new JMenuItem("JAVA");
    JMenuItem sirjet = new JMenuItem("References");
    JMenuItem akoto = new JMenuItem("MyProfile");
    JMenuItem mouse = new JMenuItem("Mouse");
    JMenuItem cpu = new JMenuItem("CPU");
    JMenuItem cam = new JMenuItem("Camera");
    JMenuItem key = new JMenuItem("Keyboard");
    JMenuItem mem = new JMenuItem("Memory");
    JMenuItem mic = new JMenuItem("Microphone");
    JMenuItem print = new JMenuItem("Printer");
    JMenuItem scan = new JMenuItem("Scanner");
    JMenuItem speak = new JMenuItem("Speaker");
    JMenuItem mon = new JMenuItem("Monitor");
    JFileChooser choosyAko = new JFileChooser(".");

    JLabel direct = new JLabel(" ");
    JLabel filename = new JLabel(" ");

    public Myprog() {
        super("My Program(Hardware)");
        setBounds(250, 250, 700, 700);
        setBackground(Color.GREEN);
        direct.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
        setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setJMenuBar(hard);
        hard.add(file);
        file.setMnemonic('F');
        file.add(open);
        open.setMnemonic('O');
        file.add(histo);
        histo.setMnemonic('H');
        histo.add(cam);
        histo.add(cpu);
        histo.add(key);
        histo.add(mem);
        histo.add(mic);
        histo.add(mon);
        histo.add(mouse);
        histo.add(print);
        histo.add(scan);
        histo.add(speak);
        file.add(exit);
        exit.setMnemonic('X');
        hard.add(view);
        view.setMnemonic('V');
        view.add(descript);
        descript.setMnemonic('D');
        view.add(spm);
        spm.setMnemonic('S');
        hard.add(extra);
        extra.setMnemonic('E');
        extra.add(trans);
        trans.setMnemonic('T');
        extra.add(calcu);
        calcu.setMnemonic('C');
        extra.add(qoutes);
        qoutes.setMnemonic('Q');
        extra.add(game);
        game.setMnemonic('G');
        hard.add(about);
        about.setMnemonic('B');
        about.add(java);
        java.setMnemonic('J');
        about.add(sirjet);
        sirjet.setMnemonic('N');
        about.add(akoto);
        add(direct, BorderLayout.NORTH);
        add(filename, BorderLayout.SOUTH);
//        add(choosyAko, BorderLayout.CENTER);

        open.addActionListener(new open());
        exit.addActionListener(new exit());
    }

    class open implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JFileChooser choosyAko = new JFileChooser(".");
            JLabel direct = new JLabel(" ");
            JLabel filename = new JLabel(" ");
            choosyAko.setControlButtonsAreShown(false);
            choosyAko.showOpenDialog(null);
        }
    }

    class exit implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        Myprog frame = new Myprog();
        frame.setVisible(true);
    }
}