黑马程序员——java基础学习笔记——第十四天

------ Java培训、Android培训、iOS培训、.Net培训 、期待与您交流! -------
GUI
一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(网格包布局管理器):非规则的矩阵。

简单的窗体创建过程:

Frame f = new Frame(“my window”);

f.setLayout(new FlowLayout());

f.setSize(500,400);//设置窗体大小

f.setLocation(300,200);//设置窗体出现在屏幕的位置

f.setVisible(true);


三事件监听

1.事件对象: 一般继承自java.util.EventObject对象,由开发者自行定义。

2.事件源: 就是触发事件的源头,不同的事件源会触发不同的事件类型。

3.事件监听器:事件监听器负责监听事件源发出的事件.一个事件监听器通常实现java.util.EventListener这个标识接口。


事件监听流程图:


事件监听机制
1.确定事件源(容器或组件)
2.通过事件源对象的addXXXListener()方法将侦听器注册到该事件源上。该方法中接收XXXListener的子类对象,或者XXXListener的子类XXXAdapter的子类对象。一般用匿名内部类来表示。在覆盖方法的时候,方法的参数一般是XXXEvent类型的变量接收。
3.事件触发后会把事件打包成对象传递给该变量。(其中包括事件源对象。通过getSource()或者,getComponent()获取。)

例子:


import java.awt.*;
import java.awt.event.*;


class MouseAndKeyEvent 
{
	private Frame f;
	private Button but;
	private TextField tf;

	MouseAndKeyEvent()
	{
		init();
	}

	public void init()
	{
		f = new Frame("my frame");

		f.setBounds(300,100,600,500);
		f.setLayout(new FlowLayout());

		tf = new TextField(20);

		but = new Button("my button");


		
		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);
			}
		});


		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()
		{
			public void keyPressed(KeyEvent e)
			{	
				if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
					//System.exit(0);
				System.out.println("ctrl+enter is run");

				//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());
			}
		});



		/*
		but.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.out.println("action ok");
			}
		});
		*/

		/*

		but.addMouseListener(new MouseAdapter()
		{
			private int count = 1;
			private int clickCount = 1;
			public void mouseEntered(MouseEvent e) 
			{
				System.out.println("鼠标进入到该组件"+count++);
			}
			public void mouseClicked(MouseEvent e)
			{
				if(e.getClickCount()==2)
					System.out.println("双击动作"+clickCount++);
			}
		});
		*/
	}






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

四菜单

MenuBar,Menu,MenuItem
先创建菜单条,再创建菜单,每一个菜单中建立菜单项。也可以菜单添加到菜单中,作为子菜单。通过setMenuBar()方法,将菜单添加到Frame中。

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MyMenuTest
{

	private Frame f;
	private MenuBar bar;
	private TextArea ta;
	private Menu fileMenu;
	private MenuItem openItem,saveItem,closeItem;


	private FileDialog openDia,saveDia;

	private File file;
	MyMenuTest()
	{
		init();
	}
	public void init()
	{
		f = new Frame("my window");
		f.setBounds(300,100,650,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);


	}
	private void 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 (IOException ex)
				{
					throw new RuntimeException();
				}
				
			}
		});


		openItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				openDia.setVisible(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);	
			}
		});
	}
	
	public static void main(String[] args) 
	{
		new MyMenuTest();
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值