• java之 22天 GUI 图形界面编程(二)


    转自:http://takeme.iteye.com/blog/1876853

    模拟window开打文件 

    Java代码
      1 import java.awt.Button;  
      2 import java.awt.Dialog;  
      3 import java.awt.FlowLayout;  
      4 import java.awt.Frame;  
      5 import java.awt.Label;  
      6 import java.awt.TextArea;  
      7 import java.awt.TextField;  
      8 import java.awt.event.ActionEvent;  
      9 import java.awt.event.ActionListener;  
     10 import java.awt.event.KeyAdapter;  
     11 import java.awt.event.KeyEvent;  
     12 import java.awt.event.WindowAdapter;  
     13 import java.awt.event.WindowEvent;  
     14 import java.io.File;  
     15   
     16 public class MyWinDemo {  
     17   
     18     private Frame f;  
     19     private TextField tf;  
     20     private Button btn;  
     21     private TextArea ta;  
     22     private Dialog d;  
     23     private Label lab;  
     24     private Button okbtn;  
     25       
     26     MyWinDemo(){  
     27         init();  
     28     }  
     29       
     30     private void init(){  
     31         f=new Frame("my Window");  
     32         f.setBounds(300,100,600,500);  
     33         f.setLayout(new FlowLayout());  
     34           
     35         tf=new TextField(60);  
     36           
     37         btn=new Button("转到");  
     38         ta=new TextArea(25,70);  
     39           
     40         d=new Dialog(f,"提示信息-self",true);  
     41         d.setBounds(400,200,250,150);  
     42         d.setLayout(new FlowLayout());  
     43           
     44         lab=new Label();  
     45         okbtn=new Button("确定");  
     46           
     47         d.add(lab);  
     48         d.add(okbtn);  
     49           
     50           
     51         f.add(tf);  
     52         f.add(btn);  
     53         f.add(ta);  
     54           
     55         myEvent();  
     56         f.setVisible(true);  
     57           
     58     }  
     59       
     60       
     61     private void myEvent() {  
     62           
     63         //添加 回车 时候 自动转到  
     64         tf.addKeyListener(new KeyAdapter() {  
     65             @Override  
     66             public void keyPressed(KeyEvent e) {  
     67                 // TODO Auto-generated method stub  
     68                 if(e.getKeyCode()==KeyEvent.VK_ENTER){  
     69                     showDir();  
     70                 }  
     71             }  
     72         });  
     73         //确定按钮事件  
     74         okbtn.addActionListener(new ActionListener() {  
     75               
     76             @Override  
     77             public void actionPerformed(ActionEvent e) {  
     78                 // TODO Auto-generated method stub  
     79                 d.setVisible(false);  
     80             }  
     81         });  
     82           
     83         d.addWindowListener(new WindowAdapter() {  
     84             @Override  
     85             public void windowClosing(WindowEvent e) {  
     86                 // TODO Auto-generated method stub  
     87                 d.setVisible(false);  
     88             }  
     89           
     90         });  
     91           
     92         btn.addActionListener(new ActionListener() {  
     93               
     94             @Override  
     95             public void actionPerformed(ActionEvent e) {  
     96                 showDir();  
     97             }  
     98               
     99         });  
    100           
    101           
    102         f.addWindowListener(new WindowAdapter() {  
    103             public void windowClosing(WindowEvent e){  
    104                 System.exit(0);  
    105             }  
    106               
    107         });  
    108           
    109     }  
    110     private void showDir() {  
    111         String dirPath=tf.getText();  
    112           
    113         File dir=new File(dirPath);  
    114         if(dir.exists() && dir.isDirectory()){  
    115             ta.setText("");  
    116             String[] names=dir.list();  
    117             for (String name : names) {  
    118                 ta.append(name+"
    ");  
    119                 //ta.setText(text);  
    120             }  
    121         }else{  
    122             String info="您输入的信息:"+dirPath+"有误,请重新输入";  
    123             lab.setText(info);  
    124             d.setVisible(true);  
    125         }  
    126           
    127         tf.setText("");  
    128     }  
    129     public static void main(String[] args) {  
    130         new MyWinDemo();  
    131     }  
    132   
    133 }  

    模拟简单记事本 

    Java代码
      1 import java.awt.FileDialog;  
      2 import java.awt.Frame;  
      3 import java.awt.Menu;  
      4 import java.awt.MenuBar;  
      5 import java.awt.MenuItem;  
      6 import java.awt.TextArea;  
      7 import java.awt.event.ActionEvent;  
      8 import java.awt.event.ActionListener;  
      9 import java.awt.event.WindowAdapter;  
     10 import java.awt.event.WindowEvent;  
     11 import java.io.BufferedReader;  
     12 import java.io.BufferedWriter;  
     13 import java.io.File;  
     14 import java.io.FileReader;  
     15 import java.io.FileWriter;  
     16 import java.io.IOException;  
     17   
     18 /** 
     19  * 实现菜单栏 
     20  */  
     21 public class MyMenuDemo {  
     22   
     23       
     24     private Frame f;  
     25     private MenuBar mb;  
     26     private Menu m,subm;  
     27     private MenuItem closeItem,openItem,saveItem,subItem1,subItem;  
     28     private FileDialog openDialog,saveDialog;  
     29     private TextArea ta;  
     30     private File file;  
     31       
     32     MyMenuDemo(){  
     33         init();  
     34     }  
     35       
     36     private void init(){  
     37         f=new Frame("my Window");  
     38         f.setBounds(300,100,600,500);  
     39         //f.setLayout();  
     40           
     41         mb=new MenuBar();  
     42         m=new Menu("文件");  
     43           
     44         closeItem=new MenuItem("退出");  
     45         openItem=new MenuItem("打开");  
     46         saveItem=new MenuItem("保存");  
     47           
     48         subm=new Menu("新建");  
     49         subItem1=new MenuItem("Web Project");  
     50         subItem=new MenuItem("Java Project");  
     51         subm.add(subItem);  
     52         subm.add(subItem1);  
     53           
     54         m.add(subm);  
     55         m.add(openItem);  
     56         m.add(saveItem);  
     57         m.add(closeItem);  
     58         mb.add(m);  
     59           
     60         openDialog=new FileDialog(f,"我要打开",FileDialog.LOAD);  
     61         saveDialog=new FileDialog(f,"我要保存",FileDialog.SAVE);  
     62           
     63         ta=new TextArea();  
     64           
     65         f.add(ta);  
     66         f.setMenuBar(mb);  
     67         myEvent();  
     68           
     69         f.setVisible(true);  
     70           
     71     }  
     72       
     73     private void myEvent() {  
     74           
     75         saveItem.addActionListener(new ActionListener() {  
     76               
     77             @Override  
     78             public void actionPerformed(ActionEvent e) {  
     79                 // TODO Auto-generated method stub  
     80                 if(file==null){  
     81                     saveDialog.setVisible(true);  
     82                     String dirPath=saveDialog.getDirectory();  
     83                     String fileName=saveDialog.getFile();  
     84                     if(dirPath==null || fileName==null)  
     85                         return;  
     86                     file=new File(dirPath,fileName);  
     87                 }  
     88                 try {  
     89                       
     90                     BufferedWriter bufw=new BufferedWriter(new FileWriter(file));  
     91                       
     92                     String text=ta.getText();  
     93                       
     94                     bufw.write(text);  
     95                     bufw.close();  
     96                 } catch (IOException e2) {  
     97                     throw new RuntimeException("保存失败");  
     98                 }  
     99                   
    100             }  
    101         });  
    102           
    103         //打开菜单  
    104         openItem.addActionListener(new ActionListener() {  
    105               
    106             @Override  
    107             public void actionPerformed(ActionEvent e) {  
    108                 // TODO Auto-generated method stub  
    109                 openDialog.setVisible(true);  
    110                 String dirPath=openDialog.getDirectory();  
    111                 String fileName=openDialog.getFile();  
    112                 System.out.println(dirPath+"...."+fileName);  
    113                 if(dirPath==null || fileName==null)  
    114                     return;  
    115                 ta.setText("");  
    116                 file=new File(dirPath,fileName);  
    117                 try {  
    118                     BufferedReader bufr=new BufferedReader(new FileReader(file));  
    119                     String line=null;  
    120                     while((line=bufr.readLine())!=null){  
    121                         ta.append(line+"
    ");  
    122                     }  
    123                     bufr.close();  
    124                 } catch (IOException e2) {  
    125                     throw new RuntimeException("打开异常");  
    126                 }  
    127                   
    128             }  
    129         });  
    130           
    131           
    132         closeItem.addActionListener(new ActionListener() {  
    133               
    134             @Override  
    135             public void actionPerformed(ActionEvent e) {  
    136                 // TODO Auto-generated method stub  
    137                 System.exit(0);  
    138             }  
    139         });  
    140           
    141         f.addWindowListener(new WindowAdapter() {  
    142             public void windowClosing(WindowEvent e){  
    143                 System.exit(0);  
    144             }  
    145               
    146         });  
    147           
    148     }  
    149       
    150       
    151     public static void main(String[] args) {  
    152         // TODO Auto-generated method stub  
    153         new MyMenuDemo();  
    154     }  
    155   
    156 }  

  • 相关阅读:
    每日日报16
    每日日报15
    每日日报14
    每日日报13
    每日日报12
    每日日报11
    每日日报10
    每日作业报告
    每日作业报告
    每日作业报告
  • 原文地址:https://www.cnblogs.com/fnlingnzb-learner/p/5953845.html
Copyright © 2020-2023  润新知