Java Swing菜单初学者建议
我刚开始学习一些JavaGUI技术,我创建了一个小程序,它有一个菜单栏、一个菜单以及菜单中的一个项目。现在我要做的是实现一个文件选择器,这样当菜单中的项目被选中时,文件选择器就会被执行。问题是我创建的文件选择器位于另一个类中。单击菜单中的项目时,是否有可能启动我的文件选择器类? 这是我的密码
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);
}
}
# 1 楼答案
如果要使用JFileChooser,必须在菜单项上添加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