學習了一天,把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();
}
}