黑马程序员——GUI
来源:互联网 发布:淘宝达人账号简介 编辑:程序博客网 时间:2024/05/17 00:57
---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
(教22-01)
GUI
Graphical User Interface(图形用户接口)
java为GUI提供的对象都存在java.Awt和javax.Swing包中。
java.Awt: Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现,增强了移植性,属轻量级控件。
Container:为容器,是一个特殊的组件,该组件中可以通过add方法添加其他组件进来。
布局管理器:容器中的组件的排放方式,就是布局。
常见的布局管理器:
FlowLayout(流式布局管理器):从左到右的顺序排列。Panel默认的布局管理器。
BorderLayout(边界布局管理器):东南西北中,Frame默认的布局管理器。
GridLayout(网格布局管理器):规则的矩阵。
CardLayout(卡片布局管理器):选项卡。
GridBagLayout(网格包布局管理器):非规则的矩阵。
创建图形化界面:
1、创建frame窗体。
2、对窗体进行基本设置。
比如大小,位置,布局。
3、定义组件。
4、将组件通过窗体的add方法添加到窗体中。
5、让窗体显示,通过setVisible(true).
事件监听机制的特点:
1、事件源。
2、事件。
3、监听器。
4、事件处理。
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中。
以上三者,在java中都已经定义好了。
直接获取其对象来用就可以啦。
我们要做的事情是,对产生的动作进行处理。
import java.awt.*;import java.awt.event.*;class AwtDemo{ publicstatic void main(String[] args) { Framef=new Frame("my awt"); f.setSize(500,400); f.setLocation(300,200); f.setLayout(newFlowLayout()); Buttonb=new Button("我是一个按钮"); f.add(b); f.addWindowListener(newMyWin()); f.setVisible(true); System.out.println("HelloWorld!"); }}//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口。//并覆盖了其中的所有方法,那么只要继承自Windowadapter覆盖我需要的方法即可。class MyWin extends WindowAdapter{ publicvoid windowClosing(WindowEvent e) { System.out.println("windowclosing"); System.exit(0); }}
小程序,关于按钮:
import java.awt.*;import java.awt.event.*;class FrameDemo{ //定义该图形中所需的组件引用 privateFrame f; privateButton but; FrameDemo() { init(); } publicvoid init() { f=new Frame("my frame"); //对frame进行基本设置 f.setBounds(300,100,600,500); f.setLayout(newFlowLayout()); but=newButton("my button"); //将组件添加到frame中。 f.add(but); //加载一下窗体上事件 myEvent(); //显示窗体 f.setVisible(true); } privatevoid myEvent() { f.addWindowListener(newWindowAdapter() { publicvoid windowClosing(WindowEvent e) { System.exit(0); } }); //让按钮具备退出程序的功能。 /* 按钮就是事件源。 那么选择哪个监听器? 通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。 需要查看该组件对象的功能, 通过查阅button的描述,发现按钮支持一个特有监听addActionListener。 */ but.addActionListener(newActionListener() { publicvoid actionPerformed(ActionEvent e) { System.out.println("退出,按钮。"); System.exit(0); } }); } publicstatic void main(String[] args) { newFrameDemo(); }}
练习:设计如下图程序
import java.awt.*;import java.awt.event.*;import java.io.*;class MyWindowDemo{ privateFrame f; privateButton but; privateTextField tf; privateTextArea ta; privateDialog d; privateLabel lab; privateButton okBut; MyWindowDemo() { init(); } publicvoid init() { f=new Frame("my window"); f.setBounds(300,100,600,500); f.setLayout(newFlowLayout()); tf=newTextField(40); but=newButton("转到"); ta=newTextArea(25,50); d=newDialog(f,"提示信息",true); d.setBounds(400,200,240,100); d.setLayout(newFlowLayout()); lab=newLabel(); okBut=newButton("确定"); d.add(lab); d.add(okBut); f.add(tf); f.add(but); f.add(ta); myEvent(); f.setVisible(true); } privatevoid myEvent() { d.addWindowListener(newWindowAdapter() { publicvoid windowClosing(WindowEvent e) { d.setVisible(false); } }); okBut.addActionListener(newActionListener() { publicvoid actionPerformed(ActionEvent e) { d.setVisible(false); } }); f.addWindowListener(newWindowAdapter() { publicvoid windowClosing(WindowEvent e) { System.exit(0); } }); tf.addKeyListener(newKeyAdapter() { publicvoid keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_ENTER) showDir(); } }); but.addActionListener(newActionListener() { publicvoid actionPerformed(ActionEvent e) { showDir(); } }); } publicvoid showDir() { StringdirPath=tf.getText(); Filedir=new File(dirPath); if(dir.exists()&&dir.isDirectory()) { ta.setText(""); String[]names=dir.list(); for(Stringname:names) { ta.append(name+"\r\n"); } } else { Stringinfo="您输入的信息错误,请重输!"; lab.setText(info); d.setVisible(true); } } publicstatic void main(String[] args) { newMyWindowDemo(); }}
菜单演示:如下图
import java.awt.*;import java.awt.event.*;import java.io.*;class MyWindowDemo{ privateFrame f; privateButton but; privateTextField tf; privateTextArea ta; privateDialog d; privateLabel lab; privateButton okBut; MyWindowDemo() { init(); } publicvoid init() { f=new Frame("my window"); f.setBounds(300,100,600,500); f.setLayout(newFlowLayout()); tf=newTextField(40); but=newButton("转到"); ta=newTextArea(25,50); d=newDialog(f,"提示信息",true); d.setBounds(400,200,240,100); d.setLayout(newFlowLayout()); lab=newLabel(); okBut=newButton("确定"); d.add(lab); d.add(okBut); f.add(tf); f.add(but); f.add(ta); myEvent(); f.setVisible(true); } privatevoid myEvent() { d.addWindowListener(newWindowAdapter() { publicvoid windowClosing(WindowEvent e) { d.setVisible(false); } }); okBut.addActionListener(newActionListener() { publicvoid actionPerformed(ActionEvent e) { d.setVisible(false); } }); f.addWindowListener(newWindowAdapter() { publicvoid windowClosing(WindowEvent e) { System.exit(0); } }); tf.addKeyListener(newKeyAdapter() { publicvoid keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_ENTER) showDir(); } }); but.addActionListener(newActionListener() { publicvoid actionPerformed(ActionEvent e) { showDir(); } }); } publicvoid showDir() { StringdirPath=tf.getText(); Filedir=new File(dirPath); if(dir.exists()&&dir.isDirectory()) { ta.setText(""); String[]names=dir.list(); for(Stringname:names) { ta.append(name+"\r\n"); } } else { Stringinfo="您输入的信息错误,请重输!"; lab.setText(info); d.setVisible(true); } } publicstatic void main(String[] args) { newMyWindowDemo(); }}
jar包打包总结:
1、先在编写的程序上添加包名 package mymenu
2、将编译的.class文件放到一个文件夹中:javac –d c:\myclassMyMenuDemo.java
3、添加配置信息,新建一个txt文件,在里面添加主类名信息,注意空格和最后回车。
4、转到编译文件根目录,打包:jar –cvfm my.jar 1.txt mymenu
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
- 黑马程序员—GUI
- 黑马程序员—GUI
- 黑马程序员 — GUI
- 黑马程序员—GUI
- 黑马程序员—GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- 黑马程序员——GUI
- hibernate集合映射
- org.hibernate.exception.SQLGrammarException: could notinsert: [com.michael.model.Storen]
- el表达式
- Struts2的Stack Context和ValueStack
- spring一些知识
- 黑马程序员——GUI
- [自学问题总结]静态成员和非静态成员的区别
- struts2类型转换
- struts2标签不显示Label属性
- jsp自定义标签
- poj 2033 Alphacode
- Filter与Servlet的区别
- struts2 Action访问Servlet Api
- oracle使用汉字首字母查询函数