我正在嘗試將JFileChooser
集成到我的程序中。從本質上講,我想有一個界面來選擇一個CSV文件讀入我的程序。我試圖用JFileChooser
來做到這一點。我在別處看到的例子顯示這樣做,但JFileChooser
沒有JFrame
就馬上打開。有沒有辦法讓JFileChooser
成爲我的JFrame
元素的子元素?使用JFrame和JFileChooser選擇文件
我的代碼如下:
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);
}
}
是否有可能將功能添加到JFileChooser對話框中的「打開」和「保存」按鈕?如果是這樣,怎麼樣?只需添加一個actionListener給他們? – dgalati54
* YES * fileChooser.showSaveDialog();保存和fileChooser.showOpenDialog();開放 –