我刚刚开始学习一些java GUI技术,我已经创建了一个小程序,它有一个菜单栏、一个菜单以及该菜单中的一个项目。现在我要做的是实现一个文件选择器,这样当菜单中的项目被选中时,文件选择器就会被执行。问题是我创建的文件选择器在另一个类中。当菜单中的项目被点击时,有没有可能启动我的文件选择器的类?以下是我的代码
public class menu {
public static void main(String[] args){
//to menu
JFrame frame = new JFrame("Menu");
frame.setVisible(true);
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
frame.setJMenuBar(bar);
JMenu search = new JMenu("Browse");
bar.add(search);
final JMenuItem songstoplay = new JMenuItem("Browse Songs To Play");
search.add(songstoplay);
//to outline
final Container content = frame.getContentPane();
content.setLayout(new GridLayout(3, 1));
JLabel text = new JLabel("My first iteration", JLabel.CENTER);
text.setVerticalAlignment(JLabel.TOP);
text.setFont(new Font("Serif", Font.PLAIN, 30));
content.add(text);
JLabel text2 = new JLabel();
text2.setText("List of drives connected: C:/");
text2.setFont(new Font("Serif", Font.PLAIN, 20));
text2.setVerticalAlignment(JLabel.TOP);
content.add(text2);
}
}发布于 2014-04-18 04:41:19
如果你想使用JFileChooser,你必须在你的菜单项上添加一个事件监听器。你最好看看这个:http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
或者直接链接到上面示例中的代码:http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemoProject/src/components/FileChooserDemo.java
https://stackoverflow.com/questions/23142511
复制相似问题