黑馬程序員_GUI

  ------- android培訓java培訓、期待與您交流! ----------


一 GUI(圖形用戶界面)
1 GUI 圖形用戶界面,用圖形的方式顯示計算機的界面,方便直觀

2 CLI 命令行,需要記住一些命令,比較麻煩

3 java爲GUI提供的對象都存在java.Awt和javax.Swing兩個包中。
(1)Awt:需調用系統底層資源,有點依賴平臺,屬重量級控件。
(2)Swing:基於Awt,提供了更多的組件,完全由java實現。屬輕量級控件
Container 是一個特殊的組件,他可以添加其他組件

4 佈局,組件的排放方式。幾種常見的佈局管理器:
(1)FlowLayout,流式佈局,按照從左到右排放,默認是居中的。Pannel的默認佈局
(2)BoarderLayout,邊界式佈局,東南西北中排放,默認是居中的。Frame的默認佈局
(3)CardLayout,卡片式佈局,選項卡
(4)GridLayout,網格式佈局。規則的矩陣
(5)GridBagLayout,網格包式佈局,非規則的矩陣

5 事件監聽機制
(1)事件源:就是awt包和swing包中的組件
(2)事件:每個事件源都有自己對應的事件和共性事件
(3)監聽器:將可以觸發某一個事件的某些動作封裝到監聽器中
(4)事件處理:針對產生的動作進行處理。


二   組件
1 Frame 窗體
(1)構造方法:
Frame f=new Frame("窗體");//創建窗體並命名
(2)方法摘要:
setSize(int len,int wide);//設置窗體長和寬大小
setLocation(int len,int wide);//設置窗體位置
setLayout(FlowLayout f);//設置窗體佈局
add(PopupMenu popup);//在窗體裏添加組件
setVisible(boolean b);設置窗體顯示狀態

2 Button 按鈕
(1)構造方法:
Button b=new Button("按鈕");//創建按鈕並命名
(2)例子:創建窗體並添加按鈕組件。添加窗體關閉事件和按鈕關閉事件。
代碼:

import java.awt.*;
import java.awt.event.*;
class  AwtDemo
{
	public static void main(String[] args) 
	{
		//創建一個窗體,並給窗體定個標題
		Frame f=new Frame("我的窗體");
		//設置窗體大小以及距離屏幕位置
		f.setSize(700,600);
		f.setLocation(300,100);
		//設置窗體的佈局爲流式佈局,默認是邊界式佈局
		f.setLayout(new FlowLayout());
		//創建一個按鈕,並起個名字
		Button b=new Button("退出");
		//將按鈕添加到窗體中
		f.add(b);
		//添加窗口監聽器
		f.addWindowListener(new WindowAdapter()
		{
			//窗口關閉事件
			public void windowClosing(WindowEvent e)
			{
				//處理方法
				System.exit(0);//退出程序
			}
		});
		//添加按鈕監聽器
		b.addActionListener(new ActionListener()
		{
			//按鈕活動事件
			public void actionPerformed(ActionEvent e)
			{
				//處理方法
				System.exit(0);//退出程序
			}
		});
		//顯示窗體
		f.setVisible(true);
	}
}

3 TextArea 文本區域

4 TextField 文本框

5 Dialog 對話框

6 Label 標籤

舉例:模擬一個列出指定目錄下的文件名稱

代碼:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class AwtTest 
{

	private Frame f;//窗體/定義
	private TextField tf;//定義文本框
	private Button b;//定義按鈕
	private TextArea ta;//定義文本區域
	private File fil;//定義文件

	private Dialog d;//定義對話框
	private Label lab;//定義標籤
	private Button ok;//定義按鈕

	AwtTest()
	{
		init();//初始化
	}
	public void init()
	{
		//創建窗體並設置大小和佈局
		f=new Frame("我的窗口");
		f.setBounds(300,100,600,500);
		f.setLayout(new FlowLayout());
		//創建文本區域、文本對話框
		tf=new TextField(70);
		b=new Button("轉到");
		ta=new TextArea(30,80);

		//創建對話框並設置佈局和大小
		d=new Dialog(f,"提示信息",true);
		d.setBounds(400,200,300,200);
		d.setLayout(new FlowLayout());
		
		//創建標籤和確定按鈕
		lab=new Label();
		ok=new Button("確定");
		//在對話框中添加標籤和確定按鈕
		d.add(lab);
		d.add(ok);
		//在窗體添加文本框、按鈕、文本區域
		f.add(tf);
		f.add(b);
		f.add(ta);
		//添加時間
		event();
		//設置窗口顯示
		f.setVisible(true);
	}

	public void event()
	{
		//在確定按鈕添加鍵盤監聽器
		ok.addKeyListener(new KeyAdapter()
		{
			//鍵盤按下事件
			public void keyPressed(KeyEvent e)
			{
				//如果按下ENTRY鍵對話框就關閉
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					d.setVisible(false);
			}
		});
		//在文本框添加鍵盤監聽器
		tf.addKeyListener(new KeyAdapter()
		{
			//鍵盤按下事件
			public void keyPressed(KeyEvent e)
			{
				//如果按下ENTRY鍵調用show方法
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					show();
			}
		});
		//在確定按鈕添加鍵盤監聽器
		ok.addActionListener(new ActionListener()
		{
			//鍵盤按下事件
			public void actionPerformed(ActionEvent e)
			{
				//對話框關閉
				d.setVisible(false);
			}
		});
		//在對話框添加窗口監聽器
		d.addWindowListener(new WindowAdapter()
		{
			//窗口關閉事件
			public void windowClosing(WindowEvent e)
			{
				//窗口關閉
				d.setVisible(false);
			}
		});
		//在轉到按鈕添加活動監聽器
		b.addActionListener(new ActionListener()
		{
			//按鈕活動事件
			public void actionPerformed(ActionEvent e)
			{
				//調用show方法
				show();
				//System.out.println(file);
			}
		});
		//在窗體添加監聽器
		f.addWindowListener(new WindowAdapter()
		{
			//窗體關閉事件
			public void windowClosing(WindowEvent e)
			{
				//程序退出
				System.exit(0);
			}
		});
	}

	public void show()
	{
		//先清空文本區域內容
		ta.setText("");
		//獲取文本框內容並封裝成文件對象
		String file=tf.getText();
		fil=new File(file);
		//判斷文件是否是目錄並且文件存在,條件滿足
		if(fil.exists() && fil.isDirectory())
		{
			//變量目錄下的文件並添加到文本區域中
			String[] files=fil.list();
			for(String name: files)
			{
				ta.append(name+"\r\n");
			}
		}
		//條件不滿足
		else
		{
			//讓對話框顯示並給出提示
			String info="您輸入:"+file+"不正確,請重輸";
			lab.setText(info);
			d.setVisible(true);
		}
	}    
	public static void main(String[] args) 
	{
		new AwtTest();
	}
}

