黑马程序员_javaGUI初学体会

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


学习了一天,把GUI的相关知识整理一下

一,组件之间的关系:

|--Dialog|-|--FileDialog|--Window|-|--Frame|--Panel|--ContainerComponent|--Button|--Label |--Checkbox|--TextComponent|--TextArea|--TextField

二,创建图形化界面基本步骤:

1,创建Frame窗口。Frame f =new Frame(String)2,对窗口进行基本的设置。如:大小, 位置, 布局。void setSize(kuan,gao) ,viod setLocation(x,y) ,==>void setBounds(x,y,kuan,gao);setLayout(new FlowLayout()) //默认为边界布局//布局方式: FlowLayout -- 流式布局 BorderLayout --边界布局 GirdLayout -- 网格布局 GirdBagLayout --网格包布局 GardLayout -- 卡片布局3,定义组件。Button b = new Button("String");4,将组件(和对应的监听器)通过窗体的add方法添加到窗体中。f.addButton(b);f.addaddWindowListener(...);5,通过setVisible(true),让窗口显示。--视情况而定f.setVisible(true);

三,设计监听器

事件监听机制的特点:

1,事件源。2,事件。3,监听器。4,事件处理。

其中上三者,在java中都已经定义好了。只要明确了事件源就直接获取对象拿来用就可以。

常见组件特有监视器:Window: addWindowListener(WindowListener l) ---WindowAdapter(WindowListener是一个接口(监视器都是接口)对于接口中方法既超过三个的,java提供了抽象适配器Adapter适配器中的方法为空,需要建立子类对象,并复写你所要监听事件的方法即可。) WindowAdapter中常用的方法: void windowClosing(WindowEvent e) 窗口正处在关闭过程中时调用。 void windowActivated(WindowEvent e) 将 Window 设置为活动 Window 时调用。 windowIconified(WindowEvent e) 窗口从正常状态变为最小化状态时调用。 ********* Button: addActionListener(ActionListener l) ActionListener接口只有一个方法actionPerformed,故没有适配器演示: Button but = new Button("mybutton");but.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.exit(0);}}); 方法actionPerformed(包括键盘激活)所以当只用于激活,就使用它。注意:晚于单击事件 ******************************

共性事件监听器:

鼠标事件监听器MouserListener addMouserListener(MouserListener l) ---MouserAdapter MouserListener中的方法: void mouseClicked(MouseEvent e) 鼠标按键在组件上单击(按下并释放)时调用。 void mouseEntered(MouseEvent e) 鼠标进入到组件上时调用。 void mouseExited(MouseEvent e) 鼠标离开组件时调用。 void mousePressed(MouseEvent e) 鼠标按键在组件上按下时调用。 void mouseReleased(MouseEvent e) 鼠标按钮在组件上释放时调用。 MouseEvent中的常用方法: int getClickCount() 返回与此事件关联的鼠标单击次数。用于判断双击。 字段摘要: static int BUTTON1 左键 指示鼠标按键 #1;由 getButton() 使用。 static int BUTTON2 右键 指示鼠标按键 #2;由 getButton() 使用。 static int BUTTON3 指示鼠标按键 #3;由 getButton() 使用。 **************键盘事件监听器KeyListeneraddKeyListener(KeyListener l) --- keyAdapterKeyListener中常用方法: void keyPressed(KeyEvent e) 按下某个键时调用此方法。 void keyReleased(KeyEvent e) 释放某个键时调用此方法。 void keyTyped(KeyEvent e) 键入某个键时调用此方法。 KeyEvent中常见:常用字段: static int VK_0 VK_0 到 VK_9 与 ASCII 的‘0’到‘9’(0x30 - 0x39) 相同 static int VK_A VK_A 到 VK_Z 与 ASCII 的‘A’到‘Z’(0x41 - 0x5A) 相同 static int VK_ALT static int VK_SHIFT static int VK_ESCAPE static int VK_ENTER 常用方法: char getKeyChar() 返回与此事件中的键关联的字符。 int getKeyCode() 返回与此事件中的键关联的整数 keyCode。 static String getKeyText(int keyCode) 返回描述 keyCode 的 String,如 "HOME"、"F1" 或 "A"。 从InputEvent继承的方法: void consume() 使用此事件,以便不会按照默认的方式由产生此事件的源代码来处理此事件。 boolean isAltDown() 返回 Alt 修饰符在此事件上是否为 down。 boolean isControlDown() 返回 Control 修饰符在此事件上是为 down。

好了,下面来写一个简单的程序演示一下:

import java.awt.Button;
import java.awt.Dialog;
<p>
	学习了一天,把GUI的相关知识整理一下
</p>
<p>
</p>
<p>
	一,组件之间的关系:
