黑马程序员_GUI回顾01
来源:互联网 发布:净水器 储水桶 知乎 编辑:程序博客网 时间:2024/06/05 23:40
------- android培训、java培训、期待与您交流! ----------
今天回顾GUI图形技术
我们调用了awt中event包中的WindowListener中的类,所以要再导入event包(特殊的操作,其他的不用)
import java.awt.event.*;
创建图形化界面:
1.创建frame窗体
2.对窗体进行基本的设置
比如,大小,位置,布局
3.定义组件
4.将组件通过窗体的add方法添加到窗体中
5.让窗体显示
事件监听机制的特点:
1.事件源
2.事件
3.监听器
4.事件处理
事件源:就是awt包或者swing保重的那些图形界面组件
事件:每一个事件源都有自己特有的对应事件和共性事件
监听器:将可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中
以上三者,在java中都已经定义好了,直接获取其对象来用就可以了.
我们要做的事情,就是对产生的动作进行处理
public class AwtDemo
{
public static void main(String[] args)
{
Frame f = new Frame ("My Frame");
f.setSize(500,400); //设置窗体的大小
f.setLocation(300,200); //设置窗体出现的本地位置
f.setLayout(new FlowLayout());//我们可以通过setLayout添加新的布局,默认情况下按钮的布局是自动填充,会很丑
Button b = new Button("My Button");
f.add(b);
//f.addWindowListener(new MyWin()) ;//调用我们继承WindowAdapter类并复写关闭方法的对象
//可以写一个匿名内部类来简化新建类并继承WindowAdapter类代码书写
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowActivated(WindowEvent e)
{
System.out.println("我活啦");
}
public void windowOpened(WindowEvent e)
{
System.out.println("我打开了");
}
});
f.setVisible(true); //让创建的窗体显示出来
}
}
class MywWin implements WindowListener
{
//覆盖7个方法,但我只用到了关闭的动作
//其他功能动作都没有用到,可是必须的得复写
//我们发现WindoesListener的子类WindowAdapter已经实现了WindowListener接口
//并覆盖了其中的所有方法,那么我们只要继承WindowsAdapter覆盖我们需要的方法即可
}
class MyWin extends WindowAdapter//继承WindowsAdapter并复写关闭方法
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
package com.GUI;
import java.awt.*;
import java.awt.event.*;
通常开发时awt图形化界面书写的格式
将事件和图形化界面及组件,分离开
class FrameDemo
{
//先定义该图形中所需的组件的引用
private Frame f;
private Button but;
FrameDemo()
{
init();
}
public void init()
{
f = new Frame("My Frame");
//对frame进行基本设置.
f.setBounds(300, 400, 300, 300);
f.setLayout(new FlowLayout());
but = new Button("My button");
//将组件添加到窗体中
f.add(but);
//加载窗体上的事件
myEvent();
//设置显示窗体
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
让按钮具备以下功能(例如:退出程序)
此时按钮就是事件源,我们要将监听器注册在按钮上
通过关闭窗体示例了解到,想要知道那个组件具备什么样的特有监听器
需要查看该组件的对象的功能.
通过查阅Button的API,它支持一个特有监听addActionListener(ActionListener l)
有监听我们就要往监听器中传入Listener接口,但通常Listener接口的方法我们不用都实现,
所以通常三个方法以上的的Listener接口都有个Adapter子类实现Listener的所有方法
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("按钮实现");
System.exit(0);
}
});
}
}
public class AwtDemo2
{
public static void main(String[] args)
{
new FrameDemo();
}
}
比较常用的窗体构造框架
将所窗体的基本参数封装
将所需要的组件封装
将所需要的事件封装
将封装的方法传入构造函数
package com.GUI;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyFrame
{
//定义组件 一个窗体 一个按钮 一个文本框,一个文本区域
private Frame f;
private Button but;
private TextField tf;
private TextArea ta;
private Dialog d ;
private Button ok;
private Label lb;
MyFrame() //窗体构造方法
{
init();
}
public void init()
{
f = new Frame("My Frame");
but = new Button("转到");
tf = new TextField("",30);
ta = new TextArea(10,50);
d = new Dialog(f);
ok = new Button ("OK");
lb = new Label();
f.setBounds(300, 300, 400, 400);
f.setLayout(new FlowLayout());
d.setBounds(300,300,210,70);
d.setLayout(new FlowLayout());
d.add(lb);
d.add(ok);
f.add(but);
f.add(tf);
f.add(ta);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
//定义ok按钮和Dialog的关闭效果,其实就是将Dialog的Visible设置成false
//给ok按钮也加上关闭效果,加强用户体验
ok.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
d.setVisible(false);
}
});
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//获取输入的文本数据
/* 为了演示读取路径下文件,将此处注释
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str = tf.getText();
ta.setText(str); //将文本框中数据显示在文本区域中
//System.out.println(str);
tf.setText(""); //清空文本框
}
});
*/
//在文本框中输入路径,让文本区域显示路径
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ta.setText(""); //每次读取路径下文件之前,将旧文本区域数据清除掉
String dirPath = tf.getText();
File dir = new File(dirPath); //将输入的数据封装成File对象
if(dir.exists()&& dir.isDirectory()) //判断File对象是否存在并且是否是目录
{
String [] names = dir.list(); //如果成立将dir.list()建立成String数组
for(String name : names) //遍历该数组
{
ta.append(name+"\r\n");
}
}
else
{
//通过这种方法返回错误提示不够理想
//ta.setText("路径错误");
//模拟windowsxp的Dialog错误对话框进行错误提示
/*
window的Dialog有三个组件组成,按钮,文本Label和Dialog本身
分别在构造函数中定义,开发时这种不肯定出现的对象应该在特殊情况下定义
在这里将Dialog的Visible设置成true让它出现
*/
d.setVisible(true);
String info = dirPath+"是非法路径";
lb.setText(info);
}
}
});
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
showDir();
}
}
});
}
private void showDir()
{
ta.setText(""); //每次读取路径下文件之前,将旧文本区域数据清除掉
String dirPath = tf.getText();
File dir = new File(dirPath); //将输入的数据封装成File对象
if(dir.exists()&& dir.isDirectory()) //判断File对象是否存在并且是否是目录
{
String [] names = dir.list(); //如果成立将dir.list()建立成String数组
for(String name : names) //遍历该数组
{
ta.append(name+"\r\n");
}
}
else
{
//通过这种方法返回错误提示不够理想
//ta.setText("路径错误");
//模拟windowsxp的Dialog错误对话框进行错误提示
/*
window的Dialog有三个组件组成,按钮,文本Label和Dialog本身
分别在构造函数中定义,开发时这种不肯定出现的对象应该在特殊情况下定义
在这里将Dialog的Visible设置成true让它出现
*/
d.setVisible(true);
String info = dirPath+"是非法路径";
lb.setText(info);
}
}
}
public class AwtTest
{
public static void main(String[] args)
{
new MyFrame();
}
}
- 黑马程序员_GUI回顾01
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI 详谈
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI编程
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI
- 黑马程序员_GUI
- android实现3D效果翻页
- oracle row_number over
- P6spy + sqlprofiler -持久化动态监测框架
- 最大公约数和最小公倍数求法 C++
- 经典alsa 录音和播放程序
- 黑马程序员_GUI回顾01
- 程序员写小说,也太难了
- Java JSON技术框架选型与实例
- 在DOS脚本中加入计数器
- 二叉树的遍历(前序,中序,后序)
- ubuntu中将用户加入sudo group的最好方法
- 网络编程简单例子
- 快速提高 Vi/Vim 使用效率的原则与途径
- Android adapter.notifyDataSetChanged无效