黑马程序员——Java基础-图形界面

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

图形化界面由各种组件构成,Component为组件的抽象超类,其下有Container(容器)、Button(按钮)、Label(标签)、Checkbox(选项框)、List(选项表)、Textcomponent(文本控件)等常用子类。这些子类中只有Container为容器组件,其他为控件。容器中能添加容器及控件,控件中不能添加容器或者控件。


容器添加容器或者控件时需要定义其布局方式,Frame默认的是BorderLayout布局管理器,Panel默认的是FlowLayout布局管理器,也可以通过setLayout()方法来定义布局。

流式布局:FlowLayout

    从左到右的顺序排列

    Panel默认的布局管理器

边界式布局:BorderLayout

    东、南、西、北、中

    Frame默认的布局管理器

网格布局:GridLayout

    规则的矩阵

卡片布局:CardLayout

    选项卡

网格包布局:GridBagLayout

    非规则的矩阵

除了界面之外,实现交互还需要事件监听器,能够监听用户的操作并做出反应。常用的监听器有ActionListener、WindowListener,这些监听器都是添加在组件上,用户操作组件就能促发相应事件。

一下为通过java编写的类似记事本的小程序

/*
带包编译 
javac -d f:\ Mymenu.java
jar -cvfm my.jar 1.txt mymenu
//1.txt为配置信息Main-Class:,双击时告诉jar包中找哪个类执行
*/
package mymenu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Mymenu
{
	private Frame f;
	private TextArea ta;
	private MenuBar bar;
	private Menu fileMenu;
	private MenuItem openItem,saveItem,closeItem;
	private FileDialog openDia,saveDia;
	private File file;	
	Mymenu()
	{
		init();
	}	
	public void init()
	{
		f=new Frame("my window");//新建框架容器
		f.setBounds(300,200,650,600);//设置位置和大小
		//Frame默认为BorderLayout可以通过设置改变布局
		//f.setLayout(new FlowLayout());
		ta=new TextArea();//新建文本区控件
		f.add(ta);//将文本区控件添加到框架中
		bar=new MenuBar();//新建菜单条
		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);//将菜单条设置到框架中
		//新建打开文件对话框,并调用Windows底层打开文件对话框
		openDia=new FileDialog(f,"我要打开",FileDialog.LOAD);
		//新建保存文件对话框,并调用Windows底层保存文件对话框
		saveDia=new FileDialog(f,"我要保存",FileDialog.SAVE);		
		myEvent();//调用监听事件方法
		f.setVisible(true);//将框架设置为可见
	}
	private void myEvent()
	{
		//在保存菜单子目录中添加监听器
		saveItem.addActionListener(new ActionListener()
		{
			//复写监听器中的actionPerformed方法
			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 ee)
				{
					throw new RuntimeException("保存失败");
				}
				
			}
		});			
		//在打开菜单子目录中添加监听器
		openItem.addActionListener(new ActionListener()
		{
			//复写监听器中的actionPerformed方法
			public void actionPerformed(ActionEvent e)
			{
				openDia.setVisible(true);//将打开文件对话框设置为可见
				String dirPath=openDia.getDirectory();
				String fileName=openDia.getFile();				
				if(dirPath==null||fileName==null)
					return;
				file=new File(dirPath,fileName);
				ta.setText("");//将文本区控件初始化为空
				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 ee)
				{
					throw new RuntimeException("读取失败");
				}
			}
		});
		//往框架中添加监听器
		f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		//往关闭菜单子目录中添加监听器
		closeItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		
	}
	public static void main(String[] args)
	{
		new Mymenu();
	}
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值