欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Java筆記(八 圖形界面編程)

Java筆記(八 圖形界面編程)

    AWT(Abstract Windows Toolkit),抽象窗口工具包,SUN公司提供的用于圖形界面編程(GUI)的類(lèi)庫?;镜腁WT庫處理用戶(hù)界面元素的方法是把這些元素的創(chuàng )建和行為委托給每個(gè)目標平臺上(Windows、Unix、Macintosh等)的本地GUI工具進(jìn)行處理。例如:如果我們使用AWT在一個(gè)Java窗口放置一個(gè)按鈕,那么實(shí)際上是用的是一個(gè)具有本地外觀(guān)和感覺(jué)的按鈕。這樣,從理論上來(lái)說(shuō),我們說(shuō)編寫(xiě)的圖形界面程序能運行在任何平臺上,做到了圖形界面程序的跨平臺運行
    Component是Java中所有圖形界面組件的一個(gè)基類(lèi)
    Container是Component的子類(lèi),一個(gè)容器(Container)是可以容納其他組件的組件
    Window繼承了Container,A Window object is a top-level window with no borders and no menubar.
    Frame繼承自Window,A Frame is a top-level window with a title and a border。要產(chǎn)生一個(gè)圖形界面的程序,首先就要產(chǎn)生一個(gè)框架窗口
    布局管理器
        容器里組件的位置和大小是由布局管理器來(lái)決定的。容器對布局管理器的特定實(shí)例保持一個(gè)引用。當容器需要定位一個(gè)組件時(shí),它將調用布局管理器來(lái)完成。當決定一個(gè)組件的大小時(shí),也是如此
        在A(yíng)WT中,給我們提供了五種布局管理器:BorderLayout(缺省)、FlowLayout、GridLayout、CardLayout、GridBagLauout
    AWT事件模型
       Events(事件):描述發(fā)生了什么的對象
       Event source(事件源):事件的產(chǎn)生器
       Event handlers(事件處理器):接收事件對象、解釋事件對象并處理用戶(hù)交互的方法
       委托模型:事件監聽(tīng)器是實(shí)現了監聽(tīng)器接口(java.awt.event中的WindowListenter)的類(lèi)。一個(gè)監聽(tīng)器對象是一個(gè)實(shí)現了專(zhuān)門(mén)的監聽(tīng)器接口的類(lèi)的實(shí)例