</p>
<p>
</p>
<p>
	<span style="white-space:pre"></span>  |--Dialog|-|--FileDialog<span style="white-space:pre"></span><span style="white-space:pre"></span>|--Window|-|--Frame<span style="white-space:pre"></span>|--Panel<span style="white-space:pre"></span>|--ContainerComponent<span style="white-space:pre"></span>|--Button<span style="white-space:pre"></span>|--Label <span style="white-space:pre"></span>|--Checkbox<span style="white-space:pre"></span>|--TextComponent<span style="white-space:pre"></span>|--TextArea<span style="white-space:pre"></span>|--TextField
</p>
<p>
</p>
<p>
	二,创建图形化界面基本步骤:
</p>
<p>
	1,创建Frame窗口。<span style="white-space:pre"></span>Frame f =new Frame(String)<span style="white-space:pre"></span>2,对窗口进行基本的设置。如:大小, 位置, 布局。<span style="white-space:pre"></span>void setSize(kuan,gao) ,viod setLocation(x,y) ,<span style="white-space:pre"></span>==>void setBounds(x,y,kuan,gao);<span style="white-space:pre"></span>setLayout(new FlowLayout()) //默认为边界布局<span style="white-space:pre"></span>//布局方式:  FlowLayout  -- 流式布局<span style="white-space:pre"></span> BorderLayout --边界布局<span style="white-space:pre"></span> GirdLayout  -- 网格布局<span style="white-space:pre"></span> GirdBagLayout --网格包布局<span style="white-space:pre"></span> GardLayout  --  卡片布局<span style="white-space:pre"></span><span style="white-space:pre"></span>3,定义组件。<span style="white-space:pre"></span>Button b = new Button("String");<span style="white-space:pre"></span>4,将组件(和对应的监听器)通过窗体的add方法添加到窗体中。<span style="white-space:pre"></span>f.addButton(b);<span style="white-space:pre"></span>f.addaddWindowListener(...);<span style="white-space:pre"></span>5,通过setVisible(true),让窗口显示。--视情况而定<span style="white-space:pre"></span>f.setVisible(true);
</p>
<p>
</p>
<p>
	三,设计监听器
</p>
<p>
	事件监听机制的特点:
</p>
<p>
	1,事件源。2,事件。3,监听器。4,事件处理。
</p>
<p>
	其中上三者,在java中都已经定义好了。只要明确了事件源就直接获取对象拿来用就可以。
</p>
<p>
	<span style="white-space:pre">常见组件特有监视器:Window:         addWindowListener(WindowListener l)   ---WindowAdapter<span style="white-space:pre"></span>(WindowListener是一个接口(监视器都是接口)对于接口中方法既超过三个的,java提供了抽象适配器Adapter<span style="white-space:pre"></span>适配器中的方法为空,需要建立子类对象,并复写你所要监听事件的方法即可。)<span style="white-space:pre"></span> WindowAdapter中常用的方法: <span style="white-space:pre"> </span>void windowClosing(WindowEvent e)   窗口正处在关闭过程中时调用。    void windowActivated(WindowEvent e) 将 Window 设置为活动 Window 时调用。             windowIconified(WindowEvent e)  窗口从正常状态变为最小化状态时调用。            *********      <span style="white-space:pre"> </span>  <span style="white-space:pre"></span>Button:  addActionListener(ActionListener l)  <span style="white-space:pre"></span>ActionListener接口只有一个方法actionPerformed,故没有适配器<span style="white-space:pre"></span><span style="white-space:pre"></span>演示: Button but = new Button("mybutton");<span style="white-space:pre"></span>but.addActionListener(new ActionListener(){<span style="white-space:pre"></span><span style="white-space:pre"></span>public void actionPerformed(ActionEvent e){<span style="white-space:pre"></span><span style="white-space:pre"></span>System.exit(0);<span style="white-space:pre"></span><span style="white-space:pre"></span>}<span style="white-space:pre"></span>});<span style="white-space:pre"> </span><span style="white-space:pre"></span>方法actionPerformed(包括键盘激活)所以当只用于激活,就使用它。<span style="white-space:pre"></span>注意:晚于单击事件<span style="white-space:pre"> </span>、 <span style="white-space:pre"></span>******************************</span>
</p>
<p>
</p>
<p>
	<span style="white-space:pre">共性事件监听器:</span>
