模拟window开打文件
模拟简单记事本
jar -cvfm 1.txt com.mymenu
import java.awt.Button; import java.awt.Dialog; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; public class MyWinDemo { private Frame f; private TextField tf; private Button btn; private TextArea ta; private Dialog d; private Label lab; private Button okbtn; MyWinDemo(){ init(); } private void init(){ f=new Frame("my Window"); f.setBounds(300,100,600,500); f.setLayout(new FlowLayout()); tf=new TextField(60); btn=new Button("转到"); ta=new TextArea(25,70); d=new Dialog(f,"提示信息-self",true); d.setBounds(400,200,250,150); d.setLayout(new FlowLayout()); lab=new Label(); okbtn=new Button("确定"); d.add(lab); d.add(okbtn); f.add(tf); f.add(btn); f.add(ta); myEvent(); f.setVisible(true); } private void myEvent() { //添加 回车 时候 自动转到 tf.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyCode()==KeyEvent.VK_ENTER){ showDir(); } } }); //确定按钮事件 okbtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub d.setVisible(false); } }); d.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub d.setVisible(false); } }); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showDir(); } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); } private void showDir() { String dirPath=tf.getText(); File dir=new File(dirPath); if(dir.exists() && dir.isDirectory()){ ta.setText(""); String[] names=dir.list(); for (String name : names) { ta.append(name+"\r\n"); //ta.setText(text); } }else{ String info="您输入的信息:"+dirPath+"有误,请重新输入"; lab.setText(info); d.setVisible(true); } tf.setText(""); } public static void main(String[] args) { new MyWinDemo(); } }
模拟简单记事本
import java.awt.FileDialog; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * 实现菜单栏 */ public class MyMenuDemo { private Frame f; private MenuBar mb; private Menu m,subm; private MenuItem closeItem,openItem,saveItem,subItem1,subItem; private FileDialog openDialog,saveDialog; private TextArea ta; private File file; MyMenuDemo(){ init(); } private void init(){ f=new Frame("my Window"); f.setBounds(300,100,600,500); //f.setLayout(); mb=new MenuBar(); m=new Menu("文件"); closeItem=new MenuItem("退出"); openItem=new MenuItem("打开"); saveItem=new MenuItem("保存"); subm=new Menu("新建"); subItem1=new MenuItem("Web Project"); subItem=new MenuItem("Java Project"); subm.add(subItem); subm.add(subItem1); m.add(subm); m.add(openItem); m.add(saveItem); m.add(closeItem); mb.add(m); openDialog=new FileDialog(f,"我要打开",FileDialog.LOAD); saveDialog=new FileDialog(f,"我要保存",FileDialog.SAVE); ta=new TextArea(); f.add(ta); f.setMenuBar(mb); myEvent(); f.setVisible(true); } private void myEvent() { saveItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(file==null){ saveDialog.setVisible(true); String dirPath=saveDialog.getDirectory(); String fileName=saveDialog.getFile(); if(dirPath==null || fileName==null) return; file=new File(dirPath,fileName); } try { BufferedWriter bufw=new BufferedWriter(new FileWriter(file)); String text=ta.getText(); bufw.write(text); bufw.close(); } catch (IOException e2) { throw new RuntimeException("保存失败"); } } }); //打开菜单 openItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub openDialog.setVisible(true); String dirPath=openDialog.getDirectory(); String fileName=openDialog.getFile(); System.out.println(dirPath+"...."+fileName); if(dirPath==null || fileName==null) return; ta.setText(""); file=new File(dirPath,fileName); try { BufferedReader bufr=new BufferedReader(new FileReader(file)); String line=null; while((line=bufr.readLine())!=null){ ta.append(line+"\r\n"); } bufr.close(); } catch (IOException e2) { throw new RuntimeException("打开异常"); } } }); closeItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); } public static void main(String[] args) { // TODO Auto-generated method stub new MyMenuDemo(); } }
jar -cvfm 1.txt com.mymenu