黑马程序员 GUI学习

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

 

GUI简介

Graphical User Interface(图形用户接口)

java为GUI提供的对象都存在java.awt和java.swing包中。

java.awt:Abstract Window ToolKit,需要调用本地系统方法实现功能,属于重量级控件

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

 

组件继承关系图:

 

Container:是一个容器型组件,可以add其它组件进来。

 

布局管理器

管理容器中的组件的排放方式。

 

常见的布局管理器:

1、FlowLayout(流式):从左到右;Panel默认的布局管理器

2、BorderLayout(边界):东西南北中;Frame默认的布局管理器

3、GridLayout(网格):规则矩阵

4、CardLayout(卡片):选项卡

5、GridBagLayout(网格包):非规则矩阵

 

一个简单的窗体显示代码:

package cn.itcast.javaBase.gui;

import java.awt.*;
import java.awt.event.*;
public class FrameDemo extends Frame {
	
	//初始化窗体
	{	
		//初始化布局管理器
		LayoutManager ly = new FlowLayout();			
		this.setLayout(ly);
		
		this.setTitle("The First Frame!");
		this.setBounds(300, 300, 400, 300);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		//创建窗体对象
		FrameDemo myFrame = new FrameDemo();
		
		//添加窗口关闭监听事件
		myFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		//添加按键
		Button b = new Button("按键");
		myFrame.add(b);
	}
}


事件监听机制组成:

1、事件源 2、事件对象(Event) 3、监听器(Listener) 4、事件处理。

 

事件监听流程图:

 

 

 

常用监听器:WindowListener、ActionListener、MouseListener、KeyListener。

对话框:Dialog

菜单:MeunBar(菜单栏)、Menu(菜单)、MenuItem(子菜单)

Menu是MenuItem的子类 ,所以也可以作为MenuItem用。

/*
需求:演示用java.awt实现一个能打开、保存文本文件的记事本

思路:用到窗体、菜单
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Notepad {
	private Frame f;
	private MenuBar mb;
	private Menu fileMenu;
	private MenuItem openItem, saveItem, saveAnotherItem, exitItem;
	private TextArea ta;
	private FileDialog openDialog, saveDialog;
	private File file;;

	Notepad() {

		init();

	}

	public void init() {
		f = new Frame("Notepad_Melon");
		mb = new MenuBar();
		ta = new TextArea();
		

		fileMenu = new Menu("文件");
		openItem = new MenuItem("打开");
		saveItem = new MenuItem("保存");
		saveAnotherItem = new MenuItem("另存为");
		exitItem = new MenuItem("退出");
	
		
		f.setMenuBar(mb);
		f.add(ta);
		mb.add(fileMenu);
		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.add(saveAnotherItem);
		fileMenu.add(exitItem);

		f.setBounds(200,100,700,500);

		notepadEvent();

		f.setVisible(true);
	}

	private void notepadEvent() {
		f.addWindowListener(new WindowAdapter(){ //关闭窗口
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		exitItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);	
			}
		});

		openItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openDialog = new FileDialog(f, "打开Melon", FileDialog.LOAD);
				openDialog.setVisible(true);
				String dirPath = openDialog.getDirectory();
				String fileName = openDialog.getFile();
				if(dirPath == null || fileName == null)
					return;
				ta.setText("");
				file = new File(dirPath, fileName);
				try
				{
					BufferedReader br = new BufferedReader(new FileReader(file));
					String line = null;
					while((line=br.readLine()) != null) {
						ta.append(line + "\r\n");
					}
					br.close();
				}
				catch (IOException ex)
				{
					throw new RuntimeException("读取错误");
				}
			}
		});

		saveItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveDialog = new FileDialog(f, "保存Melon", FileDialog.SAVE);
				saveDialog.setVisible(true);
				if(file == null) {											
					String dirPath = saveDialog.getDirectory();
					String fileName = saveDialog.getFile();	
					if(dirPath == null || fileName == null)
						return;
					file = new File(dirPath, fileName);
				}
				try
				{
					BufferedWriter bw = new BufferedWriter(new FileWriter(file));
					String text = ta.getText();
					bw.write(text);
					bw.close();
				}
				catch (IOException ex)
				{
					throw new RuntimeException("写入错误");
				}
				
			}
		});

		saveAnotherItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveDialog = new FileDialog(f, "另存Melon", FileDialog.SAVE);
				saveDialog.setVisible(true);
														
				String dirPath = saveDialog.getDirectory();
				String fileName = saveDialog.getFile();	
				if(dirPath == null || fileName == null)
					return;
				file = new File(dirPath, fileName);
				
				try
				{
					BufferedWriter bw = new BufferedWriter(new FileWriter(file));
					String text = ta.getText();
					bw.write(text);
					bw.close();
				}
				catch (IOException ex)
				{
					throw new RuntimeException("写入错误");
				}
				
			}
		});
	}

	public static void main(String[] args) {
		new Notepad();
	}
	
}


 

 

 

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

 详情请查看:http://edu.csdn.net/heima

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值