当前位置: 技术问答>java相关
java中怎么实现在windows中那样点击打开菜单就能弹出选择文件的列表框?
来源: 互联网 发布时间:2015-10-04
本文导语: java中怎么实现在windows中那样点击打开菜单就能弹出选择文件的列表框? | import java.awt.event.*; import javax.swing.*; public class Demo extends JFrame implements ActionListener { private JMenuBar _menuBar...
java中怎么实现在windows中那样点击打开菜单就能弹出选择文件的列表框?
|
import java.awt.event.*;
import javax.swing.*;
public class Demo extends JFrame implements ActionListener {
private JMenuBar _menuBar = new JMenuBar();
private JMenu _file = new JMenu("File");
private JMenuItem _open = new JMenuItem("Open");
//生成一个文件选择器,"c:\"为直接打开C盘根目录
private JFileChooser _chooser = new JFileChooser("c:\");
public Demo(String s) {
super(s);
_open.addActionListener(this);
_file.add(_open);
_menuBar.add(_file);
this.setJMenuBar(_menuBar);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.show();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == _open) {
//显示文件打开对话框,保存对话框为showSaveDialog
_chooser.showOpenDialog(this);
}
}
public static void main(String[] args) {
new Demo("今天我有空");
}
}
import javax.swing.*;
public class Demo extends JFrame implements ActionListener {
private JMenuBar _menuBar = new JMenuBar();
private JMenu _file = new JMenu("File");
private JMenuItem _open = new JMenuItem("Open");
//生成一个文件选择器,"c:\"为直接打开C盘根目录
private JFileChooser _chooser = new JFileChooser("c:\");
public Demo(String s) {
super(s);
_open.addActionListener(this);
_file.add(_open);
_menuBar.add(_file);
this.setJMenuBar(_menuBar);
this.setBounds(200,200,200,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.show();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == _open) {
//显示文件打开对话框,保存对话框为showSaveDialog
_chooser.showOpenDialog(this);
}
}
public static void main(String[] args) {
new Demo("今天我有空");
}
}
|
JFileChooser.java
public int showDialog(Component parent, String approveButtonText)
public int showDialog(Component parent, String approveButtonText)
|
用JFileChooser