博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
黑马程序员——图形用户界面(GUI)总结
阅读量:6837 次
发布时间:2019-06-26

本文共 9342 字,大约阅读时间需要 31 分钟。

                  ------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
什么是GUI?
GUI就是计算机通过图形化界面与用户产生的交互。
注意:Java提供的GUI对象都存储在Java.Awt和Java.Swing包中
 
GUI的体系结构
容器的布局管理器
FlowLayout(流式布局管理器)
从左到右的顺序排列
Panel的默认布局管理器
BorderLayout(边界布局管理器)
东南西北中顺序排列
Frame默认布局管理器
GirdLayout(网格布局管理器)
规矩的矩阵
CardLayout(卡片布局管理器)
选项卡
GirdBagLayout(网格包布局管理器)
非规矩的矩形
 
创建图形化界面步骤
1:创建窗体
Frame frame = new Frame();
2:对窗体基本设置,如大小,位置,布局
frame.setSize(500,400);
frame.setLocation(300,200);
frame.setLayout(new FlowLayout());
3:定义组件
Button button = new Button();
4:将组件通过窗体的add方法添加到窗体中
frame.add(button);
5:让窗体显示化
frame.setVisiable(true);
 
事件监听机制的特点
1,事件源:组件
2,事件:每个组件上有对应的事件
3,监听器
4,事件处理
 
常用的事件有:窗体事件,Action事件,鼠标事件,键盘事件
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");  }    }}

 

转载于:https://www.cnblogs.com/yuemingxingxing/p/5078028.html

你可能感兴趣的文章
《SQL Server性能调优实战》知识点汇总
查看>>
JS 中文乱码
查看>>
原生JS实现音乐播放器!
查看>>
hive-安装MySQL(centos6.4)
查看>>
UVa 12100 Printer Queue (习题 5-7)
查看>>
windows下安装apache zookeeper
查看>>
第三周作业
查看>>
git pull --rebase
查看>>
linux下mysql的root密码忘记解决方
查看>>
FastDFS的配置、部署与API使用解读(6)FastDFS配置详解之Storage配置(转)
查看>>
rabbitmq的web管理界面无法使用guest用户登录
查看>>
[LeetCode]题解(python):048-Rotate Image
查看>>
[LeetCode]题解(python):075-Sort Colors
查看>>
protobuf 中的嵌套消息的使用 主要对set_allocated_和mutable_的使用
查看>>
0-1背包问题
查看>>
系统的Drawable(二)-Selector
查看>>
CAS 界面根据不同的域名显示不同的界面
查看>>
java获取在各种编码下中文及英文的字符个数
查看>>
数组知识0913
查看>>
ECharts+百度地图网络拓扑应用
查看>>