标签:
1 练习 2 package bolgtest; 3 /* 4 * 演示窗体事件 5 */ 6 import java.awt.*; 7 import java.awt.event.*; 8 9 public class GuiTest1 { 10 11 public static void main(String[] args) { 12 FrameDemo demo = new FrameDemo(); 13 14 } 15 16 } 17 class FrameDemo{ 18 private Frame frame; 19 private Button button; 20 21 FrameDemo(){ 22 init(); 23 myEvent(); 24 } 25 26 public void init(){ 27 frame = new Frame("窗体"); 28 frame.setBounds(300,200,500,400); 29 button = new Button(); 30 frame.add(button); 31 frame.setVisible(true); 32 33 } 34 35 private void myEvent(){ 36 frame.addWindowListener(new WindowAdapter(){ 37 public void windowClosing(WindowEvent e){ 38 System.exit(0); 39 } 40 }); 41 } 42 43 44 }
1 练习 2 package bolgtest; 3 /* 4 * 演示ActionEvent事件 5 */ 6 import java.awt.*; 7 import java.awt.event.*; 8 9 public class GuiTest1 { 10 11 public static void main(String[] args) { 12 FrameDemo demo = new FrameDemo(); 13 14 } 15 16 } 17 class FrameDemo{ 18 private Frame frame; 19 private Button button; 20 21 FrameDemo(){ 22 init(); 23 myEvent(); 24 } 25 26 public void init(){ 27 frame = new Frame("窗体"); 28 frame.setBounds(300,200,500,400); 29 button = new Button(); 30 frame.add(button); 31 frame.setVisible(true); 32 33 } 34 35 private void myEvent(){ 36 frame.addWindowListener(new WindowAdapter(){ 37 public void windowClosing(WindowEvent e){ 38 System.exit(0); 39 } 40 }); 41 button.addActionListener(new ActionListener(){ 42 public void actionPerformed(ActionEvent e){ 43 System.exit(0); 44 } 45 }); 46 } 47 48 49 }
1 练习 2 package bolgtest; 3 /* 4 * 演示鼠标事件 5 */ 6 import java.awt.*; 7 import java.awt.event.*; 8 9 public class GuiTest1 { 10 11 public static void main(String[] args) { 12 FrameDemo demo = new FrameDemo(); 13 14 } 15 16 } 17 class FrameDemo{ 18 private Frame frame; 19 private Button button; 20 21 FrameDemo(){ 22 init(); 23 myEvent(); 24 } 25 26 public void init(){ 27 frame = new Frame("窗体"); 28 frame.setLayout(new FlowLayout()); 29 frame.setBounds(300,200,500,400); 30 button = new Button(); 31 frame.add(button); 32 frame.setVisible(true); 33 34 } 35 36 private void myEvent(){ 37 frame.addWindowListener(new WindowAdapter(){ 38 public void windowClosing(WindowEvent e){ 39 System.exit(0); 40 } 41 }); 42 button.addActionListener(new ActionListener(){ 43 public void actionPerformed(ActionEvent e){ 44 System.exit(0); 45 } 46 }); 47 button.addMouseListener(new MouseAdapter(){ 48 public void mouseEntered(MouseEvent e){ 49 System.out.println("鼠标进入"); 50 } 51 }); 52 } 53 54 55 }
1 练习 2 package bolgtest; 3 /* 4 * 演示键盘事件 5 */ 6 import java.awt.*; 7 import java.awt.event.*; 8 9 public class GuiTest1 { 10 11 public static void main(String[] args) { 12 FrameDemo demo = new FrameDemo(); 13 14 } 15 16 } 17 class FrameDemo{ 18 private Frame frame; 19 private Button button; 20 21 FrameDemo(){ 22 init(); 23 myEvent(); 24 } 25 26 public void init(){ 27 frame = new Frame("窗体"); 28 frame.setLayout(new FlowLayout()); 29 frame.setBounds(300,200,500,400); 30 button = new Button(); 31 frame.add(button); 32 frame.setVisible(true); 33 34 } 35 36 private void myEvent(){ 37 frame.addWindowListener(new WindowAdapter(){ 38 public void windowClosing(WindowEvent e){ 39 System.exit(0); 40 } 41 }); 42 43 button.addKeyListener(new KeyAdapter(){ 44 public void keyPressed(KeyEvent e){ 45 System.out.println(e.getKeyCode()+"......"+e.getKeyChar()); 46 } 47 }); 48 } 49 50 51 }
练习 package bolgtest; /* * 1:在文本框中输入目录,点击转到按钮,讲该目录下和子文件目录下的文件和文件夹名称列在下面的文本区域 * 2:制作菜单栏,让菜单中的打开栏和保存栏可以操作文件数据 * */ import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class FrameTest { public static void main(String[] args)throws Exception { new FrameWindow(); InetAddress inet = InetAddress.getLocalHost(); System.out.println(inet); } } class FrameWindow{ private Frame frame; private Button button; private TextArea textarea; private TextField textfield; private MenuBar menubar; private Menu menu,sonmenu; private MenuItem openitem,storeitem,closeitem,sonitem; private File filetest; FrameWindow(){ init(); myEvent(); } public void init(){ frame = new Frame("我的文件"); frame.setBounds(200,100,800,500); frame.setLayout(new FlowLayout()); textarea = new TextArea(100,100); button = new Button("转到"); textfield = new TextField(80); menubar = new MenuBar(); menu = new Menu("文件"); sonmenu = new Menu("子菜单"); openitem = new MenuItem("打开"); storeitem = new MenuItem("保存"); closeitem = new MenuItem("退出"); sonitem = new MenuItem("我的电脑"); menubar.add(menu); menu.add(openitem); menu.add(storeitem); menu.add(closeitem); menu.add(sonmenu); sonmenu.add(sonitem); frame.setMenuBar(menubar); frame.add(textfield); frame.add(button); frame.add(textarea); frame.setVisible(true); } private void myEvent(){ frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String str = textfield.getText(); File file = new File(str); boolean flag; flag = file.isDirectory(); if(flag==true){ directory(file); } else{ new DialogDemo(); } } }); textfield.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ if(e.getKeyChar()==e.VK_ENTER ){ String str = textfield.getText(); try { URL url = new URL(str); URLConnection urlconnection = url.openConnection(); InputStream input = urlconnection.getInputStream(); BufferedReader buff = new BufferedReader(new InputStreamReader(input)); String str5 = null; while((str5 = buff.readLine())!=null){ textarea.append(str5); } } catch (Exception e1) { e1.printStackTrace(); } } } }); openitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ FileDialog filedialog = new FileDialog(frame,"打开文件",FileDialog.LOAD); filedialog.setVisible(true); String str1 = filedialog.getFile(); String str2 = filedialog.getDirectory(); System.out.println(str1 + "...." + str2); File file3 = new File(str2,str1); BufferedReader buff =null; try{ textarea.setText(""); buff = new BufferedReader(new InputStreamReader(new FileInputStream(file3))); String str3 = null; while((str3 = buff.readLine()) != null){ textarea.append(str3); textarea.append("\r\n"); } }catch(Exception e1){ System.out.println(e1.toString()); }finally{ try{ buff.close(); }catch(Exception e1){ throw new RuntimeException("打开文件失败"); } } } }); storeitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(filetest == null){ FileDialog filedialog = new FileDialog(frame,"保存",FileDialog.SAVE); filedialog.setVisible(true); String str1 = filedialog.getFile(); String str2 = filedialog.getDirectory(); if(str1 == null || str2 == null){ return; } filetest = new File(str2,str1); } BufferedWriter buff = null; try{ buff = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filetest))); String str = textarea.getText(); buff.write(str); buff.flush(); }catch(Exception e1){ System.out.println(e1.toString()); }finally{ try{ buff.close(); }catch(Exception e1){ throw new RuntimeException("打开文件失败"); } } } }); closeitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0); } }); } private class DialogDemo{ private Dialog dialog; private Label label; private Button button1; DialogDemo(){ initcopy(); eventcopy(); } private void initcopy(){ dialog = new Dialog(frame,"出错"); dialog.setBounds(300,200,500,150); dialog.setLayout(new FlowLayout()); dialog.setVisible(true); label = new Label("没有符合搜索匹配的文件"); button1 = new Button("确定"); dialog.add(label); dialog.add(button1); } private void eventcopy(){ dialog.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dialog.setVisible(false); } }); button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dialog.setVisible(false); } }); }} private void directory(File file){ File[] str = file.listFiles(); for(int i = 0; i < str.length; i++){ if(str[i].isDirectory()) directory(str[i]); else textarea.append(str[i].toString()+"\n"); } } }
标签:
原文地址:http://www.cnblogs.com/yuemingxingxing/p/5078028.html