黑马程序员-----GUI

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

 

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

       用图形的方式,来显示计算机操作的界面,更方便、更直观。

CLI(Command line User Interface ) 命令行用户接口

       就是常见的Dos命令行操作

 

Java为GUI提供的对象都存在java.Awtjavax.Swing两个包

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

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

 

Eclipse 是由Swt界面设计

 

布局管理器

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

常见的布局管理器:

       FlowLayout(流式布局管理器)

              从左到右的顺序排列

              Panel默认的布局管理器

       BorderLayout(边界布局管理器)

              东南西北中

              Frame默认的布局管理器

       GridLayout(网格布局管理器)

              规则的矩阵

       GridBagLayout(网格包布局管理器)

              非规则的矩阵

       CardLayout(卡片布局管理器)

              选项卡

创建图形化界面:

1,  创建frame窗体

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

3,  定义组件

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

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

 

事件监听机制

特点:

1,  事件源:Awt包或者Swing包的那些图形界面组件

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

3,  监听器:将可以触发某一个事件的动作(不只一个)已经封装到监听器中。

4,  事件处理方式:

1,2,3 java已经定义好了,我们要做的事情是4,事件的处理方式。

 

Java.awt.Window

       addWindowListener(WindowListener l);

       Interface WindowListener

              |---WindowAdapter :实现了WindowListener接口,并覆盖了所有方法

 

监听器

Frame.addWindowListener(new WindowAdapter(){}); 框架的监听

Button.addActionListener(new ActionListener(){});    按钮的活动监听

Button.addMouseListener(new MouseAdapter(){}); 按钮的鼠标监听

Button.addKeyListener(new KeyAdapter(){});    按钮的键盘监听

TextField.addKeyListener(new KeyAdapter(){}); 文本框的键盘监听

 

制作双击执行的Jar 包

如:package mymenu

1,编译将生成的class文件放在一个包中

Javac –d c:/myclass MyMenuDemo.java

2,建一个Jar配置信息的文件1.txt(随意起名)

Main-Class: mymenu.MyMenuDemo  (冒号后面一定要有空格;结尾要有回车)

3,生成可以双击执行的jar包

jar –cvfm my.jar 1.txt mymenu

 

两个小例子:一个显示window窗口,一个是菜单显示

/*
window窗口设计
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class  MyWindow
{
	private Frame f;
	private TextField tf;
	private Button but;
	private TextArea ta;
	private Dialog d;
	private Label lab;
	private Button okBut;
	MyWindow()
	{
		init();
	}
	//初始化
	public void init()
	{
		f = new Frame("my window");
		f.setBounds(300,200,600,500);
		f.setLayout(new FlowLayout());
		
		d = new Dialog(f,"提示信息",true);
		lab = new Label();
		okBut = new Button("确定");
		d.setBounds(300,200,250,150);
		d.setLayout(new FlowLayout());
		d.add(lab);
		d.add(okBut);

		tf = new TextField(70);
		but = new Button("转到");
		ta = new TextArea(25,78);


		f.add(tf);
		f.add(but);
		f.add(ta);
		
		myEvent();
		f.setVisible(true);
	}
	//事件
	public void myEvent()
	{
		d.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				d.setVisible(false);
			}
		});
		okBut.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				d.setVisible(false);
			}
		});

		tf.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					showDir();
			}
		});

		but.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				showDir();
			}
		});
		f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
	//显示目录
	private void showDir()
	{
		
		String dirPath = tf.getText();

		File dir = new File(dirPath);
		if(dir.exists() && dir.isDirectory())
		{
			ta.setText("");
			String[] names = dir.list();
			for(String name : names)
			{
				ta.append(name+"\r\n");
			}
		}
		else
		{
			String info = "您输入的信息"+dirPath+"是错误的,请重输";
			lab.setText(info);
			d.setVisible(true);
		}

		tf.setText("");
		
	}
	public static void main(String[] args) 
	{
		new MyWindow();
	}
}
/*
window简单菜单设计
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyMenuDemo 
{
	private Frame f;
	private MenuBar mb;
	private Menu m,newFile;
	private MenuItem closeItem,openFile,saveFile,htmlFile;
	private FileDialog openDialog,saveDialog;
	private TextArea ta;
	private File file;
	MyMenuDemo()
	{
		init();
	}
	//初始化
	public void init()
	{
		f = new Frame("my window");
		f.setBounds(300,200,600,500);
		
		mb = new MenuBar();
		ta = new TextArea();
		m = new Menu("文件");
		newFile = new Menu("新建");
		
		openFile = new MenuItem("打开");
		saveFile = new MenuItem("保存");
		closeItem = new MenuItem("关闭");
		htmlFile = new MenuItem("HTML网页");

		openDialog = new FileDialog(f,"我的打开",FileDialog.LOAD);
		saveDialog = new FileDialog(f,"我的保存",FileDialog.SAVE);
		
		f.setMenuBar(mb);
		f.add(ta);
		mb.add(m);
		m.add(newFile);
		m.add(openFile);
		m.add(saveFile);
		m.add(closeItem);
		newFile.add(htmlFile);
		
		myEvent();
		f.setVisible(true);
		
	}
	//事件
	public void 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);
			}
		});
		//打开菜单的事件
		openFile.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				openDialog.setVisible(true);
				String dirPath = openDialog.getDirectory();
				String fileName = openDialog.getFile();
				
				if(dirPath==null || fileName==null)
					return;
				File 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("读取错误");
				}

			}
		});
		//保存菜单的事件
		saveFile.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(file==null)
				{
					saveDialog.setVisible(true);
					String dirPath = saveDialog.getDirectory();
					String fileName = saveDialog.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.close();
				}
				catch (IOException ex)
				{
					throw new RuntimeException("写失败");
				}
			}
		});

		
	}

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



 

-------------------------------------------- android培训java培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值