大家帮帮忙,把下面代码直接改成一运行就显现打开图片的对话框,即删掉“f”对话框- package open;
- import java.awt.FileDialog;
- import java.awt.Frame;
- import java.awt.Menu;
- import java.awt.MenuBar;
- import java.awt.MenuItem;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JLabel;
- public class Main {
- @SuppressWarnings("deprecation")
- public static void main(String[] args) {
- // TODO code application logic here
- final Frame f = new Frame("打开");// 标题
- final JLabel lbl = new JLabel();
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- MenuBar mb = new MenuBar();
- Menu m1 = new Menu("photolist");
- MenuItem mi2 = new MenuItem("open");
- mi2.addActionListener(new ActionListener() { // 打开文件夹
- public void actionPerformed(ActionEvent e) {
- FileDialog fd = new FileDialog(f, "photo open",
- FileDialog.LOAD);
- fd.show();
- String fileName = fd.getDirectory()+ fd.getFile();
- Icon icon = new ImageIcon(fileName,"");
- lbl.setIcon(icon);
- System.out.println(fd.getDirectory()+fd.getFile()); //显示图片路径及名称
- }
- });
-
- m1.add(mi2);
- mb.add(m1);
-
- f.setMenuBar(mb);
- f.add(lbl);
- f.show();
- }
- }
复制代码
|
|