我刚刚开始学习一些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);
}
}
答案 0 :(得分:0)
如果要使用JFileChooser,则必须在menuitem上添加eventlistener。你最好看看这个:http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
或上述示例代码的直接链接: http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemoProject/src/components/FileChooserDemo.java