9.4 AWT组件库

目录

1.Button

2.复选款Checkbox/复选款CheckboxGroup

3.下拉式菜单

4.单行文本输入区TextField

5.TextArea文本输入区

6.Dialog/FileDialog对话框

菜单问题 


1.Button

按钮被点击后产生ActionEvent,需要ActionListener监听。

2.复选款Checkbox/复选款CheckboxGroup

提供on/off开关  ,参数new Checkbox(String name,null,state),state参数设置默认是否勾选

checkbox可以选中多个或1个,但是checkboxgroup只能选中一个

3.下拉式菜单

new Choice() 创建下拉列表对象,add方法添加选项对象,使用ItemListener监听

4.单行文本输入区TextField

触发ActionEvent,使用ActionListener监听器

5.TextArea文本输入区

使用ActionEvent对文本进行处理

6.Dialog/FileDialog对话框

构造函数FileDialog(parentframe,String name);

setVisible(boolean);

d.getFile();

package AWTComponent;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;

public class MyButton{
    public Frame fr;
    public Panel panel;
    public Button bt;
    public Color[] colorList = {Color.RED,Color.ORANGE,Color.GREEN,Color.PINK};
    public int pressCounter=0;
    public TextField tf;
    public Panel panelWest;
    public Checkbox cb1,cb2,cb3;
    public Choice chooserCenter;
    public TextArea textSouth;
    public List centerList;
    public static void main(String[] args){
        MyButton b = new MyButton();
        b.toShow();
    }
    public void toShow(){
        fr = new Frame("ButtonTest");
        fr.setSize(1200,800);
        fr.setVisible(true);

        bt = new Button("OK");
        bt.setBackground(Color.PINK);
        tf = new TextField(10);

        panelWest = new Panel();
        panelWest.setLayout(new GridLayout(3,1));
        panelWest.setBackground(Color.PINK);
        cb1 = new Checkbox("1.",null,false);
        cb1.addItemListener(new SelectItem());
        cb2 = new Checkbox("2.");
        cb2.addItemListener(new SelectItem());
        cb3 = new Checkbox("3.");
        cb3.addItemListener(new SelectItem());
        panelWest.add(cb1);
        panelWest.add(cb2);
        panelWest.add(cb3);

        chooserCenter = new Choice();
//        chooserCenter.setBackground(Color.RED);
        chooserCenter.add("Green");
        chooserCenter.add("Red");
        chooserCenter.add("Blue");

        Panel centerPanel = new Panel();
        centerPanel.setLayout(new BorderLayout());
        centerList = new List(4,false);
        centerList.setBackground(Color.RED);
        centerList.add("US");
        centerList.add("AFR");
        centerList.add("UK");
        centerList.add("ZH");
        centerPanel.add("North", chooserCenter);
        centerPanel.add("South",centerList);
        Button fileOpen = new Button("FileOpen");
        fileOpen.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                FileDialog fd = new FileDialog(fr,"hahahah");
                fd.setVisible(true);
                String filename = fd.getFile();
                System.out.println(filename);
            }
        });
        centerPanel.add("Center",fileOpen);


        textSouth = new TextArea();
        textSouth.setBackground(Color.YELLOW);

        fr.add(BorderLayout.SOUTH,textSouth);
        fr.add(BorderLayout.CENTER,centerPanel);
        fr.add(BorderLayout.NORTH,bt);
        fr.add(BorderLayout.EAST,tf);
        fr.add(BorderLayout.WEST,panelWest);

        bt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pressCounter < colorList.length){
                    if(e.getSource()==bt){
                        bt.setBackground(colorList[pressCounter]);
                        pressCounter++;
                    }
                }
                else{
                    tf.setText("颜色用尽了!!!");
                    tf.setForeground(Color.red);
                    tf.setBackground(Color.CYAN);

                }
            }
        });
        fr.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
    public class SelectItem implements ItemListener{
        @Override
        public void itemStateChanged(ItemEvent e) {
            if(e.getSource()==cb1){
                cb1.setBackground(Color.RED);
            }
            else if(e.getSource() == cb2){
                cb2.setBackground(Color.YELLOW);
            }
            else if(e.getSource()==cb3){
                cb3.setBackground(Color.DARK_GRAY);
            }
        }
    }
}

菜单问题

只能通过MenuBar(菜单容器)添加菜单,其只能被添加到frame中作为菜单跟基;

Menu菜单类,需要添加到容器中

MenuItem菜单选项,需要添加到菜单中

都需要ActionListener监听器

{
    public static void main(String[] args) {
        Frame fr = new Frame("Menu");
        MenuBar m = new MenuBar();
        m.add(new Menu("File"));
        m.add(new Menu("Edit"));
        Menu third = new Menu("Dota");
        third.add(new MenuItem("SAVE"));
        m.add(third);
        m.setHelpMenu(new Menu("Help"));
        fr.setMenuBar(m);
        fr.setSize(150,100);
        fr.setVisible(true);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值