java awt學習筆記


     最近這兩天,花了些時間溫習了java.awt的學習,故今日花些時間寫下自己的總結吧。

1.常見的組件:Button、TextArea、Label、Checkbox、TextField

Container---Window(Frame,Dialog)、Panel

布局管理器:

FlowLayout(流式布局管理器)
從左到右的順序排列。

Panel默認的布局管理器。

BorderLayout(邊界布局管理器)

東,南,西,北,中

Frame默認的布局管理器。

GridLayout(網格布局管理器)

規則的矩陣

CardLayout(卡片布局管理器)

選項卡

GridBagLayout(網格包布局管理器)

非規則的矩陣

2.創建圖形化界面:

<1>創建frame窗體。
<2>對窗體進行基本設置。比如大小,位置,布局。
<3>定義組件。
<4>將組件通過窗體的add方法添加到窗體中。
<5>讓窗體顯示,通過setVisible(true)

3.事件監聽機制

事件監聽機制的特點:
<1>事件源。
<2>事件。
<3>監聽器。
<4>事件處理。

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

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

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

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

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

4.窗體事件

f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);    
            }
        });

5.Action事件

closeItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });

6.鼠標事件

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++);
            }
        });

7.鍵盤事件

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

                //System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());
            }
        });

8.Dialog的使用

9.菜單的應用

MenuBar,Menu,MenuItem
先創建菜單條,再創建菜單,每一個菜單 中建立菜單項。
也可以菜單添加到菜單中,作為子菜單。
通過setMenuBar()方法,將菜單添加到Frame中。

一個簡單的記事本程序代碼如下:

package mymenu ;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class menu1 {
    Frame f;
    MenuBar bar;
    Menu m1;
    TextArea ta=new TextArea();
    MenuItem openItem,saveItem,exitItem;
    FileDialog openDialog,saveDialog;
    private File fl;
    public menu1(){
        inite();
    }
    private void inite(){
        f=new Frame("FileList");
        f.setSize(300, 400);
        f.setLocation(100, 200);
        bar=new MenuBar();
        m1=new Menu("file");
        openItem=new MenuItem("open");
        saveItem=new MenuItem("save");
        exitItem=new MenuItem("exit");
        bar.add(m1);
        ta=new TextArea();
        m1.add(openItem);
        m1.add(saveItem);
        m1.add(exitItem);
        openDialog=new FileDialog(f,"openFile",FileDialog.LOAD);
        saveDialog=new FileDialog(f,"saveFile",FileDialog.SAVE);
        f.setMenuBar(bar);
        getEvent();
        f.add(ta);
        f.setVisible(true);
        
    }


    private void getEvent() {
        saveItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if(fl==null){
                    saveDialog.setVisible(true);
                    String dirPath=saveDialog.getDirectory();
                    String fileName=saveDialog.getFile();
                    if(dirPath==null||fileName==null)
                        return;
                    fl=new File(dirPath,fileName);
                }
                try {
                    BufferedWriter bfw=new BufferedWriter(new FileWriter(fl));
                    bfw.write(ta.getText());
                    bfw.flush();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }    
        });
        openItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                openDialog.setVisible(true);
                String dirPath=openDialog.getDirectory();
                String fileName=openDialog.getFile();
                if(dirPath==null||fileName==null)
                    return;
                ta.setText("");
               fl=new File(dirPath,fileName);
                try {
                    BufferedReader bfr=new BufferedReader(new FileReader(fl));
                    String str=null;
                    while((str=bfr.readLine())!=null){
                        ta.append(str+"\r\n");
                    }
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

            }    
        });
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        exitItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }    
        });
        
    }


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

}

10.jar包雙擊執行

利用java -d  編譯生成mymenu包,再在當前目錄建立一個文本文件,不妨為1.txt,則內容為:

Main-Class: mymenu.menu1

注意空格以及末尾的換行

這是輸入jar包生成指令:

jar -cvfm my.jar 1.txt mymenu

則會生成 my.jar

WIN7系統下打開Jar文件時報錯,提示"Could not find the main class" 的警告。

通過修改注冊表來解決該問題。

      步驟一:打開注冊表,開始->運行(或者用快捷鍵WIN+R),輸入regedit,確定;

      步驟二:找個Jar文件,選擇打開方式,輸入D:/Program Files/Java/jre/bin/javaw.exe,再選擇打開就行了;

      步驟三:進入HKEY_CLASSES_ROOT/Applications/javaw.exe/shell/open/command,修改默認的鍵值為 "D:/Program Files/Java/jre/bin/javaw.exe" -jar "%1" 。

Java環境安裝在其他地方也類似,只要改一下文件地址就行了。

eclipse導出jar包

http://jingyan.baidu.com/article/5bbb5a1b280d0113eba179ce.html

http://blog.csdn.net/memray/article/details/17969443


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM