黑馬_blog6_GUI

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity開發</a>、<a href="http://www.itheima.com"target="blank">.Net培訓</a>、期待與您交流! ----------------------

1GUI(概述)

Java.awt:調用本地方法實現功能,重量級軟件

Javax.swing:用它在任何系統上都一樣,給予awt,輕量級組件。

繼承關係圖:

 

Container作爲一個組件,裏面能添加組件(add)

2GUI(佈局)

容器中組件的排放方式--佈局

常見佈局管理器

FlowLayout(流式佈局管理) 從左到右順序排列

BorderLayout(邊界佈局管理器) 東西南北中(沒指定位置,默認填充整個窗體)

GridLayout(網格佈局管理器)規則的矩陣

GridBagLayout(網格包佈局管理器)非規則的矩陣

CardLayout(卡片式佈局管理器)選項卡

座標式佈局-組件想放哪兒放哪兒。

3GUI(Frame)

創建圖形化界面步驟:

1,創建frame窗體。

2,對窗體進行基本設置。

比如大小,位置,佈局。

3,定義組件。

4,將組件通過窗體的add方法添加到窗體中。

5,讓窗體顯示,通過setVisible(true)

示例代碼:

import java.awt.*;
class  Test
{
	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.setVisible(true);
	}
}

4GUI(事件監聽機制)

事件監聽機制流程圖

 

事件監聽機制的特點:

1,事件源。

2,事件。

3,監聽器。

4,事件處理。

事件源:就是awt包或者swing包中的那些圖形界面組件。

事件:每一個事件源都有自己特有的對應事件和共性事件。

監聽器:將可以觸發某一個事件的動作(不只一個動作)都已經封裝到了監聽器中。

以上三者,在java中都已經定義好了。直接獲取其對象來用就可以了。

我們要做的事情是,就是對產生的動作進行處理。

5GUI(窗體事件)

因爲WindowListener的子類WindowAdapter已經實現了WindowListener接口。並覆蓋了其中的所有方法。那麼我只要繼承自Windowadapter覆蓋我需要的方法即可。

示例代碼:

import java.awt.*;
import java.awt.event.*;
class  Test
{
	public static void main(String[] args) 
	{
		Frame f=new Frame("my awt");
		f.setSize(500,400);
		f.setLocation(300,200);
		f.setLayout(new FlowLayout());
		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("我被打開了");
			}
		});
		f.setVisible(true);
	}
}

6GUI(Action事件)

需求:讓按鈕具備退出程序的功能。

通過查閱Button的描述,發現按鈕支持一個特有監聽addActionListener。它沒有適配器,它是少數沒有適配器藉口的其中一個。

適配器的出現原因就一個,覆蓋很多歌方法會很麻煩,所以,給我們提供了一個已有的子類,都覆蓋完了,我們只要繼承它,覆寫我們需要的方法就可以了。

示例代碼:

import java.awt.*;
import java.awt.event.*;
class  Test
{
	public static void main(String[] args) 
	{
		new FrameDemo();
	}
}
class FrameDemo
{
	private Frame f;
	private Button but;
	FrameDemo()
	{
		init();
	}

	public void init()
	{
		f=new Frame("my awt");
		f.setBounds(300,100,600,500);
		f.setLayout(new FlowLayout());
		but=new Button("my button");
		f.add(but);
		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)
			{
				System.out.println("退出");
				System.exit(0);
			}
		});
	}
}

7GUI(鼠標事件)

示例代碼:

but.addMouseListener(new MouseAdapter()
		{
			public void mouseEntered(MouseEvent e)
			{
				System.out.println("進入");
			}
			public void mouseClicked(MouseEvent e)
			{
				System.out.println("單擊");
			}
		});


 

注意:MouseListenerActionListener先執行。

雙擊示例代碼:

but.addMouseListener(new MouseAdapter()
		{
			public void mouseEntered(MouseEvent e)
			{
				System.out.println("進入");
			}
			public void mouseClicked(MouseEvent e)
			{
				if(e.getClickCount()==2)
				{
					System.out.println("雙擊");
				}
			}
		});

8.GUI(鍵盤事件)

示例代碼:

but.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode()==KeyEvent.VK_J)
					System.exit(0);
			}
		});

組合鍵

but.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
					System.out.println("ctrl+enter");
			}
		});

取消事件:e.consume();

需求:要求文本框中只能輸入數字:

示例代碼:

t.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				if(!(e.getKeyCode()>=KeyEvent.VK_0&&e.getKeyCode()<=KeyEvent.VK_9))
				{
					System.out.println("非法");
					e.consume();
				}
			}
		});


 

9GUI(練習-列出指定目錄內容)

示例代碼:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class  Test
{
	private Frame f;
	private TextField tf;
	private Button but;
	private TextArea ta;
	
	Test()
	{
		init();
	}
	public void init()
	{
		f=new Frame("my awt");
		f.setBounds(300,400,500,400);
		f.setLayout(new FlowLayout());

		tf=new TextField(30);
		but=new Button("轉到");
		ta=new TextArea(20,50);
		
		f.add(tf);
		f.add(but);
		f.add(ta);

		myevent();

		f.setVisible(true);
	}
	public static void main(String[] args) 
	{
		new Test();
	}
	private void myevent()
	{
		f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});

		but.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				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");
					}
				}
				tf.setText("");
			}
		});

	}
}

