黑马程序员_GUI

------- android培训java培训、期待与您交流! ----------

GUI

Graphical User Interface(图形用户接口)。

java为GUI提供的对象都存在java.Awt和javax.Swing两个包中。

java.Awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。

javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由java实现。增强了移植性,属轻量级控件。

容器中的组件的排放方式,就是布局

常见的布局管理器:

FlowLayout(流式布局管理器)

从左到右的顺序排列。

Panel默认的布局管理器。

BorderLayout(边界布局管理器)

东,西,南,北,中

Frame默认的布局管理器。

GridLayout(网格布局管理器)

规则的矩阵

CardLayout(卡片布局管理器)

选项卡

GridBagLayout(网格包布局管理器)

非规则的矩阵

创建图形化界面

1、创建frame窗体

2、对窗体进行基本设置,比如大小,位置,布局

3、定义组件

4、将组件通过窗体的add方法添加到窗体中

5、让窗体显示,通过setVisible(true);

import java.awt.*;
import ava.awt.event.*;
class AwtDemo
{
	publicstatic void main(String[] args)
	{
		Frame f=new Frame(“my awt”);//生成窗体。默认边界式布局
		f.setSize(500,100);//设置横纵坐标
		f.setLocation(300,200)//顶点左边的距离
		f.setLayout(new FlowLayout());
		f.setVisible(true);//显示窗体
		Button b=new Button(“我是按钮”);
		f.add(b);
		f.addWindowListener(new Mywin ());
	}
}
//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口并覆盖了其中的所有方法,那么只要继承自Windowadapter覆盖我需要的方法即可。
class Mywin extendsWindowAdapter//java.awt.event包里面
{
	puiblicvoid windowClosing(WindowEvent e)
	{
		//System.out.println(“window closing”);
		System.exit(0);
	}
}


事件监听机制的特点:

1、事件源

2、事件

3、监听器

4、事件处理

事件源:就是awt包或者swing包中的那些图形界面组件。

事件:每一个事件源都有自己特有的对应事件和共性事件。

监听器:将可以出发某一个事件的动作(不止一个动作)都已经封装到了监听器中。

以上三者:在java中都已经定义好了,直接获取其对象来用就可以了。

我们要做的事情是,就是对产生的动作进行处理。

import java.awt.*;
import java.awt.event.*;
class FrameDemo
{
	//定义该图形中所需的组件引用
	privateFrame f;
	privateButton but;
	FrameDemo()
	{
		init();
	}
	publicvoid init()
	{
		f=new Frame();
		//对frame进行基本设置
		f.setBounds(15,15,600,500);//移动组件并调整大小
		f.setLayout(new FlowLayout());
		but=new Button(“mybutton”);
		//将组件添加到frame中
		f.add(but);
		//加载一下窗体上事件
		myEvent();
		//显示窗体
		f.setVisible(true);
	}
	privatevoid myEvent()
	{
		f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		//让按钮具备退出程序的功能
/*
按钮就是事件源
那么选择哪一个监听器呢?通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器,需要查看该组件对象的功能。通过查阅button的描述,发现按钮支持一个监听addActionListen
*/
		but.addActionListener(new ActionListener()//只有一个方法,少数没有适配器接口的其中一个,只要方法超过三个的几乎listener都有适配器,
		{
			public void actionPerformed(ActionEvent e)//添加的活动监听,返回的应该是活动事件
			{
				System.out.println(“退出,按钮干的”);
				System.exit(0);
			}
		});
	}
	publicstatic void main(String[] args)
	{
		new FrameDemo();
	}
}

