JAVA學習筆記(四十四)- AWT組件 (Abstract Window Toolkit)

來源:互聯網
上載者:User

標籤:abstract   awt   表單組件   

Frame表單組件
import java.awt.Color;import java.awt.Frame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/* * Frame表單組件 */public class Test01 {    public static void main(String[] args) {        Frame frame=new Frame("我的表單"); //建立表單        /*frame.setSize(300, 300);//設定表單大小        frame.setLocation(250, 250);//設定表單出現的位置        */              frame.setBounds(250,250,300,300);//同時設定位置和大小        frame.setBackground(Color.red);//設定表單背景顏色        frame.setLocationRelativeTo(null);//設定表單出現在中間        frame.setVisible(true);//顯示表單        //添加監聽事件,當點擊右上方的關閉時關閉表單        frame.addWindowListener(new WindowAdapter() {            @Override            public void windowClosing(WindowEvent e) {                System.exit(0);//當點擊按鈕時退出程式            }        });    }}
Panel面板組件
import java.awt.Color;import java.awt.Dimension;import java.awt.Frame;import java.awt.Panel;/* * Panel面板組件 */public class Test02 {    public static void main(String[] args) {        Frame frame =new Frame("My Frame");        Panel pnl=new Panel();//建立面板        pnl.setSize(new Dimension(100, 100));        pnl.setBackground(Color.yellow);        frame.add(pnl);//將面板添加到表單中        frame.setSize(200, 200);        frame.setBackground(Color.gray);        frame.setLocationRelativeTo(null);        frame.setLayout(null);//取消布局管理器,採用手動布局        frame.setVisible(true);    }}
Label標籤和TextField單行文字框
import java.awt.FlowLayout;import java.awt.Frame;import java.awt.Label;import java.awt.TextField;/* * Label標籤和TextField單行文字框 */public class Test07 extends Frame{    Label lblUsername=new Label("使用者名稱:");    TextField txtUsername=new TextField(10);    public Test07(String title){        setTitle(title);//調用父類的構造方法,設定標題        setLayout(new FlowLayout());        add(lblUsername);//將標籤添加到表單中        add(txtUsername);//將文字框添加到表單中        setSize(200, 200);    }    public static void main(String[] args) {        Test07 test=new Test07("我的表單");        test.setVisible(true);    }}
TextArea多行文字框和Button按鈕
/* * TextArea多行文字框和Button按鈕 */public class Test08 extends Frame{    TextArea txtComment =new TextArea(5, 20);    Button btnOK=new Button("確定");    Button btnCancel=new Button("取消");    public Test08(String title){        super(title);        init();//初始化    }    public void init(){        setLayout(new FlowLayout());        add(txtComment);        add(btnOK);        add(btnCancel);        setSize(200, 200);        setLocationRelativeTo(null);        setVisible(true);    }    public static void main(String[] args) {        new Test08("我的小蘋果");    }}
標題
/* * Checkbox複選框和CheckboxGroup */public class Test09  extends Frame{    Checkbox hobby1=new Checkbox("吃飯");    Checkbox hobby2=new Checkbox("睡覺");    Checkbox hobby3=new Checkbox("打豆豆");    //建立複選框組,將複選框添加到組中,變為選項按鈕    CheckboxGroup sexGroup=new CheckboxGroup();    Checkbox male=new Checkbox("男",true,sexGroup);    Checkbox female=new Checkbox("女",false,sexGroup);    Label lblHobby=new Label("愛好:");    Label lblSex=new Label("性別:");    public Test09(String title){        super(title);        init();    }    public void init(){        setLayout(new GridLayout(7, 1));        add(lblHobby);        add(hobby1);        add(hobby2);        add(hobby3);        add(lblSex);        add(male);        add(female);    }    public static void main(String[] args) {        Test09 test=new Test09("我的表單");        test.setSize(200,200);        test.setLocationRelativeTo(null);        test.setVisible(true);    }}
Choice選擇框和List列表框
import java.awt.Choice;import java.awt.Frame;import java.awt.GridLayout;import java.awt.Label;import java.awt.List;/* * Choice選擇框和List列表框 */public class Test10 extends Frame{    Label lblFruit=new Label("您喜愛的水果:");    Label lblStar=new Label("您喜愛的明星:");    Choice fruits=new Choice();    List starts=new List(2,true);    List lst = new List(4, false);    public Test10(String title){        super(title);        setLayout(new GridLayout(2, 2));        fruits.add("小蘋果");        fruits.add("菠蘿蜜");        fruits.add("香蕉疤瘌");        starts.add("林志玲");        starts.add("劉亦菲");        starts.add("萬麗慶");        lst.add("Mercury");         lst.add("Venus");         lst.add("Earth");         lst.add("JavaSoft");         lst.add("Mars");         lst.add("Jupiter");         lst.add("Saturn");         lst.add("Uranus");         lst.add("Neptune");         lst.add("Pluto");        add(lblFruit);        add(fruits);        add(lblStar);        add(starts);        //add(lst);    }    public static void main(String[] args) {        Test10 test=new Test10("嘿嘿");        test.setSize(300, 300);        test.setLocationRelativeTo(null);        test.setVisible(true);    }}
MenuBar、Menu、MenuItem
import java.awt.FlowLayout;import java.awt.Frame;import java.awt.Menu;import java.awt.MenuBar;import java.awt.MenuItem;import java.awt.MenuShortcut;import java.awt.event.KeyEvent;/* * MenuBar、Menu、MenuItem */public class Test11 {    public static void main(String[] args) {        Frame frame=new Frame("我的表單");        //建立功能表列        MenuBar myMenu=new MenuBar();        //建立菜單        Menu menu1=new Menu("File");        Menu menu2=new Menu("Edit");        //建立功能表項目        MenuShortcut newms=new MenuShortcut(KeyEvent.VK_N, true);//設定快速鍵        MenuItem item1=new MenuItem("New",newms);        MenuItem item2=new MenuItem("Close");        MenuItem item3=new MenuItem("Save");        MenuItem item4=new MenuItem("Copy");        MenuItem item5=new MenuItem("Cut");        MenuItem item6=new MenuItem("Paste");        //將功能表項目添加到菜單中        menu1.add(item1);        menu1.add(item2);        menu1.add(item3);        menu2.add(item4);        menu2.add(item5);        menu2.add(item6);        //將菜單添加到功能表列        myMenu.add(menu1);        myMenu.add(menu2);        frame.setLayout(new FlowLayout());        frame.setSize(200,200);        frame.setLocationRelativeTo(null);        frame.setMenuBar(myMenu);//設定表單的功能表列        frame.setVisible(true);    }}

JAVA學習筆記(四十四)- AWT組件 (Abstract Window Toolkit)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.