2 u011056996 u011056996 于 2014.03.11 09:05 提问

java在窗体里面打开任意文件

在做毕业设计,我用的是非常落伍的applet,不知道怎么在一个JFrame窗体里面打开任意的文件,
package day.two.show.card;

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;

/**

  • @author Vector */ public class FileOpen { public static void main(String[] args) { @SuppressWarnings("unused") myFileDialog dialog = new myFileDialog(); }

}

class myFileDialog extends Frame implements ActionListener {
private static final long serialVersionUID = 1L;
FileDialog fileDialog1, fileDialog2;
MenuBar menuBar;
Menu menu;
MenuItem menuItem1, menuItem2;

/**
 * 对组件进行初始化
 */
public myFileDialog() {
    super("第一天,打开文件");
    setSize(300, 300);
    setVisible(true);
    menuBar = new MenuBar();
    menu = new Menu("文件");
    menuItem1 = new MenuItem("保存文件");
    menuItem2 = new MenuItem("打开文件");
    menuItem1.addActionListener(this);
    menuItem2.addActionListener(this);
    menu.add(menuItem1);
    menu.add(menuItem2);
    menuBar.add(menu);
    setMenuBar(menuBar);
    fileDialog1 = new FileDialog(this, "保存文件对话框", FileDialog.SAVE);
    fileDialog1.setVisible(false);
    fileDialog2 = new FileDialog(this, "打开文本文件对话框", FileDialog.LOAD);
    fileDialog2.setVisible(false);
    fileDialog1.addWindowListener(new WindowAdapter() {
        // 匿名内部类
        @Override
        public void windowClosing(WindowEvent e) {
            super.windowClosing(e);
            fileDialog1.setVisible(false);
        }
    });
    fileDialog2.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            super.windowClosing(e);
            fileDialog2.setVisible(false);
        }
    });
    addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            super.windowClosing(e);
            setVisible(false);
            System.exit(0);
        }

    });
}

// 重写方法,实现事件的动作
@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == menuItem1) {
        fileDialog1.setVisible(true);

    } else if (e.getSource() == menuItem2) {
        fileDialog2.setVisible(true);
    }

}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!