7 MenuBar 菜單欄

8 Menu 菜單

9 MenuItem 菜單條

例子:模擬一個記事本軟件
代碼:

import java.awt.*;
import java.awt.event.*;
import java.io.*;

class  MyJishibenDemo
{
	private Frame f;//定義窗體
	private MenuBar bar;//定義菜單欄
	private Menu menu;//定義菜單
	private MenuItem open,save,exit;//定義打開保存退出三個菜單條
	private TextArea ta;//定義文本區域
	private FileDialog opendia,savedia;//定義文件對話框
	private File f1;//定義文件

	MyJishibenDemo()
	{
		init();//初始化
	}

	public void init()
	{	
		//初始化窗體
		f=new Frame("我的記事本");
		f.setBounds(300,200,500,400);

		//初始化菜單欄,菜單,菜單條目
		bar=new MenuBar();
		menu=new Menu("文件");
		open=new MenuItem("打開");
		save=new MenuItem("保存");
		exit=new MenuItem("退出");

		//初始化文本區域
		ta=new TextArea();

		//初始化文件對話框
		opendia=new FileDialog(f,"我要打開",FileDialog.LOAD);
		savedia=new FileDialog(f,"我要保存",FileDialog.SAVE);

		//在窗體添加菜單欄,在菜單欄添加文件菜單
		//在文件菜單下添加打開,保存,退出三個條目
		f.setMenuBar(bar);
		bar.add(menu);
		menu.add(open);
		menu.add(save);
		menu.add(exit);

		//窗體添加文本區域
		f.add(ta);

		//添加事件
		event();

		//顯示窗體
		f.setVisible(true);
	}

	public void event()
	{

		//保存菜單條添加活動監聽器
		save.addActionListener(new ActionListener()
		{
			//活動事件
			public void actionPerformed(ActionEvent e)
			{
				//如果文件不存在
				if(f1==null)
				{
					//彈出保存文件對話框獲取目錄和名字封裝成文件對象
					savedia.setVisible(true);
					String path=savedia.getDirectory();
					String name=savedia.getFile();
					f1=new File(path,name);
					if(path==null||name==null)
						return ;
				}
				//如果文件存在,不彈出文件對話框
				try
				{
					//定義寫入流,將文本區域的數據寫到文件中去
					BufferedWriter bufw=new BufferedWriter(new FileWriter(f1));
					String text=ta.getText();
					bufw.write(text);
					bufw.close();
				}
				catch (Exception en)
				{
					throw new RuntimeException("失敗");
				}
			}
		});
		//打開菜單條添加活動監聽器
		open.addActionListener(new ActionListener()
		{
			//活動事件
			public void actionPerformed(ActionEvent e)
			{
				//顯示打開文件對話框,獲取文件目錄和名字並封裝成文件對象
				opendia.setVisible(true);
				String path=opendia.getDirectory();
				String file=opendia.getFile();
				//System.out.println(path+"::"+file);
				if(path==null||file==null)
					return ;
				ta.setText("");
				f1=new File(path,file);
				try
				{				
					//定義讀取流,讀取指定文件數據寫到文本區域上
					BufferedReader bufr=new BufferedReader(new FileReader(f1));
					String line=null;
					while((line=bufr.readLine())!=null)
					{
						ta.append(line+"\r\n");
					}
					bufr.close();
				}
				catch (Exception ex)
				{
					throw new RuntimeException("失敗");
				}
				
			}
		});
		//退出菜單條添加活動監聽器
		exit.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 MyJishibenDemo();
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章