java之 22天 GUI 圖形介面編程(一)__GUI

來源:互聯網
上載者:User
GUI(圖形化使用者介面)
import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/** * GUI(圖形化使用者介面) *  Graphical User Interface(圖形使用者介面) *  用圖形的方式,來顯示電腦操作的介面,這樣更方便更直觀. *  * CLI *  Command Line User Interface(命令列使用者介面) *  就是常用的Dos命令列操作. *  需要記憶一些常用的命令.操作更直觀. *  * 舉例: *   比如:建立檔案夾,或者刪除檔案夾等  *   md haha   del haha   *    *    * Java的GUI提供的對象都存在 java.Awt 和 javax.Swing 兩個包中. *  * java.Awt:Abstract Window ToolKit(抽象 視窗工具包) *   需要調用本地系統方法實現功能.屬重量級控制項 (跨平台不夠強) *   * java.Swing:在AWT的基礎上,建立的一套圖形介面系統,器重提供了更多的組件, *   而且完全由java實現,增強了移植性,屬於輕量級控制項.(跨平台很好) *    * java.swt: IBM 公司開發 Eclipse 用的組件工具 可以Eclipse網站下載後就可以使用了. *  *  * 布局管理器 * 1)容器中的組件的排放方式,就是布局. * 2)常見的布局管理器 *   FlowLayout(流式布局管理器) *     從左至右的順序排列 *     Panel預設的布局管理器 *   BorderLayout(辯解布局管理器) *     東  南  西  北   中 *     Frame 預設的布局管理器 *     不指定布局方式,預設 滿屏覆蓋,在添加一個 也是 滿屏覆蓋 *   GridLayout (網格布局管理器) *     規則的矩陣 *   CardLayout  (卡片布局管理器) *     選項卡 *   GridBagLayout(網格包布局管理器) *    非規則的矩陣 *     * 事件監聽機制組成 *  事件來源:   *  事件:Event *  監聽器:Listener *  時間處理:(引發事件後處理方式) *   *  事件來源:就是awt包或者swing包中的那些映像介面組件. *  事件:每個事件來源都有自己特定的對應時間和共性時間. *  監聽器:可以出發某一個事件的動作都已經封裝到監聽器中. */public class GuiDemo {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 MyWin());f.setVisible(true);System.out.println("Hello world!");}}//因為介面WindowLinstener中的所有方法都被子類 WindowAdapter實現了,.//並且覆蓋了其中的所有方法,那麼我們只能繼承 WindowAdapter 覆蓋我們的方法即可class MyWin extends WindowAdapter{@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stub//System.out.println("Window closing"+e.toString());System.out.println("我關了");System.exit(0);}@Overridepublic void windowActivated(WindowEvent e) {//每次獲得焦點 就會觸發System.out.println("我活了");  //super.windowActivated(e);}@Overridepublic void windowOpened(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println("我開了");//super.windowOpened(e);}}


簡單Frame
import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class FrameDemo {//定義該映像中所需要的組件的引用private Frame f;private Button but;FrameDemo(){init();}public void init(){f=new Frame("my freame");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(){@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stub//super.windowClosing(e);System.exit(0);}});//讓按鈕具備退出程式的功能/* * 按鈕就是事件來源 * 那麼選擇那個監聽器呢? * 通過關閉表單執行個體瞭解到,下個要知道那個組件具備什麼樣的特有監聽器, * 需要查看該組件對象的功能. *///添加一個活動監聽but.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSystem.out.println("退出, 按鈕乾的");System.exit(0);}});}public static void main(String[] args) {FrameDemo f=new FrameDemo();}}


鍵盤和滑鼠事件
import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;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.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class MouseAndKeyEvent {private Frame f;private Button but;private TextField tf;public MouseAndKeyEvent() {init();}private void init(){f=new Frame("me frame");f.setBounds(300, 200, 600, 500);f.setLayout(new FlowLayout());tf=new TextField(20);but=new Button("my Botton");f.add(tf);f.add(but);event();f.setVisible(true);}private void event(){f.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubSystem.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();  //不執行加入文字框.}}});but.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSystem.out.println("actionPerformed 活動一次");}});but.addMouseListener(new MouseAdapter() {private int count=0;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("雙擊動作");}elseSystem.out.println("點擊動作"+clickCount++);}});//添加 鍵盤事件but.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e){System.out.println(e.getKeyChar()+"..."+e.getKeyCode());System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());//enter  就退出/*if(e.getKeyCode()==KeyEvent.VK_ENTER)System.exit(0);*///ctrl + Enter 發送訊息if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER){System.out.println("我要發送訊息!");}}});}public static void main(String[] args) {// TODO Auto-generated method stubnew MouseAndKeyEvent();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.