</p>
<p>
	<span style="white-space:pre">鼠标事件监听器MouserListener  addMouserListener(MouserListener l)  ---MouserAdapter <span style="white-space:pre"></span>MouserListener中的方法: void mouseClicked(MouseEvent e)           鼠标按键在组件上单击(按下并释放)时调用。  void mouseEntered(MouseEvent e)           鼠标进入到组件上时调用。  void mouseExited(MouseEvent e)           鼠标离开组件时调用。  void mousePressed(MouseEvent e)           鼠标按键在组件上按下时调用。  void mouseReleased(MouseEvent e)           鼠标按钮在组件上释放时调用。  MouseEvent中的常用方法: int getClickCount()           返回与此事件关联的鼠标单击次数。用于判断双击。          字段摘要:            static int BUTTON1 左键          指示鼠标按键 #1;由 getButton() 使用。  static int BUTTON2 右键          指示鼠标按键 #2;由 getButton() 使用。  static int BUTTON3           指示鼠标按键 #3;由 getButton() 使用。  <span style="white-space:pre"> </span>**************</span><span style="white-space:pre">键盘事件监听器</span><span style="white-space:pre">KeyListener<span style="white-space:pre"></span>addKeyListener(KeyListener l)  ---  keyAdapterKeyListener中常用方法: void keyPressed(KeyEvent e)           按下某个键时调用此方法。  void keyReleased(KeyEvent e)           释放某个键时调用此方法。  void keyTyped(KeyEvent e)           键入某个键时调用此方法。 <span style="white-space:pre"></span>KeyEvent中常见:常用字段: static int VK_0      VK_0 到 VK_9 与 ASCII 的‘0’到‘9’(0x30 - 0x39) 相同 <span style="white-space:pre"></span>      static int VK_A      VK_A 到 VK_Z 与 ASCII 的‘A’到‘Z’(0x41 - 0x5A) 相同 <span style="white-space:pre"></span><span style="white-space:pre"></span> static int VK_ALT  <span style="white-space:pre"> </span> static int VK_SHIFT  <span style="white-space:pre"> </span> static int VK_ESCAPE<span style="white-space:pre"> </span><span style="white-space:pre"></span> static int VK_ENTER  <span style="white-space:pre"> </span>常用方法:<span style="white-space:pre"> </span> char getKeyChar()           返回与此事件中的键关联的字符。  int getKeyCode() <span style="white-space:pre"></span>          返回与此事件中的键关联的整数 keyCode。  static String getKeyText(int keyCode)           返回描述 keyCode 的 String,如 "HOME"、"F1" 或 "A"。 <span style="white-space:pre"></span>从InputEvent继承的方法: void consume()           使用此事件,以便不会按照默认的方式由产生此事件的源代码来处理此事件。 <span style="white-space:pre"></span> boolean isAltDown()           返回 Alt 修饰符在此事件上是否为 down。 <span style="white-space:pre"></span> boolean isControlDown()           返回 Control 修饰符在此事件上是为 down。  </span> 
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
	

	
</p>
<p>
</p>
<p>
</p>
<p>
	<span style="background-color: rgb(240, 240, 240);"></span>
</p>
<pre name="code" class="java"><pre name="code" class="html">import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

/*实现:
 * 从键盘上输入目录到文本框,
 * 点击按钮或按回车把输入目录下的文件打印到文本区域
 */
public class FileDirTest {
	private Frame f;
	private TextField tf;
	private Button but;
	private TextArea ta;
	
	FileDirTest(){
		init();
	}
	
	//创建设置主窗体页面
	private void init() {
		f = new Frame("目录显示器:");
		tf = new TextField(40);
		but = new Button("转到");
		ta = new TextArea(40,50);
		
		f.setBounds(200,100,500,400);
		f.setLayout(new FlowLayout());
		f.add(tf);
		f.add(but);
		f.add(ta);
		
		myEvent();
		f.setVisible(true);
	}

	//给主窗体页面中的组件添加事件监听器
	private void myEvent() {
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		
		but.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				showDir();
			}
		});
		
		tf.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					showDir();
			}
		});
	}
	
	//把所输入的目录中的文件名打印到文本区域中
	private void showDir() {
		String dirName = tf.getText();
		File dir = new File(dirName);
		ta.setText("");//把上一次的 清空
		if(dir.exists()&&dir.isDirectory()){
			for(String name :dir.list()){
				ta.append(name+"\r\n");
			}
		}
		else //不合法,创建提示信息窗口
			new Tishi(dirName);
		
	}
	
	//提示信息窗口类(内部类)
	private class Tishi{
		Dialog d;
		Label lb;
		Button but;
		Tishi(String dirName){
			info();
			lb.setText("输入:"+dirName+"有误或不存在");
			d.setVisible(true);
		}
		//创建设置提示窗体页面
		private void info() {
			d = new Dialog(f,"信息 提示框",true);	
			lb = new Label();
			but = new Button("确认");
			d.setBounds(300,200,400,150);
			d.setLayout(new FlowLayout());
			d.add(lb);
			d.add(but);
			myEvent();
		}
		//给提示窗体页面中的组件添加事件监听器
		private void myEvent(){
			d.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					d.setVisible(false);
				}
			});
			
			but.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					d.setVisible(false);
				}
			});
		}
	}
	
	
	//测试用
	public static void main(String[] args) {
		new FileDirTest();
	}

}


Over,就这简单,明天继续


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值