import java.awt.*;
import java.awt.event.*;
class MouseAndKeyEvent
{
	privateFrame f;
	privateButton but;
	privateTextField tf;
	MouseAndKeyEvent()
	{
		init();
	}
	publicvoid init()
	{
		f=new Frame(“myframe”);
		f.setBounds(300,100,600,500);
		f.setLayout(new FlowLayout());
		tf=new TextFiled(20);
		but=new Button(“mybutton”);
		f.add(but);
		f.add(tf);//添加文本框
		myEvent();
		f.setVisible(true);
	}
	privatevoid myEvent()
	{
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		tf.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				int code=e.getKeyCode();
				if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9))
				{
					System.out.println(code+”....是非法的”);
					e.consume();//停止默认操作
				}
			}
		});
		//给But添加一个键盘监听
		but.addKeyListener(new KeyAdapter()//new 适配器
		{
			public void keyPressed(KeyEvent e)
			{
				System.out.println(e.getKeyChar()+”...”+e.getKeyCode());//按键及对应的码
				System.out.println(KeyEvent.getKeyText(e.getKeyCode())+”...”+e.getKeyCode());//根据码获取键值
				//按esc就可以结束
				if(e.getKeyCode()==KeyEvent.VK_ESCAPE)//27)
				System.exit(0);
				//按下键ctrl+enter组合键
				if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
				System.out.println(“ctrl+enter is run”);
			}
		});
		but.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				System.out.println(“action ok”);
			}
		});
		but.addMouserListener(new MouseAdapter(){
			private int count=1;
			public void mouseEntered(MouseEvent e)
			{
				System.out.println(“鼠标进入该组件”+count++);
			}
			public void mouseClicked(MouseEvent e)
			{
				if(e.getClickCount()==2)
				System.out.println(“双击动作”+clickCount++);
			}
		});
	}
	publicstatic void main(String[] args)
	{
		new MouseAndKeyEvent();
	}
}


//菜单

MenuBar类,往Frame里面添加菜单用setMenuBar方法。

import java.awt.*;
import java.awt.event.*;
class MyMenuDemo
{
	privateFrame f;//Frame
	privateMenuBar mb;
	privateMenu m,subMenu;//没有子菜单
	privateMenuItem closeItem,subItem;//有子菜单
	MyMenuDemo()
	{
		init();
	}
	publicvoid init()
	{
		f=new Frame(“my window”);
		f.setBounds(300,100,500,600);
		f.setLayout(new FlowLayout());
		mb=new MenuBar();
		m=new Menu(“文件”);
		subMenu=new Menu(“子菜单”);
		subItem=new MenuItem(“子条目”);
		closeItem=new MenuItem(“退出”);
		subMenu.add(subItem);
		m.add(subItem);
		m.add(closeItem);
		mb.add(m);
		f.setMenuBar(mb);
		myEvent();
		f.setVisible(true);
	}
	privatevoid myEvent()
	{
		closeItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
	publlicstatic void main(String[] args)
	{
		MyMenuDemo();
	}
}

//打开文件

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenuTest
{
	privateFrame f;
	privateMenuBar bar;
	privateTextArea ta;
	privateMenu fileMenu;
	privateMenuItem openItem,saveItem,closeItem;
	privateFileDialog openDia,saveDia;
	privateFile file;
	MyMenuTest()
	{
		init();
	}
	publicvoid init()
	{
		f=new Frame(“my window”);
		f.setBounds(300,100,500,600);
		bar=new MenuBar();
		ta=new TextArea();
		fileMenu=new Menu(“文件”);
		openItem=new MenuItem(“打开”);
		saveItem=new MenuItem(“保存”);
		closeItem=new MenuItem(“退出”);
		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.add(closeItem);
		bar.add(fileMenu);
		f.setMenuBar(bar);
		openDia=new FileDialog(f,”打开”,FileDialog.LOAD);
		saveDia=new FileDialog(f,”保存”,FileDialog.SAVE);
		f.add(ta);
		myEvent();
		f.setVisible(true);
	}
	privatevoid myEvent()
	{
		saveItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(file==null)
				{
					saveDia.setVisible(true);
					String dirPath=saveDia.getDirectory();
					String fileName=saveDia.getFile();
					if(dirPath==null||fileName==null)
					return;
					file=new File(dirPath,fileName);
				}
				try
				{
					BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
					String text=ta.getText();
					bufw.write(text);
					bufw.flush();
					bufw.close();
				}
				catch(IOExcetion ex)
				{
					throw new RuntimeException();
				}
			}
		});
		openItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				openDia.setVisiable(true);
				String dirPath=openDia.getDirectory();
				String fileName=openDia.getFile();
				//System.out.println(dirPath+”...”+fileName);
				if(dirPath==null||fileName==null)
				return;
				ta.setText(“”);
				file=new File(dirPath,fileName);
				try
				{
					BufferedReader bufr=new BufferedReader(new FileReader(file));
					String line=null;
					while((line=bufr.readLine())!=null)
					{
						ta.append(line+”\r\n”);
					}
					bufr.close();
				}
				catch(IOException ex)
				{
					throw new RuntimeException(“读取失败”);
				}
			}
		});
		closeItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
	publlicstatic void main(String[] args)
	{
		MyMenuTest();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值