day22AWT窗口布局

</pre><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">import java.awt.*;</span>
import java.awt.event.*;
/*
创建图形化界面
1、创建frame窗体
2、对窗体进行基本设置。
	比如大小,位置,布局
3、定义组件
4、将组件通过窗体的add方法添加到窗体。
5,、让窗体显示,通过setVisible(true)
*/
class AwtDemo 
{
	public static void main(String[] args) 
	{
		Frame f=new Frame("my awt");
		f.setSize(500,400);
		f.setLocation(300,200);
		f.setLayout(new FlowLayout());

		Button b=new Button("我是一个按钮");

		f.add(b);

		f.addWindowListener(new MyWin());

		f.setVisible(true);

		//System.out.println("Hello World!");
	}
}
/*
class MyWin implements WindowListener
{
	//覆盖7个方法,可是我只用到关闭动作
	//其他动作没有用到,可是必须复写
}
*/
//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口
//并覆盖了其中的所有方法,那么我只要继承WindowAdapter覆盖我需要的方法

class MyWin extends WindowAdapter
{
	public void windowsClosing(WindowEvent e)
	{
		//System.out.println("window  closing");
		System.exit(0);
	}
}
import java.awt.*;
import java.awt.event.*;

class  FrameDemo
{
	//定义该图形中所需的组件引用
	private Frame f;
	private Button but;

	FrameDemo()
	{
		init();

	}

	public void init()
	{
		f=new Frame("my frame");
		//对frame进行基本设置
		f.setBounds(300,100,600,500);
		f.setLayout(new FlowLayout());

		but=new Button("my button");

		//将组件添加到frame中
		f.add(but);

		//加载窗体上的事件
		myEvent();

		//显示窗体
		f.setVisible(true);
	}
	private void myEvent()
	{
		f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		//让按键具备退出程序的功能
		/*
		按键就是一个事件源
		那么选择那个监听器
		通过关闭窗口示例了解到,想知道哪个组件具备什么样的特有监听器
		需要查看该组件对象的功能
		发现查阅button的描述,发现按钮支持一个特有监听。addActionListener

		*/
		but.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					System.out.println("tuichu,aniu");
					System.exit(0);
				}
			});
	}
	public static void main(String[] args) 
	{
		new FrameDemo();
	}
}
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");
		//对frame进行基本设置
		f.setBounds(300,100,600,500);
		f.setLayout(new FlowLayout());

		tf=new TextField(20);

		but=new Button("my button");

		//将组件添加到frame中
		f.add(but);
		f.add(tf);

		//加载窗体上的事件
		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.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.getKeyChar())+" -- ---"+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();
	}
}
import java.awt.*;
import java.awt.event.*;
class  MyMenuDemo
{
	private Frame f;
		private MenuBar mb;
		private Menu m,subMenu;
		private MenuItem closeItem,subItem;

		MyMenuDemo()
		{
			init();
		}
		public void init()
		{
			f=new Frame("my window");
			f.setBounds(300,100,500,600);
			f.setLayout(new FlowLayout());

			mb=new MenuBar();

			m=new Menu("文件");

			subMenu=new Menu("子菜单");
			
			subItem=new MenuItem("字条目");
			closeItem=new MenuItem("退出");

			subMenu.add(subItem);

			m.add(subMenu);
			m.add(closeItem);
			mb.add(m);

			f.setMenuBar(mb);

			myEvent();

			f.setVisible(true);
		}
		private 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);
				}
			});
		}
	public static void main(String[] args) 
	{
		new MyMenuDemo();
	}
}
<pre name="code" class="html">import java.awt.*;
import java.awt.event.*;
import java.io.*;
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()
		{
			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 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)
				{
					saveDia.setVisible(true);
					if (file==null)
					{
						saveDia.setVisible(true);
						
						String dirPath=openDia.getDirectory();
						String filename=openDia.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);
				}
			});
			f.addWindowListener(new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});
		}
	public static void main(String[] args) 
	{
		new MyMenuTest();
	}
}

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class  MyWindowDemo
{
	private Frame f;
	private Button but;
	private TextField tf;
	private TextArea ta;

	private Dialog d;
	private Label lab;
	private Button okBut;

	MyWindowDemo()
	{
		init();
	}
	public void init()
	{
		f=new Frame("my window");
		f.setBounds(300,100,600,500);
		f.setLayout(new FlowLayout());

		tf=new TextField(30);

		but=new Button("转到");

		ta=new TextArea(15,40);

		d=new Dialog(f,"提示信息-self",true);
		d.setBounds(400,200,240,150);
		d.setLayout(new FlowLayout());
		lab=new Label();
		okBut=new Button("确定");

		d.add(lab);
		d.add(okBut);

		f.add(tf);
		f.add(but);
		f.add(ta);

		myEvent();
		f.setVisible(true);
	}

	private void myEvent()
	{

		okBut.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
				{
					d.setVisible(false);
				}
		});

		d.addWindowListener(new WindowAdapter()
			{
				public void windowClosing(WindowEvent 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();
					//String text=tf.getText();
					//ta.setText(text);
					//System.out.println(text);
					//tf.setText("	");
				}
		});
		
		
		f.addWindowListener(new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});
	}

	public static void main(String[] args) 
	{
		new MyWindowDemo();
	}
	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);
				}
		 }
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值