黑马程序员之JAVA CUI



------http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一、概述

       Graphical User Interface,简称GUI,中文称为图形用户接口,用图形的方式来显示计算机的操作界面。与之对应的则是CLI,command-line interface,命令行用户接口,在早期的操作系统中则是用这样的方式和计算机交互。JAVA中为GUI提供了两个包,分别是java.Awt和javax.Swing。Awt需要调用本地方法实现功能,属于重量级控件;Swing在Awt的基础上建立的一套图形界面接口,提供了更多的接口,全由JAVA开发,移植性更强,是轻量级控件。

二、继承关系

        黑马程序员之JAVA CUI_第1张图片


三、示例

      

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


/**
 * @author 
 * 
 * 事件监听特点:
 * 1.事件源
 * 2.事件
 * 3.监听器
 * 4.事件处理
 * 
 * 事件源:Awt或者Swing包中的图形组件
 * 事件:每个事件源都有自己的特有事件和共性事件
 * 监听器:将每一个触发事件的动作封装到监听器中
 * 以上三个动作在JAVA中已经封装好了, 直接获取对象就好了.
 * 我们只需要对产生的动作进行处理就好了.
 * 
 * 
 *
 */
public class AwtDemo {

	public static void main(String[] args) {

		Frame frame = new Frame("my awt");
		
		//设置窗口大小
		frame.setSize(400,500);
		
		//设置窗口显示本地位置
		frame.setLocation(400, 100);
		
		//设置窗口是否显示
		frame.setVisible(true);
		
		//设置窗体布局方式
		frame.setLayout(new FlowLayout());
		
		//添加监听事件
		frame.addWindowListener(new MyWin());
		
		//添加一个按钮
		Button b = new Button("btn");
		frame.add(b);
		
	}

}


class MyWin extends WindowAdapter{
	
	@Override
	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
}

        创建菜单示例:

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MenuDemo {
	
	private Frame frame;
	private MenuBar bar;
	private Menu menu, subMenu;
	private MenuItem menuItem, subMenuItem;
	
	
	public MenuDemo() {
		init();
	}
	
	private void init(){
		frame = new Frame("my fream");
		
		//对frame进行基本设置
		frame.setBounds(200,100,400,500);
		frame.setLayout(new FlowLayout());
		
		bar = new MenuBar();
		
		menu = new Menu("菜单");
		menuItem = new MenuItem("退出");
		
		subMenu = new Menu("子项");
		subMenuItem = new MenuItem("hhh");
		
		subMenu.add(subMenuItem);
		menu.add(subMenu);	//添加子项, 在子菜单上添加菜单
		
		bar.add(menu);
		menu.add(menuItem);
		frame.setMenuBar(bar);
		
		getEvent();
		
		frame.setVisible(true);
	}
	
	private void getEvent(){
		
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		//添加监听事件
		menuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		
		
	}

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

}


        综合示例:

import java.awt.FileDialog;
//import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MyNotebook {
	
	private Frame frame;
	private MenuBar bar;
	private MenuItem open,save,close;
	private Menu menu;
	private FileDialog openDialog, saveDialog;
	private TextArea textArea;
	private File file;
	
	public MyNotebook() {
		init();
	}
	
	private void init(){
		
		frame = new Frame("my fream");
		
		//对frame进行基本设置
		frame.setBounds(200,100,400,500);
//		frame.setLayout(new FlowLayout());
		
		
		bar = new MenuBar();
		menu = new Menu("菜单");
		open = new MenuItem("打开");
		save = new MenuItem("保存");
		close = new MenuItem("退出");
		
		bar.add(menu);
		menu.add(open);
		menu.add(save);
		menu.add(close);
		
		frame.setMenuBar(bar);
		
		textArea = new TextArea();
		frame.add(textArea);
		
		getEvent();
		
		frame.setVisible(true);
		
	}
	
	
	private void getEvent(){
		
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		close.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		open.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				openDialog = new FileDialog(frame, "打开文件", FileDialog.LOAD);
				openDialog.setVisible(true);
				String path = openDialog.getDirectory();
				String name = openDialog.getFile();
				if(null == path || null == name)
					return;
				file = new File(path, name);
				
				
				//读取文件中的内容
				BufferedReader bufr = null;
				try {
					 bufr = new BufferedReader(new FileReader(file));
					String line = null;
					while(null != (line = bufr.readLine())){
						textArea.append(line + "\r\n");
					}
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}finally {
					try {
						bufr.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		
		save.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if(null == file){
					
					saveDialog = new FileDialog(frame, "保存文件", FileDialog.SAVE);
					saveDialog.setVisible(true);
					
					String path = saveDialog.getDirectory();
					String name = saveDialog.getFile();
					
					if(null == path || null == name)
						return;
					
					file = new File(path,name + ".txt");
				}
				
				//存储
				try {
					
					BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
					String txt = textArea.getText();
					bufw.write(txt);
					bufw.flush();
					bufw.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
	}
	
	
	
	

	public static void main(String[] args) {

		new MyNotebook();
	}

}



你可能感兴趣的:(JAVA)