import java.awt.*;
import java.awt.event.*;
public class MyFrame
{
   public static void main(String[] args)
   {
      Frame f=new Frame("llilac");//Frame(String title):Constructs a new, initially invisible Frame object with the specified title.
      f.setSize(600,400);//public void setSize(int width,int height):Resizes this component so that it has width width and height height.
      f.setLocation(100,100);//左上角是計算機屏幕的原點(diǎn)public void setLocation(int x,int y):Moves this component to a new location. The top-left corner of the new location is specified by the x and y parameters in the coordinate space of this component‘s parent.
      f.setBackground(Color.GREEN);//public void setBackground(Color c):Sets the background color of this component.
      //f.setLayout(new BorderLayout(10,10));//缺省布局管理器BorderLayout
      //f.setLayout(new FlowLayout(FlowLayout.LEFT));
      f.setLayout(new GridLayout(3,2,10,10));//3行2列,垂直水平間隙為10
     
      Button btn1=new Button("hi");//Button(String label):Constructs a Button with the specified label.
      Button btn2=new Button("hello");
      Button btn3=new Button("Nice");
      Button btn4=new Button("to");
      Button btn5=new Button("metting");
      f.add(btn1,"North");//void add(Component comp, Object constraints):Adds the specified component to the end of this container.
      f.add(btn2,"South");
      f.add(btn3,"West");
      f.add(btn4,"East");
      f.add(btn5,"Center");//要在f.show()前面
      //f.addWindowListener(new MyWindowListener());//注冊一個(gè)事件監聽(tīng)器
      //f.addWindowListener(new YouWindowListener());
      f.addWindowListener(new WindowAdapter()//用匿名的內部類(lèi),因為WindowAdapter是抽象類(lèi),所以加上{}進(jìn)行實(shí)現。采用此種方法時(shí),不再需要MyWindowListener類(lèi)和YouWindowListener類(lèi)
                          {
                              public void windowClosing(WindowEvent e) //Invoked when the user attempts to close the window from the window‘s system menu.
                               {
                                    System.exit(0);
                               }
                          });
      f.show();//繼承自Window,public void show():Makes the Window visible. If the Window and/or its owner are not yet displayable, both are made displayable. The Window will be validated prior to being made visible. If the Window is already visible, this will bring the Window to the front.還有一個(gè)方法,是繼承自Component,public void setVisible(boolean b):Shows or hides this component depending on the value of parameter b. 但這個(gè)方法只能讓窗口可見(jiàn),不能讓窗口到最前面
   }
}
class MyWindowListener implements WindowListener//定義了一個(gè)事件監聽(tīng)器類(lèi)
{
   public void windowActivated(WindowEvent e)
   {
    
   }
   public void windowClosed(WindowEvent e)//用來(lái)對窗口已經(jīng)關(guān)閉之后的事件進(jìn)行響應Invoked when a window has been closed as the result of calling dispose on the window. 
   {
     
   }
   public void windowClosing(WindowEvent e) //Invoked when the user attempts to close the window from the window‘s system menu.
   {
          System.exit(0);
   }
   public void windowDeactivated(WindowEvent e)
   {
    
   }
   public void windowDeiconified(WindowEvent e)
   {
    
   }
   public void windowIconified(WindowEvent e)
   {
    
   }
   public void windowOpened(WindowEvent e)
   {
    
   } 
}//如果用的是實(shí)現接口的話(huà),就要實(shí)現接口中所有的類(lèi)。這樣很麻煩。于是Java提供了一種類(lèi)叫適配器類(lèi),接口WindowListener對應的適配器類(lèi)是WindowAdapter,它實(shí)現了WindowListener的所有方法,但都是空實(shí)現。
class YouWindowListener extends WindowAdapter//Java把適配器類(lèi)WindowAdapter聲明為abstract,因為它對接口方法的實(shí)現都是空實(shí)現,所以直接實(shí)例化適配器的一個(gè)類(lèi)是沒(méi)有意義的,所以Java把他聲明為了abstract,以防用戶(hù)直接實(shí)例化。我們只要直接從適配器類(lèi)WindowAdapter派生一個(gè)子類(lèi)就行了
{
   public void windowClosing(WindowEvent e) //Invoked when the user attempts to close the window from the window‘s system menu.
   {
          System.exit(0);
   }
}
-----------------------------------------------------------------------------
//實(shí)現Win記事本部分功能
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class HisFrame//創(chuàng )建菜單:菜單欄(MenuBar類(lèi))-菜單(Menu類(lèi))-菜單項(MenuItem類(lèi))
{
   public static void main(String[] args)
   {
      final Frame f=new Frame("http://llilac");
      f.setSize(600,400);
      f.setLocation(100,100);
      /*構造文本域,只能存放單行文本
      TextField tf=new TextField(20);
      f.add(tf,"North");
      */
      final TextArea tf=new TextArea();
      f.add(tf);
      f.addWindowListener(new WindowAdapter()//窗口監聽(tīng)器
                          {
                              public void sindowClosing(WindowEvent e)
                              {
                                System.exit(0); 
                              } 
                          });
                         
      MenuBar mb=new MenuBar();
      Menu m1=new Menu("File");
      Menu m2=new Menu("Edit");
      MenuItem mi1=new MenuItem("New");
      MenuItem mi2=new MenuItem("open");
      mi2.addActionListener(new ActionListener()//打開(kāi)文件功能
                            {
                               public void actionPerformed(ActionEvent e)
                               {
                                  FileDialog fd=new FileDialog(f,"Open File Dialog",FileDialog.LOAD);//public FileDialog(Frame parent,String title,int mode):parent - the owner of the dialog;title - the title of the dialog;mode - the mode of the dialog; either FileDialog.LOAD or FileDialog.SAVE
                                  fd.show();
                                  /*把文件的內容讀進(jìn)來(lái),此時(shí)只能讀入當前目錄下的文件,因為getFile()知識返回文件名,構建輸入流后它只在當前目錄下查找,所以要想讀取其它目錄的文件,要給出路經(jīng)
                                  String strFile=fd.getFile();
                                  if(strFile!=null)
                                  {
                                    try
                                    {
                                       FileInputStream fis=new FileInputStream(strFile);
                                       byte[] buf=new byte[10*1024];
                                       int len=fis.read(buf);
                                       tf.append(new String(buf,0,len)); 
                                       fis.close();
                                    } 
                                    catch (Exception ex)
                                    {ex.printStackTrace();}
                                  }
                                  */
                                  //把文件的內容讀進(jìn)來(lái),此時(shí)可以讀取其它目錄的文件,因為給出了路經(jīng)
                                  String strFile=fd.getDirectory()+fd.getFile();//獲取的是完整的路徑名
                                  if(strFile!=null)
                                  {
                                    try
                                    {
                                       FileInputStream fis=new FileInputStream(strFile);
                                       byte[] buf=new byte[10*1024];
                                       int len=fis.read(buf);
                                       tf.append(new String(buf,0,len));
                                       fis.close(); 
                                    } 
                                    catch (Exception ex)
                                    {ex.printStackTrace();}
                                  }
                                 
                               }
                            });
      MenuItem mi3=new MenuItem("save");
      MenuItem mi4=new MenuItem("exit");
      mi4.addActionListener(new ActionListener()//退出功能
                            {
                               public void actionPerformed(ActionEvent e)
                               {
                                  System.exit(0); 
                               }
                            });
      MenuItem mi5=new MenuItem("copy");
      MenuItem mi6=new MenuItem("paste");
     
      m1.add(mi1);
      m1.add(mi2);
      m1.add(mi3);
      m1.add(mi4);
      m2.add(mi5);
      m2.add(mi6);
      mb.add(m1); 
      mb.add(m2);
     
      f.setMenuBar(mb); 
      f.show();
   }
}
--------------------------------------------------------------------------------
java.awt包中還有一些表較常用的組件,比如choice—下拉列表框;Checkbox—復選框;Label—標簽

    Java基礎類(lèi)
      JFC(Java foundation Classes):Java基礎類(lèi),是關(guān)于GUI組件和服務(wù)的完整集合,主要包括5個(gè)API:AWT、Java2D、Accessiblility、Drag&Drop、Swing。JFC提供了幫助開(kāi)發(fā)人員設計復雜應用程序的一整套應用程序開(kāi)發(fā)包
      Java2D是一圖形API,它為Java應用程序提供了一套高級的有關(guān)二維(2D)圖形圖像處理的類(lèi),Java2D API擴展了java.awt和java.awt.image類(lèi),并提供了豐富的繪圖風(fēng)格,定義了復雜圖形的機制和精心調節繪制過(guò)程的方法和類(lèi)。這些API使得獨立于平臺的圖形應用程序的開(kāi)發(fā)更加簡(jiǎn)便
      Accessiblility API提供了一套高級工具,用以輔助開(kāi)發(fā)使用非傳統輸入和輸出的應用程序。它提供了一個(gè)輔助的技術(shù)接口,如:屏幕閱讀器,屏幕放大器,聽(tīng)覺(jué)文本閱讀器(語(yǔ)音處理)等等
      Drag&Drop技術(shù)提供了Java和本地應用程序之間的互操作性,用來(lái)在Java應用程序和不支持Java技術(shù)的應用程序之間交換數據
      JFC模塊的重點(diǎn)在Swing。Swing用來(lái)進(jìn)行基于窗口的應用程序開(kāi)發(fā),它提供了一套豐富的組件和工作框架,以指定GUI如何獨立于平臺地展現其視覺(jué)效果

    javax.Swing中的組件都是以JComponent這個(gè)類(lèi)為基類(lèi)的
import javax.swing.*;
public class SwingTest
{
  public static void main(String[] args)
  {
    JFrame jf=new JFrame("llilac");
    jf.setSize(600,400);
    jf.setLocation(100,100);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//public static final int EXIT_ON_CLOSE:The exit application default window close operation.
    JButton btn=new JButton("l");
    jf.getContentPane().add(btn,"West");//首先獲取一個(gè)內容面板,然后將組件增加到內容面板上
    jf.show();
  }
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Java中如何關(guān)閉Frame窗口
JAVA事件的總結2
JavaSwing圖形界面編程之布局管理器(一)
《Java程序設計教程》09 圖形用戶(hù)界面
定時(shí)器
java圖形界面應用程序
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久