Java 使用 JFrame 和 JFileChooser 选择文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20624825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:11:22  来源:igfitidea点击:

Using JFrame and JFileChooser to select a File

javaswinguser-interface

提问by thebighoncho

I am attempting to integrate JFileChooserinto my program. Essentially, I would like to have a interface to select a CSV file to be read into my program. I'm trying to do this using JFileChooser. Examples I've seen elsewhere show this being done, but the JFileChooseropens up right away without the JFrame. Is there a way to have JFileChooserbe a child element of my JFrameelement?

我正在尝试集成JFileChooser到我的程序中。本质上,我想要一个界面来选择要读入我的程序的 CSV 文件。我正在尝试使用JFileChooser. 我在其他地方看到的例子表明这已经完成,但JFileChooser没有JFrame. 有没有办法JFileChooser成为我JFrame元素的子元素?

My code is below:

我的代码如下:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class QuitButtonExample extends JFrame {
    JPanel panel = new JPanel();

    public QuitButtonExample() {
        initUI();
        quitButton();
        menu();
        fileChooser();
    }

    private void initUI() {
        JLabel label1 = new JLabel(
            "Selct the .csv file contaning the addresses to be geocoded...");
        label1.setBounds(0, 0, 500, 50);
        panel.add(label1); 
        getContentPane().add(panel);
        setSize(1000, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void quitButton() {

        // Quit Button

        panel.setLayout(null);
        JButton quitButton = new JButton("Quit");
        quitButton.setBounds(0, 50, 80, 30);

        quitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
        panel.add(quitButton);
        setTitle("Quit Button");
    }

    private void menu() {
        // Menu Bar

        // "File"
        JMenuBar menuBar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenuItem menuItem = new JMenuItem("Exit"); // eMenuItem
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        file.add(menuItem);
        menuBar.add(file);

        // "Credits"
        JMenu credits = new JMenu("Credits");
        JMenuItem about = new JMenuItem("About...");
        about.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0); // TODO - New window, showing credits for
                        // project
            }
        });

        credits.add(about);
        menuBar.add(credits);
        setJMenuBar(menuBar);
    }

    private void fileChooser() {
        // FileChooser

        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "CSV Files", "csv");
        chooser.setFileFilter(filter);
        chooser.setBounds(0, 75, 500, 300);
        panel.add(chooser);
    }

    public static void main(String[] args) {
        System.out.println("Hello World");
        QuitButtonExample ex = new QuitButtonExample();
        ex.setVisible(true);
    }
}

采纳答案by Rakesh KR

You are trying to call fileChooser()in side the constructor. change that one and call fileChooser()inside a ActionListenerie whether a buttonis clicked or Menuitemis pressed. So after corresponding action JFilechooserwill come in the action.

您正在尝试调用 fileChooser()构造函数。改变那个并fileChooser()在 a 内调用 ,ActionListener即 abutton是被点击还是Menuitem被按下。所以在相应的动作之后JFilechooser就会出现动作。

Edit:

编辑

JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooserAddDoc.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
     file = fileChooser.getSelectedFile();
     .
     .
}

回答by MadProgrammer

Start by not calling QuitButtonExamplewithin the constructor.

首先不要QuitButtonExample在构造函数中调用。

Instead, create a menu open called Open(for example) and within it's ActionListenercall the method instead

相反,创建一个打开的菜单Open(例如),并在其中ActionListener调用方法

Take a look at How to use File Choosersand Code Conventions for the Java Programming Language

看看如何使用Java 编程语言的文件选择器代码约定

回答by amahfouz

This code works as it should.

这段代码可以正常工作。

You are adding a JFileChooser (which is a JComponent) to a JFrame's panel. Like any other JComponent, the file chooser is nested within the frame. What you are seeing is the frame (evident from the menus) with the file chooser being one of its components.

您正在将 JFileChooser(它是一个 JComponent)添加到 JFrame 的面板。与任何其他 JComponent 一样,文件选择器嵌套在框架内。您看到的是框架(从菜单中可见),其中文件选择器是其组件之一。