MyException - 我的异常网
当前位置:我的异常网» 编程 » 榛戦┈绋嬪簭鍛?GUI鍥惧舰鐢ㄦ埛鐣岄潰一

榛戦┈绋嬪簭鍛?GUI鍥惧舰鐢ㄦ埛鐣岄潰一

www.MyException.Cn  网友分享于:2013-02-20  浏览:5次
榛戦┈绋嬪簭鍛?GUI鍥惧舰鐢ㄦ埛鐣岄潰1

----------------------聽android鍩硅銆?a target="blank" href="http://edu.csdn.net/heima" style="color: #ca0000; text-decoration: underline; text-align: center; font-size: 12px; line-height: 26px;">java鍩硅銆佹湡寰呬笌鎮ㄤ氦娴侊紒 ----------------------

GUI鍥惧舰鐢ㄦ埛鐣岄潰

GUI(鍥惧舰鐢ㄦ埛鐣岄潰)

GUI-Graphical User Interface(鍥惧舰鐢ㄦ埛鎺ュ彛)銆?/p>

鐢ㄥ浘褰㈢殑鏂瑰紡锛屾潵鏄剧ず璁$畻鏈烘搷浣滅殑鐣岄潰锛岃繖鏍锋洿鏂逛究鏇寸洿瑙傘€?/p>

