GUI編程(三)-----非容器組件

來源:互聯網
上載者:User

標籤:sla   style   name   about   儲存   lin   layout   rip   code   

常用的非容器組件有:JLabel、JTextField、JPasswordField、JRadioButton、JCheckBox、JComboBox、JTextArea。

基本用法:

public static void main(String[] args) {        JFrame frame= new JFrame("註冊");        frame.setSize(500, 400);        frame.setLocationRelativeTo(null);                //建立一個面板        JPanel panel = new JPanel();        frame.add(panel);         //使用者名稱        JLabel nameLabel = new JLabel("使用者名稱");        //使用者名稱的輸入框        JTextField nameField = new JTextField(12);        //把使用者名稱的組件添加到面板上        panel.add(nameLabel);        panel.add(nameField);                //密碼        JLabel passLabel= new JLabel("密碼");        //密碼框        JPasswordField passField = new JPasswordField(12);        //把密碼的組件添加到面板        panel.add(passLabel);        panel.add(passField);                //性別--單選框        JLabel sexLabel = new JLabel("性別");        JRadioButton man = new JRadioButton("男",true);        JRadioButton woman = new JRadioButton("女");        //如果是單選框必須要進行分組,同一個組的單選框只能選擇其中的一個        ButtonGroup group = new ButtonGroup();        group.add(woman);        group.add(man);        //把性別組件添加到面板上        panel.add(sexLabel);        panel.add(man);        panel.add(woman);                //來自城市--->下拉框        JLabel cityLabel = new JLabel("來自的城市");        Object[]  arr = {"北京","上海","廣州","深圳","湛江"};        JComboBox citys = new JComboBox(arr);        panel.add(cityLabel);        panel.add(citys);                //興趣愛好---->複選框        JLabel hobitLabel = new JLabel("興趣愛好:");        JCheckBox checkBox1 = new JCheckBox("籃球",true);        JCheckBox checkBox2 = new JCheckBox("java",true);        JCheckBox checkBox3 = new JCheckBox("javascript");        JCheckBox checkBox4 = new JCheckBox("android");        panel.add(hobitLabel);        panel.add(checkBox1);        panel.add(checkBox2);        panel.add(checkBox3);        panel.add(checkBox4);                        //簡歷        JLabel jLabel = new JLabel("簡歷");        JTextArea area = new JTextArea(20, 15);        area.setLineWrap(true); //設定自動換行         panel.add(jLabel);        panel.add(area);        frame.setVisible(true);}

菜單組件

菜單組件是用來描述一個菜單的。主要用到的組件有JMenuBar、JMenu、JMenuItem

用法如下:

 1 public class Demo2 { 2      3     JFrame frame = new JFrame("記事本"); 4      5     //菜單條 6     JMenuBar bar = new JMenuBar(); 7      8     //檔案菜單 9     JMenu fileMenu = new JMenu("檔案");10     JMenu editMenu  = new JMenu("編輯");11     12     JMenu switchMenu = new JMenu("切換工作目錄");13     14     15     //功能表項目16     JMenuItem openMenu = new JMenuItem("開啟");17     JMenuItem saveMenu = new JMenuItem("儲存");18  19     JMenuItem aboutMenu = new JMenuItem("關於");20     JMenuItem closeMenu = new JMenuItem("關閉");21     22     23     JMenuItem  workMenu1 = new JMenuItem("0910project");24     JMenuItem  workMenu2 = new JMenuItem("1208project");25     JMenuItem  workMenu3 = new JMenuItem("1110project");26     27     28     29     JTextArea area = new JTextArea(20,30);30     31     public void initNotepad(){32         //菜單添加功能表項目33         fileMenu.add(openMenu);34         fileMenu.add(saveMenu);35         36         editMenu.add(aboutMenu);37         editMenu.add(closeMenu);38         39         //複選菜單40         switchMenu.add(workMenu1);41         switchMenu.add(workMenu2);42         switchMenu.add(workMenu3);43         //菜單添加菜單就是複選菜單44         fileMenu.add(switchMenu);45         46         47         48         //菜單條添加菜單49         bar.add(fileMenu);50         bar.add(editMenu);51         52         //添加菜單條53         frame.add(bar,BorderLayout.NORTH);54         frame.add(area);55         frame.setSize(500, 400);56         frame.setLocationRelativeTo(null);57         frame.setVisible(true);58     }59     60     public static void main(String[] args) {61         new Demo2().initNotepad();62     }63 }

 

GUI編程(三)-----非容器組件

聯繫我們

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