黑马程序员--- 学习笔记(第二十二天)
来源:互联网 发布:淘宝网男士羽绒服 编辑:程序博客网 时间:2024/05/21 08:37
—————————— ASP.Net+Android+IOS开发、.Net培训、期待与您交流!——————————
GUI(Graphical User Interface)图形化用户接口
CLI(Command Line User Interface)命令行用户接口
java为GUI提供的对象都在java.awt和java.swing两个包中
java.awt:(abstract window toolkit(抽象窗口工具包))
需要调用本地系统方实现功能,是重量级控件
java.swing:在awt的基础上,建立的一套图形界面系统,其中
提供了更多的组件,而且完全由java实现,增加移植性,属于轻量级
控件
Component组件
container:容器,是一个特殊的组件,该组件可以通过add方法添加
其他组件进来
容器中组件的排版方式就是布局:
常见的布局管理器:
FlowLayout:流式布局 默认居中
.从左到右的顺序排列
.Panel默认的布局管理器
BorderLayout:边界式布局 默认居中
.东南西北中
.Frame默认的布局管理器
GridLayout:网格布局
.规则的矩阵
CradleLayout:卡片布局
.选项卡
GridBagLayout:网格包布局
.非规则的矩阵
最厉害的是坐标式布局, Eclipse可以安装插件轻松布局
cmd命令:
md 目录名 //创建目录
rd 目录名 //删除目录
创建图形化界面步骤:
1.创建Frame窗体
2.对窗体进行基本设置(比如大小,位置,布局等)
3.定义组件
4.将组件通过窗体的add方法添加到窗体中
5.让窗体显示
事件监听机制
事件源(组件)
事件(Event)
监听器(Listener)
事件处理(引发事件后的处理方式)
抽象类没有抽象方法,该类只是为了不让创建对象,
因为创建对象没有意义.
窗体事件
事件和图形化界面分离出来
按钮事件
/*
按钮就是事件源
那么选择哪个监听器呢?
通过关闭窗体示例了解到,想要知道哪个组件具备什么样的监听器
需要查看该组件对象的功能
通过查询Button的描述,发现按钮支持一个特有的监听addActionListener
*/
鼠标事件
键盘事件
/*
练习 文本框只能输入数字,要使用consume()方法
*/
对话框(Dialog)
/*
练习 输入目录,列出目录的列表
*/
菜单:
MenuBar 菜单栏
Menu 菜单
MunuItem 菜单项
菜单是菜单项的子类
文件对话框(FileDialog)
双击执行jar程序
创建个清单文件list.txt
写入 Main-Class:空格包名.类名回车
注意空格,回车必须要
然后jar -cvfm jar名称 list.txt 类名
电脑系统盘目录-工具-文件夹选项-文件类型-jar-高级
-新建open -值为安装目录的javaw.exe目录 -jar(注意后面的-jar
是在exe的后面空格隔开)
—————————— ASP.Net+Android+IOS开发、.Net培训、期待与您交流!——————————
GUI(Graphical User Interface)图形化用户接口
CLI(Command Line User Interface)命令行用户接口
java为GUI提供的对象都在java.awt和java.swing两个包中
java.awt:(abstract window toolkit(抽象窗口工具包))
需要调用本地系统方实现功能,是重量级控件
java.swing:在awt的基础上,建立的一套图形界面系统,其中
提供了更多的组件,而且完全由java实现,增加移植性,属于轻量级
控件
Component组件
container:容器,是一个特殊的组件,该组件可以通过add方法添加
其他组件进来
容器中组件的排版方式就是布局:
常见的布局管理器:
FlowLayout:流式布局 默认居中
.从左到右的顺序排列
.Panel默认的布局管理器
BorderLayout:边界式布局 默认居中
.东南西北中
.Frame默认的布局管理器
GridLayout:网格布局
.规则的矩阵
CradleLayout:卡片布局
.选项卡
GridBagLayout:网格包布局
.非规则的矩阵
最厉害的是坐标式布局, Eclipse可以安装插件轻松布局
cmd命令:
md 目录名 //创建目录
rd 目录名 //删除目录
创建图形化界面步骤:
1.创建Frame窗体
2.对窗体进行基本设置(比如大小,位置,布局等)
3.定义组件
4.将组件通过窗体的add方法添加到窗体中
5.让窗体显示
事件监听机制
事件源(组件)
事件(Event)
监听器(Listener)
事件处理(引发事件后的处理方式)
抽象类没有抽象方法,该类只是为了不让创建对象,
因为创建对象没有意义.
窗体事件
事件和图形化界面分离出来
按钮事件
/*
按钮就是事件源
那么选择哪个监听器呢?
通过关闭窗体示例了解到,想要知道哪个组件具备什么样的监听器
需要查看该组件对象的功能
通过查询Button的描述,发现按钮支持一个特有的监听addActionListener
*/
鼠标事件
键盘事件
/*创建图形化界面步骤:1.创建Frame窗体2.对窗体进行基本设置(例如大小,位置,布局方式等)3.定义组件4.将组件通过窗体的add方法添加到窗体中5.让窗体显示 让事件和图形化界面分离出来 窗体事件按钮事件鼠标事件键盘事件*/import java.awt.*;import java.awt.event.*;//窗体类class MyFrame extends Frame{//定义该窗体中所需的控件private Button b;//构造函数 初始化窗体public MyFrame(){init();}//初始化private void init(){ //setBounds(x,y,w,h);==setSize(w,h)+setLacation(x,y); //设置组件基本属性 //Frame setTitle("my first program"); //设置标题 setBounds(300,200,500,400);//设置位置以及大小 setLayout(new FlowLayout());//设置布局 //Button b=new Button("first button"); //添加控件 add(b);//添加组件 //调用事件方法windowEvent();buttonEvent();mouseEvent();keyEvent(); //显示窗体 setVisible(true);}//窗体事件private void windowEvent(){addWindowListener(new WindowAdapter(){//void windowActivated(WindowEvent e) // 激活窗口时调用。 public void windowActivated(WindowEvent e){ sop("我活动了");}// void windowClosing(WindowEvent e) // 窗口正处在关闭过程中时调用。 public void windowClosing(WindowEvent e){sop("正在关闭");System.exit(0);}// void windowOpened(WindowEvent e) // 已打开窗口时调用。 public void windowOpened(WindowEvent e){sop("窗口打开"); }});}//按钮事件private void buttonEvent(){ b.addActionListener(new ActionListener(){ //按钮活动事件 public void actionPerformed(ActionEvent e){// sop("按钮关闭"); // System.exit(0);} });}//鼠标事件private void mouseEvent(){b.addMouseListener(new MouseAdapter(){ //鼠标移进public void mouseEntered(MouseEvent e){sop("鼠标移动进按钮了");}//鼠标单机public void mouseClicked(MouseEvent e){//getClickCount判断 实现双击if(e.getClickCount()==2)sop("鼠标被双击");elsesop("鼠标被单机");} });}//键盘事件private void keyEvent(){b.addKeyListener(new KeyAdapter(){//键盘按下public void keyPressed(KeyEvent e){//输出文本形式的键 和键码sop(KeyEvent.getKeyText(e.getKeyCode())+" "+e.getKeyCode());// boolean isControlDown() //返回 Control 修饰符在此事件上是为 down。 //组合键if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_B)sop("按下了ctrl+b 组合键");}});}private void sop(Object o){System.out.println(o);}}//程序运行入口class Demo1 {public static void main(String[] args) {new MyFrame();}}
/*
练习 文本框只能输入数字,要使用consume()方法
*/
/* 练习 文本框只能输入数字*/import java.awt.*;import java.awt.event.*;class MyFrame extends Frame{private TextField tf;public MyFrame(){init();}private void init(){ setBounds(300,200,500,400); setLayout(new FlowLayout()); setTitle("文本框输入练习"); tf=new TextField(20); add(tf); keyEvent(); windowEvent(); setVisible(true);}//窗体事件private void windowEvent(){this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){System.exit(0); }});}//键盘事件private void keyEvent(){tf.addKeyListener(new KeyAdapter(){//键盘按下public void keyPressed(KeyEvent e){int code=e.getKeyCode();//判断是否在0-9if(!(code>=0x30 && code<=0x39))e.consume();//不是的话就不处理}});}}class Demo2 {public static void main(String[] args) {new MyFrame();}}
对话框(Dialog)
/*
练习 输入目录,列出目录的列表
*/
/*文本框输入目录 转到文本域输出列表*/import java.awt.*;import java.awt.event.*;import java.io.*;class MyFrame extends Frame{private TextField tf; //文本框private Button b; //转到按钮private TextArea ta; //文本域private Dialog d;//对话框private Label l; //标签private Button sure; //确定按钮public MyFrame(){super("输出文件列表");init();}//初始化private void init(){setBounds(300,200,500,500);setLayout(new FlowLayout());tf=new TextField(50);b=new Button("转到");ta=new TextArea(28,60);l=new Label();sure=new Button("确定");d=new Dialog(this,"温馨提示:",true);d.setBounds(400,400,300,100);d.setLayout(new FlowLayout());d.add(l);d.add(sure);add(tf);add(b);add(ta); windowEvent(); keyEvent(); buttonEvent();setVisible(true);}//窗体事件private void windowEvent(){this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});d.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){d.setVisible(false);}});}//键盘事件private void keyEvent(){tf.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){ if(e.getKeyCode()==10) showDir();}});}//按钮事件private void buttonEvent(){ b.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){showDir();} }); sure.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){d.setVisible(false);} });}//展示目录private void showDir(){String filepath=tf.getText();File file=new File(filepath);if(file.exists() && file.isDirectory()){ta.setText("");tf.setText("");String []strs=file.list();for (String s : strs ){ta.append(s+"\r\n");}}else{l.setText("该目录"+filepath+"不存在,请重新输入");tf.setText("");d.setVisible(true);}}}class Demo3 {public static void main(String[] args) {new MyFrame();}}
菜单:
MenuBar 菜单栏
Menu 菜单
MunuItem 菜单项
菜单是菜单项的子类
文件对话框(FileDialog)
/*打开文件保存文件简单记事本程序 */import java.awt.*;import java.awt.event.*;import java.io.*;class MyFrame extends Frame{private MenuBar filemenu;private Menu file;private MenuItem open;private MenuItem save;private Menu zicaidan;private MenuItem zicaidanxiang;private FileDialog of;private FileDialog sf;private TextArea ta;private Dialog tishi;private Label l;private String path;private String filename;public MyFrame(){super("打开与保存文件");init();}//初始化private void init(){ setBounds(300,200,500,500); filemenu=new MenuBar(); file=new Menu("File"); open=new MenuItem("open"); save=new MenuItem("save"); zicaidan=new Menu("子菜单"); zicaidanxiang=new MenuItem("子菜单项"); ta=new TextArea(30,60); tishi=new Dialog(this,"温馨提示",true); l=new Label(); tishi.setBounds(300,200,300,100); tishi.add(l); file.add(open); file.add(save); file.add(zicaidan); zicaidan.add(zicaidanxiang); this.setMenuBar(filemenu); filemenu.add(file); windowEvent(); menuItemEvent(); add(ta); setVisible(true);}private void windowEvent(){addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});tishi.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){tishi.setVisible(false);}});}private void menuItemEvent(){//打开open.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){of=new FileDialog(MyFrame.this,"打开文件",FileDialog.LOAD);of.setBounds(300,200,400,400);of.setVisible(true); path=of.getDirectory(); filename=of.getFile();if(path!=null || filename!=null)read();}});//保存save.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){if(of!=null){write();}else{if(sf==null){sf=new FileDialog(MyFrame.this,"保存文件",FileDialog.SAVE);sf.setBounds(300,200,400,400);sf.setVisible(true);path=sf.getDirectory();filename=sf.getFile();if(path!=null && filename!=null)write();}else{path=sf.getDirectory();filename=sf.getFile();if(path!=null && filename!=null)write();}}}});}//读取private void read(){ File f=new File(path,filename); BufferedReader br=null; ta.setText(""); try {br=new BufferedReader(new FileReader(f));String line=null;while((line=br.readLine())!=null){ta.append(line+"\r\n");} } catch (IOException o) { l.setText("读取失败");tishi.setVisible(true); } finally { try {if(br!=null)br.close(); } catch (IOException e) {l.setText("关闭流异常");tishi.setVisible(true); } }}//写入private void write(){ File f=new File(path,filename); BufferedWriter br=null; try {br=new BufferedWriter(new FileWriter(f));String text=ta.getText();br.write(text);br.flush(); } catch (IOException o) { l.setText("读取失败");tishi.setVisible(true); } finally { try {if(br!=null)br.close(); } catch (IOException e) {l.setText("关闭流异常");tishi.setVisible(true); } }}}class Demo4 {public static void main(String[] args) {new MyFrame();}}
双击执行jar程序
创建个清单文件list.txt
写入 Main-Class:空格包名.类名回车
注意空格,回车必须要
然后jar -cvfm jar名称 list.txt 类名
电脑系统盘目录-工具-文件夹选项-文件类型-jar-高级
-新建open -值为安装目录的javaw.exe目录 -jar(注意后面的-jar
是在exe的后面空格隔开)
—————————— ASP.Net+Android+IOS开发、.Net培训、期待与您交流!——————————
0 0
- 黑马程序员--- 学习笔记(第二十二天)
- 黑马程序员--第二十二天:图形化界面
- 黑马程序员--Java基础学习(GUI图形化界面)第二十二天
- 黑马程序员——java第二十二天:GUI
- 黑马程序员 —— 图形用户接口GUI (第二十二天)
- 黑马程序员---学习笔记
- 黑马程序员----学习笔记
- 黑马程序员----学习笔记
- 黑马程序员---学习笔记
- 第二十二天:听课笔记
- 第二十二天笔记xml
- 第二十二天笔记xml
- Java学习第二十二天
- 黑马程序员--- 学习笔记(第二天)
- 黑马程序员--- 学习笔记(第四天)
- 黑马程序员--- 学习笔记(第六天)
- 黑马程序员--- 学习笔记(第十天)
- 黑马程序员--- 学习笔记(第十二天
- Unity3d用户手册 AdvancediOS 设备故障排除
- 叫花鸡开户行手机卡带报道一下
- EJB到底是什么?
- SSH搭建环境
- 选择排序
- 黑马程序员--- 学习笔记(第二十二天)
- 自定义Ant任务
- TCP协议与UDP协议的区别
- jboss之启动加载过程详解(二)
- js中post提交
- 2014-04-16工作日志:error:用RegSetValueEx设置注册表的SZ_DWORD类型的值时,提示无访问内存的权限
- Hive.分组排序和TOP
- error_logger升级到lager:erlang日志框架
- 到货日期功能修改