CLI-Command line User Interface (鍛戒护琛岀敤鎴锋帴鍙o級

灏辨槸甯歌鐨凞os鍛戒护琛屾搷浣溿€偮?/p>

闇€瑕佽蹇嗕竴浜涘父鐢ㄧ殑鍛戒护锛屾搷浣滀笉鐩磋銆?/p>

涓句緥锛?聽

姣斿锛氬垱寤烘枃浠跺す锛屾垨鑰呭垹闄ゆ枃浠跺す绛?/p>

Java涓篏UI鎻愪緵鐨勫璞¢兘瀛樺湪java.Awt鍜宩avax.Swing涓や釜鍖呬腑銆?/p>

Awt鍜孲wing

Awt涓?Swing

java.Awt锛欰bstract Window ToolKit (鎶借薄绐楀彛宸ュ叿鍖?锛岄渶瑕佽皟鐢ㄦ湰鍦扮郴缁熸柟娉曞疄鐜板姛鑳姐€傚睘閲嶉噺绾ф帶浠躲€傛瘮杈冧緷璧栧钩鍙帮紝璺ㄥ钩鍙版€т笉鏄緢濂斤紝浼氭湁缁嗗井鐨勫尯鍒€?/p>

javax.Swing锛氬湪AWT鐨勫熀纭€涓婏紝寤虹珛鐨勪竴濂楀浘褰㈢晫闈㈢郴缁燂紝鍏朵腑鎻愪緵浜嗘洿澶氱殑缁勪欢锛岃€屼笖瀹屽叏鐢盝ava瀹炵幇銆傚寮轰簡绉绘鎬э紝灞炶交閲忕骇鎺т欢銆?/p>

eclipse鏄敤swt鍋氱殑銆?/p>

Container锛氫负瀹瑰櫒锛屾槸涓€涓壒娈婄殑缁勪欢锛岃缁勪欢涓彲浠ラ€氳繃add鏂规硶娣诲姞鍏朵粬缁勪欢杩涙潵銆?/p>

甯冨眬绠$悊鍣?/p>

瀹瑰櫒涓殑缁勪欢鐨勬帓鏀炬柟寮忥紝灏辨槸甯冨眬銆?/p>

甯歌鐨勫竷灞€绠$悊鍣細

FlowLayout锛堟祦寮忓竷灞€绠$悊鍣級

浠庡乏鍒板彸鐨勯『搴忔帓鍒椼€?/p>

Panel榛樿鐨勫竷灞€绠$悊鍣ㄣ€?/p>

BorderLayout锛堣竟鐣屽竷灞€绠$悊鍣級

涓滐紝鍗楋紝瑗匡紝鍖楋紝涓?/p>

Frame榛樿鐨勫竷灞€绠$悊鍣ㄣ€?/p>

GridLayout锛堢綉鏍煎竷灞€绠$悊鍣級

瑙勫垯鐨勭煩闃?/p>

CardLayout锛堝崱鐗囧竷灞€绠$悊鍣級

閫夐」鍗?/p>

GridBagLayout锛堢綉鏍煎寘甯冨眬绠$悊鍣級

闈炶鍒欑殑鐭╅樀

鍧愭爣寮忓竷灞€鎵嶆槸鏈€濂界殑锛屽彲浠ョ敤鍥惧舰鍖栫紪杈戝櫒锛屽GBuilder鎴栬€卐clipse娣诲姞鍥惧舰鍖栨彃浠躲€傚氨鍏锋湁鐢诲竷锛岄渶瑕佷粈涔堢粍浠堕€変粈涔堢粍浠讹紝涓€鍒囨崲鍒颁唬鐮佹ā寮忎唬鐮佸叏閮ㄧ敓鎴愩€?/p>

濡傛灉涓€涓獥鍙d腑闇€瑕佺敤鍒板绉嶅竷灞€褰㈠紡锛屽彲浠ョ嚎鍒掑垎涓哄涓潰鏉匡紝鐒跺悗瀹氫箟鍚勪釜闈㈡澘鐨勫竷灞€褰㈠紡銆?/p>

鍥惧舰鍖栫晫闈㈡槸鐢卞彟澶栫殑绾跨▼鎺у埗鐨勩€?/p>

璁剧疆绐楀彛鐨勫昂瀵哥涓€涓弬鏁版槸妯潗鏍囷紝绗簩涓槸绾靛潗鏍囥€?/p>

寤虹珛涓€涓畝鍗曠殑绐椾綋

Container甯哥敤瀛愮被锛歐indow 聽 Panel锛堥潰鏉匡紝涓嶈兘鍗曠嫭瀛樺湪銆傦級

Window甯哥敤瀛愮被锛欶rame 聽Dialog

绠€鍗曠殑绐椾綋鍒涘缓杩囩▼锛?/p>

Frame 聽f = new Frame(鈥渕y window鈥?;

f.setLayout(new FlowLayout());

f.setSize(500,400);//璁剧疆绐椾綋澶у皬

f.setLocation(300,200);//璁剧疆绐椾綋鍑虹幇鍦ㄥ睆骞曠殑浣嶇疆

f.setVisible(true);

鍒涘缓鍥惧舰鍖栫晫闈細

1锛屽垱寤篺rame绐椾綋銆?/p>

2锛屽绐椾綋杩涜鍩烘湰璁剧疆銆?/p>

姣斿澶у皬锛屼綅缃紝甯冨眬銆?/p>

3锛屽畾涔夌粍浠躲€?/p>

4锛屽皢缁勪欢閫氳繃绐椾綋鐨刟dd鏂规硶娣诲姞鍒扮獥浣撲腑銆?/p>

5锛岃绐椾綋鏄剧ず锛岄€氳繃setVisible(true)

浜嬩欢鐩戝惉鏈哄埗鐨勭壒鐐癸細

1.浜嬩欢婧愶紙缁勪欢锛?/p>

2.浜嬩欢锛圗vent锛?/p>

3.鐩戝惉鍣紙Listener锛?/p>

4.浜嬩欢澶勭悊锛堝紩鍙戜簨浠跺悗澶勭悊鏂瑰紡锛?/p>

浜嬩欢婧愶細灏辨槸awt鍖呮垨鑰卻wing鍖呬腑鐨勯偅浜涘浘褰㈢晫闈㈢粍浠躲€?/p>

浜嬩欢锛氭瘡涓€涓簨浠舵簮閮芥湁鑷繁鐗规湁鐨勫搴斾簨浠跺拰鍏辨€т簨浠躲€?/p>

鐩戝惉鍣細灏嗗彲浠ヨЕ鍙戞煇涓€涓簨浠剁殑鍔ㄤ綔锛堜笉鍙竴涓姩浣滐級閮藉凡缁忓皝瑁呭埌浜嗙洃鍚櫒涓€?/p>

浠ヤ笂涓夎€咃紝鍦╦ava涓兘宸茬粡瀹氫箟濂戒簡銆?/p>

鐩存帴鑾峰彇鍏跺璞℃潵鐢ㄥ氨鍙互浜嗐€?/p>

鎴戜滑瑕佸仛鐨勪簨鎯呮槸锛屽氨鏄浜х敓鐨勫姩浣滆繘琛屽鐞嗐€?/p>

WindowAdapter绫绘槸涓€涓娊璞$被锛屽唴閮ㄦ病鏈夋娊璞℃柟娉曪紝鎶借薄鍙槸涓嶈鍒涘缓瀵硅薄锛屾槸WindowListener鐨勫瓙绫伙紝瑕嗙洊浜哤indowListener绫讳腑鐨勬柟娉曪紝浣嗘槸鏂规硶浣撳唴鏄┖锛屾墍浠ヨ皟鐢ㄦ病鏈夋剰涔夛紝

	
import java.awt.*;
import java.awt.event.*;
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 WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.out.println("鎴戝叧");
				System.exit(0);
			}
			//婵€娲荤獥鍙?
			public void windowActivated(WindowEvent e) 
			{
				System.out.println("鎴戞椿浜嗐€?);

			}
			//鎵撳紑
			public void windowOpened(WindowEvent e) 
			{
				System.out.println("鎴戣鎵撳紑浜?hahahhahah");
			}

		});
		f.setVisible(true);

	}
}

