[英]Using JFrame and JFileChooser to select a File
I am attempting to integrate JFileChooser
into my program. 我正在尝试将
JFileChooser
集成到我的程序中。 Essentially, I would like to have a interface to select a CSV file to be read into my program. 本质上,我希望有一个界面来选择要读取到程序中的CSV文件。 I'm trying to do this using
JFileChooser
. 我正在尝试使用
JFileChooser
做到这一点。 Examples I've seen elsewhere show this being done, but the JFileChooser
opens up right away without the JFrame
. 我在其他地方看到的示例显示了此操作的完成,但是
JFileChooser
打开而没有JFrame
。 Is there a way to have JFileChooser
be a child element of my JFrame
element? 有没有办法让
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);
}
}
You are trying to call fileChooser()
in side the constructor. 您正在尝试在构造函数旁边调用
fileChooser()
。 change that one and call fileChooser()
inside a ActionListener
ie whether a button
is clicked or Menuitem
is pressed. 改变一个并调用
fileChooser()
一个内部ActionListener
即是否button
被点击或Menuitem
被按下。 So after corresponding action JFilechooser
will come in the action. 因此,在执行相应的操作后,
JFilechooser
将进入操作。
Edit : 编辑 :
JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooserAddDoc.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
.
.
}
Start by not calling QuitButtonExample
within the constructor. 首先不调用构造函数中的
QuitButtonExample
。
Instead, create a menu open called Open
(for example) and within it's ActionListener
call the method instead 而是创建一个名为
Open
的菜单Open
(例如),并在其中的ActionListener
调用该方法
Take a look at How to use File Choosers and Code Conventions for the Java Programming Language 看看如何将文件选择器和代码约定用于Java编程语言
This code works as it should. 该代码可以正常工作。
You are adding a JFileChooser (which is a JComponent) to a JFrame's panel. 您正在向JFrame的面板添加JFileChooser(这是JComponent)。 Like any other JComponent, the file chooser is nested within the frame.
像任何其他JComponent一样,文件选择器嵌套在框架中。 What you are seeing is the frame (evident from the menus) with the file chooser being one of its components.
您所看到的是框架(从菜单中显而易见),文件选择器是其组成部分之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.