/*
创建图形化界面:
1.创建frame窗体。
2.对窗体进行基本设置。
比如大小、位置、布局。
3.定义组件。
4.将组件通过窗体+6+的add方法添加到窗体中。
5.让窗体显示,通过setVisible(true);
事件监听机制的特点:
1.事件源。
2.事件。
3.监听器。
4.事件处理。
事件源:就是awt包或者swing包中的那些图形化界面组件。
事件:每一个事件源都有自己的特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。
以上三者,在java中都已经定义好了。
直接获取其对象来用就可以了。
我们要做的事情是,就是对产生的动作进行处理。
*/
import java.awt.*;
import java.awt.event.*;
class AwtDemo{
public static void main(String[] args) {
Frame f = new Frame("my awt");
f.setSize(500,400);
f.setLocation(300,200);
f.setLayout(new FlowLayout());
Button b = new Button("我是一个按钮");
f.add(b);
f.addWindowListener(new MyWin());//也可以用匿名内部类
/*
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
*/
f.setVisible(true);
}
}
//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口
//并覆盖了其中的所有方法。那么只要继承自WindowAdapter覆盖我需要的方法即可。
class MyWin extends WindowAdapter{
public void windowClosing(WindowEvent e){
//System.out.println("window closing");
System.exit(0);
}
}
/*--------------------------------------------------------------------------*/
//一般写法
import java.awt.*;
import java.awt.event.*;
class FrameDemo(){
//定义该图形中所需要的组件引用
private Frame f;
private Button but;
private TextField tf;
FrameDemo(){
init();
}
public void init(){
f = new Frame("my frame");
//对frame进行基本设置
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
but = new Button("my button");
tf = new TextField(10);//构造函数为列数
//将组件添加到frame中
f.add(tf);
f.add(but);
//加载一下窗体事件
myEvent();
//显示窗体
f.setVisible(true);
}
private void myEvent(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
});
}
/*按钮事件*/
//让按钮具备退出程序的功能
/*
按钮就是事件源。
通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。
需要查看该组件对象的功能。
通过查阅button的描述,发现按钮支持一个特有监听addActionListener().
*/
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
//鼠标事件
but.addMouseListener(new MouseAdapter(){
private int count = 1;
private int clickCount = 1;
private int doubleCount = 1;
public void mouseEntered(MouseEvent e){
System.out.println("鼠标进入该组件次数:"+count++);
}
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2)
System.out.println("双击击动作:"+doubleCount++);
else
System.out.println("点击动作:"+clickCount++);
}
});
//给Button添加键盘事件
but.addKeyListener(new KeyAdapter(){
//e.getKeyChar();--获取键盘按键
//e.getKeyCode();--获取键盘按键的值
//KeyEvent.getKeyText(e.getKeyCode());
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
System.exit(0);
/*
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
*/
}
});
//文本框的键盘事件
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(!(e.getKeyCode()>=KeyEvent.VK_0 && e.getKeyCode()<=KeyEvent.VK_9))
e.consume();//屏蔽键
}
});
}
public static void main(String[] args){
new FrameDemo();
}
}
/*----------------------------------------------------------------*/
//文本框
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindowDemo{
private Frame f;
private TextField tf;
private Button btn;
private TextArea ta;
private Dialog d;
private Label l;
private Button okBtn;
MyWindowDemo(){
init();
}
public void init(){
f = new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
btn = new Button("转到");
ta = new TextArea(25,70);
d = new Dialog(f,"提示信息",true);
d.setBounds(400,200,300,150);
d.setLayout(new FlowLayout());
l = new Label();
okBtn = new Button("确定")
d.add(lab);
d.add(okBtn);
f.add(tf);
f.add(btn);
f.add(ta);
myEvent();
f.setVisible(true);
}
public void myEvent(){
f.addWindowListener(new WindowListener(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
d.addWindowListener(new WindowListener(){
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
});
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
showDir();
}
});
okBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setVisible(false);
tf.setText("");
}
});
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
});
public void showDir(){
String path = tf.getText();
File dir = new File(path);
if(dir.exists()&&dir.isDirectory()){
ta.setText("");
String[] names = dir.list();
for(String name : names){
ta.append(name+"\r\n");
}
}else{
String info = "您输入的信息:"+path+"是错误的,请重新输入!";
l.setText(info);
d.setVisible(true);
}
}
}
public static void main(String[] args){
new MyWindowDemo();
}
}
/*-----------------------------------------------*/
//菜单+打开保存文件对话框。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenuDemo{
private Frame f;
private MenuBar mb;
private TextArea ta;
private Menu fileMenu,subMenu;
private MenuItem closeItem,openItem,savaItem,subItem;
private File file;
private FileDialog openDialog,saveDialog;
MyMenuDemo(){
init();
}
public void init(){
f = new Frame("my window");
f.setBounds(300,100,650,600);
//f.setLayout(new FlowLayout());不设置为边界布局
mb = new MenuBar();
ta = new TextArea();
fileMenu = new Menu("文件");
subMenu = new subMenu("子菜单");
openItem = new MenuItem("打开");
saveItem = new MenuItem("保存");
subItem = new MenuItem("子条目");
closeItem = new MenuItem("退出");
subMenu.add(subItem);
fileMenu.add(openItem);
fileMenu.add(savaItem);
fileMenu.add(subItem);
fileMenu.add(closeItem);
mb.add(fileMenu);
f.setMenuBar(mb);
openDialog = new FileDialog(f,"打开",FileDialog.LOAD);//打开对话框
saveDialog = new FileDialog(f,"保存",FileDialog.SAVE);//保存对话框
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
closeItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
openDialog.setVisible(true);
String path = openDialog.getDirectory();
String name = openDialog.getFile();
if(path == null || name == null)
return;
ta.setText("");
file = new File(path,name);
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(file));
String line = null;
while((line = br.readLine()) != null){
ta.append(line+"\r\n");
}
}catch (IOException ex){
throw new RuntimeException("读取失败!");
}finally{
try{
if(br != null)
br.close();
}catch (IOException ee){
throw new RuntimeException("关闭流失败!");
}
}
}
});
saveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(file == null){
saveDialog.setVisible(true);//判定文件存不存在再决定弹不弹出对话框
String path = openDialog.getDirectory();
String name = openDialog.getFile();
if(path == null || name == null)
return;
file = new File(path,name);
}
BufferedWriter bw = null;
try{
bw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bw.write(text);
}catch (IOException ex){
throw new RuntimeException();
}finally{
try{
if(bw != null)
bw.close();
}catch (IOException ee){
throw new RuntimeException();
}
}
}
});
}
public void main(String[] args){
new MyMenuDemo();
}
}
/*
javac -d C:\myclass MyMenuTest.java//把.class文件编译到C盘myclass文件夹下,生成mynemu文件夹
jar -cvf my.jar mynemu
//给jar包添加自定义属性
新建一个文件夹"1.txt",里面写:
Main-Class: mynemu.MyMenuTest //Main-Class: 空格+包名.类名 后面有个回车。
jar -cvfm my.jar 1.txt mymenu
jar包双击可执行
*/
javaGUI
最新推荐文章于 2022-10-21 00:14:23 发布