鍥犱负WindowListener鐨勫瓙绫籛indowAdapter宸茬粡瀹炵幇浜哤indowListener鎺ュ彛銆?骞惰鐩栦簡鍏朵腑鐨勬墍鏈夋柟娉曘€傞偅涔堟垜鍙缁ф壙鑷猈indowadapter瑕嗙洊鎴戦渶瑕佺殑鏂规硶鍗冲彲銆?/p>

class MyWin extends WindowAdapter
{
	public void windowClosing(WindowEvent e)//瑕嗙洊WindowAdapter涓殑鏂规硶銆?
	{
		//System.out.println("window closing---"+e.toString());//璇ュ姩浣滆Е鍙戜竴娆℃墦鍗颁竴娆°€?
		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");

		//瀵筬rame杩涜鍩烘湰璁剧疆銆?
		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);
			}
		});

		//璁╂寜閽叿澶囬€€鍑虹▼搴忕殑鍔熻兘
		/*
		鎸夐挳灏辨槸浜嬩欢婧愩€?
		閭d箞閫夋嫨鍝釜鐩戝惉鍣ㄥ憿锛?
		閫氳繃鍏抽棴绐椾綋绀轰緥浜嗚В鍒帮紝鎯宠鐭ラ亾鍝釜缁勪欢鍏峰浠€涔堟牱鐨勭壒鏈夌洃鍚櫒銆?
		闇€瑕佹煡鐪嬭缁勪欢瀵硅薄鐨勫姛鑳姐€?
		 閫氳繃鏌ラ槄button鐨勬弿杩般€傚彂鐜版寜閽敮鎸佷竴涓壒鏈夌洃鍚琣ddActionListener銆?
		//闈㈠悜瀵硅薄鐨勬€濇兂锛屽璋佽繘琛屾搷浣滆皝灏辨彁渚涙搷浣滅殑鏂规硶銆?
		*/
//ActionListener鏄竴涓病鏈夐€傞厤鍣ㄧ殑鎺ュ彛锛屽洜涓哄唴閮ㄦ柟娉曞彧鏈変竴涓紝閫傞厤鍣ㄦ槸涓轰簡璁╁瓙绫绘柟渚夸娇鐢紝娌℃湁閫傞厤鍣ㄧ殑鎺ュ彛鍙湁涓変釜锛屼竴鑸帴鍙d腑鏈変笁涓互涓婃柟娉曠殑閮芥湁閫傞厤鍣ㄣ€?
		but.addActionListener(new ActionListener()
		{
			private int count = 1;
			public void actionPerformed(ActionEvent e)
			{
				//System.out.println("閫€鍑猴紝鎸夐挳骞茬殑");
				//System.exit(0);
				
				//f.add(new Button("Button-"+(count++)));
				//f.setVisible(true);
				//f.validate();

				//System.out.println(e.getSource());
				Button b = (Button)e.getSource();
				
				Frame f1 = (Frame)b.getParent();

				f1.add(new Button("button-"+count++));
				f1.validate();


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

榧犳爣浜嬩欢鍜岄敭鐩樹簨浠?/p>

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();//濡傛灉瀛楃闈炴硶锛屼娇鐢╟onsume鏂规硶鍙互涓嶈瀛楃杩涘叆鏂囨湰妗?
				}


			}
		});

		//缁橞ut娣诲姞涓€涓敭鐩樼洃鍚€?
		but.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{	//鍒ゆ柇ctrl閿槸鍚﹁鎸変笅
				if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
					//System.exit(0);
				System.out.println("ctrl+enter is run");
				System.out.println(e.getKeyChar()+"---"+e.getKeyCode());//鏍瑰熀閿殑瀛楃鑾峰彇閿搴旂殑鐮?
				//鏍规嵁閿洏鐮佽幏鍙栧埌閿殑鏂囨湰锛岃繑鍥炵殑鏄瓧绗︿覆銆?
				System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());
			}
		});



		//鍙互缁欐寜閽坊鍔犳椿鍔ㄧ洃鍚紝鍙娲诲姩灏辨墽琛岋紝榧犳爣閿洏閮借兘璁╂寜閽椿鍔?
		//娣诲姞浜嬩欢鐨勬椂鍊欏敖閲忔坊鍔燗ctionEvent
		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 void mouseClicked(MouseEvent e)
			{
					System.out.println("鍗曞嚮鍔ㄤ綔"+clickCount++);
			}
		});
		
	}
	public static void main(String[] args) 
	{
		new MouseAndKeyEvent();
	}
}

----------------------聽android鍩硅銆?a target="blank" href="http://edu.csdn.net/heima" style="color: #ca0000; text-decoration: underline; text-align: center; font-size: 12px; line-height: 26px;">java鍩硅銆佹湡寰呬笌鎮ㄤ氦娴侊紒 ----------------------

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有