代碼優化:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class  Test
{
	private Frame f;
	private TextField tf;
	private Button but;
	private TextArea ta;
	private Dialog d;
	private Label lab;
	private Button okb;
	Test()
	{
		init();
	}
	public void init()
	{
		f=new Frame("my awt");
		f.setBounds(300,400,500,400);
		f.setLayout(new FlowLayout());

		tf=new TextField(30);
		but=new Button("轉到");
		ta=new TextArea(20,50);
		
		d=new Dialog(f,"提示信息-self",true);
		d.setBounds(400,200,240,150);
		d.setLayout(new FlowLayout());
		lab=new Label();
		okb=new Button("確定");
		d.add(lab);
		d.add(okb);
		f.add(tf);
		f.add(but);
		f.add(ta);

		myevent();

		f.setVisible(true);
	}
	public static void main(String[] args) 
	{
		new Test();
	}
	private void myevent()
	{
		f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		d.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				d.setVisible(false);
			}
		});
		okb.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				d.setVisible(false);
			}
		});

		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();
				}
			}
		});
	}
	public 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
		{
			lab.setText("你輸入的"+tf.getText()+"是錯誤的,請重新輸入");
			d.setVisible(true);
		}
	}
}

10GUI(菜單)

注意:將MenuBar添加到窗體用setMenuBar()

  Menu可以添加Menu,也可以添加MenuItem

示例代碼:

import java.awt.*;
import java.awt.event.*;
class  Test
{
	public static void main(String[] args) 
	{
		new MenuDemo();
	}
}
class MenuDemo
{
	private Frame f;
	private MenuBar mb;
	private Menu m,subMenu;
	private MenuItem closeItem,subItem;

	MenuDemo()
	{
		init();
	}
	public void init()
	{
		f=new Frame("my awt");
		f.setBounds(300,200,200,300);
		f.setLayout(new FlowLayout());

		mb=new MenuBar();
		m=new Menu("文件");
		subMenu=new Menu("子菜單");

		closeItem=new MenuItem("退出");
		subItem=new MenuItem("子條目");

		m.add(closeItem);
		m.add(subMenu);
		subMenu.add(subItem);
		mb.add(m);
		f.setMenuBar(mb);
		myevent();
		f.setVisible(true);
	}
	public void myevent()
	{
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		closeItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
	}
}

11GUI(練習-打開保存文件)

示例代碼:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class  Test
{
	public static void main(String[] args) 
	{
		new MenuDemo();
	}
}
class MenuDemo
{
	private Frame f;
	private MenuBar mb;
	private Menu fileMenu;
	private MenuItem closeItem,openItem,saveItem;
	private TextArea ta;
	private FileDialog opendia,savedia;

	private File file;
	MenuDemo()
	{
		init();
	}
	public void init()
	{
		f=new Frame("my awt");
		f.setBounds(300,200,200,300);

		mb=new MenuBar();
		fileMenu=new Menu("文件");
	
		
		openItem=new MenuItem("打開");
		saveItem=new MenuItem("保存");
		closeItem=new MenuItem("退出");

		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.add(closeItem);

		mb.add(fileMenu);
		f.setMenuBar(mb);

		opendia=new FileDialog(f,"我要打開",FileDialog.LOAD);
		savedia=new FileDialog(f,"我要保存",FileDialog.SAVE);

		ta=new TextArea();
		f.add(ta);
		myevent();
		f.setVisible(true);
	}
	public void myevent()
	{
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		closeItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		openItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				opendia.setVisible(true);
				String dirpath=opendia.getDirectory();
				String dirname=opendia.getFile();
				if(dirpath==null||dirname==null)
				{
					return ;
				}
				ta.setText("");
				File file=new File(dirpath,dirname);
				try
				{
					BufferedReader br=new BufferedReader(new FileReader(file));
					String line=null;
					while((line=br.readLine())!=null)
					{
						ta.append(line+"\r\n");
					}
					br.close();
				}
				catch (Exception e1)
				{
					throw new RuntimeException("讀取失敗");
				}
			}
		});
		saveItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				if(file==null)
				{
				savedia.setVisible(true);
				String dirpath=savedia.getDirectory();
				String dirname=savedia.getFile();
				if(dirpath==null||dirname==null)
				{
					return;
				}
				file=new File(dirpath,dirname);
				}
				
				try
				{
					BufferedWriter bw=new BufferedWriter(new FileWriter(file));
					String text=ta.getText();
					bw.write(text);
					bw.close();
				}
				catch (IOException e1)
				{
					throw new RuntimeException("shibai");
				}
			}
			});
		}
}




 

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity開發</a>、<a href="http://www.itheima.com"target="blank">.Net培訓</a>、期待與您交流! ----------------------







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