GUI(界面化布局)+简单记事本软件【JAVA基础】

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

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

AWT:Abstract Window Toolkit,抽象窗口开发包。重量级组件。在Windows系统与Linux系统都可运行,但由于是调用操作系统实现的组件,在两个平台运行后并不是完全相同,可移植性一般。

AWT最基本组成部分是组件(Component)

Component是一个具有图形表示能力的对象

Component
Window:顶级窗口,可独立存在
Panel: 确定一个四边形,可以添加其他组件(add()方法),但必须放在Window或Window的子类之中,才能显示出来。
Frame:可以添加其他组件(add()方法)的窗口,具有标题和缩放角
Dialog :是一个带标题和边界的顶层窗口,边界一般用于从用户处获得某种形式的输入
TextComponent:允许编辑文本的组件的超类
TextField:创建文本框对象
TextArea:显示文本的多行区域。可以将它设置为允许编辑或只读
Button:标签按钮。当按下该按钮时,应用程序能执行某项动作
Label:显示一行只读文本。文本可由应用程序更改,但是用户不能直接对其进行编辑
FileDialog:文件对话框。
FileDialog构造函数:
FileDialog openDia=new FileDialog(f,“我要打开”,FileDialog.LOAD);//模式:打开
FileDialog saveDia=new FileDialog(f,“我要保存”,FileDialog.SAVE) ;//模式:保存

布局管理器:

容器中的组件的排放方式,就是布局。
常见的布局管理器:
FlowLayout(流式布局管理器)
从左到右的顺序排列。
Panel默认的布局管理器
BorderLayout(边界布局管理器)
东,南,西,北,中
Frame默认的布局管理器
GridLayout(网格布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵

创建图形化界面的步骤:

1.创建frame窗体。
2.对窗体进行基本设置
如:大小 位置 布局
3.定义组件。
4.将组件通过窗体的add方法添加到窗体中.
5.让窗体显示,通过setVisible(true).

事件监听机制:

特点:
1.事件源:awt包或者swing包中的那些图形界面组件
2.事件 每一个事件源都有自己特有的对应事件和共性事件
3.监听器:将可以触发某个事件的动作都已经封装到了监听器中
4.事件处理:对产生的动作进行处理。

常用案例

窗口监听(适配器)

f.addWindowListener(new WindowAdapter() {//窗口监听(事件)
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);//程序结束
	}
	public void windowActivated() {
		System.out.println("页面前置");
	}			
});

按钮活动事件
(让按钮具备退出程序的功能)

 but.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e)
	{
		System.out.println("推出");
		System.exit(0);
	}
 });

鼠标事件监听(适配器)

but.addMouseListener(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("双击操作"+count++);
	}
});

键盘监听(适配器)

but.addKeyListener(new KeyAdapter() {
	public void keyPressed(KeyEvent e) {
		if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER)
			System.exit(0);//ctrl+回车 推出程序
		//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());
	}
});

文本框添加键盘监听(适配器)

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();//取消当前事件的操作(字母不能进入文本框)				
		}
			
	}
});

记事本小软件

package 黑马_GUI;
//图形化界面之简单记事本
//Frame add menuBar add menu add menuItem or menu
import java.awt.*;
import java.awt.event.*;//事件包
import java.io.*;

public class MyMenuDemo {
	private Frame f;
	private MenuBar bar;//菜单栏
	private TextArea ta;
	private Menu fileMenu;//子菜单
	private MenuItem closeItem,openItem,saveItem;//菜单条目
	
	private FileDialog openDia,saveDia;
	private File file=null;
	
	MyMenuDemo(){
		init();
	}
	public void init() {
    	f=new Frame("my window");
    	f.setBounds(500,200,750,600);
    	//f.setLayout(new FlowLayout());
    	
    	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);
    	f.add(ta);
    	
    	openDia=new FileDialog(f,"我要打开",FileDialog.LOAD);//模式:打开
    	saveDia=new FileDialog(f,"我要保存",FileDialog.SAVE)	;//模式:保存	
    	
    	myEvent();
    	
    	f.setVisible(true);
    			
	}  
	public void myEvent() {
		f.addWindowListener(new WindowAdapter() {
    		public void windowClosing(WindowEvent e)
    		{
    			System.exit(0);
    		}
    	});
		openItem.addActionListener(new ActionListener() {//打开
			public void actionPerformed(ActionEvent e) {
				openDia.setVisible(true);
				String dirPath=openDia.getDirectory();
				String fileName=openDia.getFile();
				
				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("读取失败");
				}
			}
			
		});
		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.close();
				}
				catch(IOException ex)
				{
					throw new RuntimeException("保存失败");
					
				}
				
			}
			
		});		
		closeItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);				
			}
			
		});
	}

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

	}

}

运行

记事本
保存

原创文章 26 获赞 2 访问量 899

猜你喜欢

转载自blog.csdn.net/liudachu/article/details/105858059