[Java] Font/Color/Event 字体/颜色/事件综合

import java.awt.*;
import java.awt.event.*;
public class FontTest {
    Frame f = new Frame("FontTest");
    Panel p1 = new Panel();
    Panel p2 = new Panel();
    Panel p3 = new Panel();
    Panel p4 = new Panel();
    Panel p5 = new Panel();
    Label lblName = new Label("用户名:");
    Label lblPwd = new Label("密码:");
    Button btnShow = new Button("ShowInfo");
    Button btnClean = new Button("CleanInfo");
    TextArea taShow = new TextArea(10,40);
    TextField tfName = new TextField(10);
    TextField tfPwd = new TextField(10);
    Font font = new Font("Verdana",1,12);
       
    public void Start(){
       
        //设置lblName and lblPwd的属性
        //lblName.setBackground(Color.YELLOW);
        //lblPwd.setBackground(Color.YELLOW);
        lblName.setForeground(Color.GREEN);
        lblPwd.setForeground(Color.GREEN);
        lblName.setFont(new Font("楷体",1,15));
        lblPwd.setFont(new Font("宋体",1,15));
       
        //设置btnShow and btnClean的属性
        btnShow.setBackground(Color.RED);
        btnClean.setBackground(Color.RED);
        btnShow.setForeground(Color.GREEN);
        btnClean.setForeground(Color.GREEN);
        btnShow.setFont(new Font("宋体",1,15));
        btnClean.setFont(new Font("宋体",1,15));
       
        //设置taShow的属性
        taShow.setEditable(false);//设置taShow不可编辑
        taShow.setBackground(Color.WHITE);//设置背景色
        taShow.setForeground(Color.RED);//设置前景色,字体颜色
        taShow.setFont(font);
       
        //给每个组件设置布局
        p1.setLayout(new FlowLayout(0));
        p2.setLayout(new FlowLayout(0,12,10));
        p3.setLayout(new BorderLayout());
        p4.setLayout(new FlowLayout(0,27,0));
        p5.setLayout(new BorderLayout());
        p1.add(lblName);
        p1.add(tfName);
        p2.add(lblPwd);
        p2.add(tfPwd);
        p3.add(p1,BorderLayout.NORTH);
        p3.add(p2,BorderLayout.SOUTH);
        p4.add(btnShow);
        p4.add(btnClean);
        p5.add(p3,BorderLayout.NORTH);
        p5.add(p4,BorderLayout.CENTER);
        p5.add(taShow,BorderLayout.SOUTH);
        f.add(p5);
       
        //给frame添加匿名的关闭事件
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        //给btnClean添加匿名事件
        btnClean.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                taShow.setText("");
            }
        });
       
        //给btnShow添加匿名事件
        btnShow.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String strName= tfName.getText();
                String strPwd = tfPwd.getText();
                String strShow;
                //判断tfName 和 tfPwd 是否为空值,用String的equals方法
                if(strName.equals("")){
                    strShow = "用户名不能为空,请重新输入用户名!/n";
                }
                else if(strPwd.equals("") ){
                    strShow = "密码不能为空,请重新输入密码!/n";
                }
                else{
                    strShow = "User Name:"+strName + ", Password:" +strPwd+"/n";
                    tfName.setText("");
                    tfPwd.setText("");
                }
                //将字符串输出到taShow中
                taShow.append(strShow);
               
            }   
        });
       
        //设置frame可见
        f.setBackground(Color.BLUE);
        f.setLocationByPlatform(true);
        f.pack();
        f.setVisible(true);
    }
    //main方法
    public static void main(String[] args){
        new FontTest().Start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值