选择组件的应用
来源:互联网 发布:游戏代练iapp源码 编辑:程序博客网 时间:2024/05/16 19:00
package Demo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestTextField extends Frame implements ActionListener{
TextField txtName;
TextField txtPassword;
Label lblTitle,lblName,lblPassword;
Button btnSubmit;
Button btnRest;
Panel p1,p11,p12,p2;
public TestTextField(){
super("登陆界面");
lblTitle=new Label("请输入您的用户信息");
lblName=new Label("用户名");
txtName=new TextField(10);//创建能容纳10个字符的空文本框
lblPassword=new Label("密码");
txtPassword=new TextField(10);
txtPassword.setEchoChar('*');//设置文本框中的回显字符
btnSubmit =new Button("提交");
btnRest =new Button("重置");
p1=new Panel();
p11=new Panel();
p2=new Panel();
p12=new Panel();
add(lblTitle,"North");
p1.setLayout(new BorderLayout());//边框布局
p11.setLayout(new GridLayout(2,1));//网格布局
p11.add(lblName);
p11.add(lblPassword);
p12.setLayout(new GridLayout(2,1));
p12.add(txtName);
p12.add(txtPassword);
p1.add(p11,"West");//定位置
p1.add(p12,"Center");//定位置
txtName.addActionListener(this);//在txtName上注册监听器
txtPassword.addActionListener(this);//在txtPassword上注册监听器
p2.add(btnSubmit);
p2.add(btnRest);
btnSubmit.addActionListener(this);
btnRest.addActionListener(this);
add(p1,"Center");
add(p2,"South");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);//退出程序
}
});
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();// 获取动作命令,默认是按钮上的标签
if(s.equals("重置")){
clear();
}else if(s.equals("提交")){
submit();
}else if(e.getSource()==txtName)//获取源信息
{
txtPassword.requestFocus();//焦点
}
else if(e.getSource()==txtPassword){
submit();
}
}
public void clear(){
txtName.setText("");
txtPassword.setText("");
}
public void submit(){
String n= txtName.getText();
String paw= txtPassword.getText();
if(n.equals("admin")&&paw.equals("1234")){
JOptionPane.showMessageDialog(this,"合法用户,欢迎进入本系统");
}else{
JOptionPane.showMessageDialog(this,"非法用户,禁止进入本系统");
}
}
public static void main(String[] args){
TestTextField t=new TestTextField();
t.setSize(200,130);
t.setBackground(Color.pink);
t.setLocation(300,200);
t.setVisible(true);
}
}
/*文本框TextField:用于接收/编辑单行文本信息
构造方法
public TextField();
public TextField(int columns);
public TextField(String str);
public TextField(String str,int columns);
常用方法
public String getText();
public void setText(String str)
public String getSelectedText();
public void setEchoChar(char c);
public void setEditable(Boolean b); 触发事件
在TextField组件中按下回车键时,可以触发ActionEvent事件,
因此在TextField组件上可注册ActionListener监听器,
以关联所需的处理逻辑
内部类
在Java GUI 事件处理中,经常采用内部类来定义监听器类,
这是因为监听器类中封装的业务逻辑具有非常强的针对性,
通常没有重用价值。
而且作为内部类的监听器对象可以直接访问外部类中的成员,
这可以提供很大的便利。
匿名类
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
txtDisplay.setText("鼠标位置"+e.getPoint());
}
});
多重监听
由于事件源可以产生多种不同类型的事件,
因而可以注册多种不同类型的监听器,
但是当事件源发生了某种类型的事件时,
只触发事先已就该种事件类型注册过的监听器。
事件源组件和监听器对象的对应关系:
针对同一个事件源组件的同一种事件也可以注册多个监听器。
针对同一个事件源组件的多种事件也可以注册同一个监听器对象进行处置。
只是这要求监听器对象是一个“多面手”,即有能力处理各种不同类型的事件
同一个监听器对象可以被同时注册到多个不同的事件源上。
*/
package Demo;
import java.awt.*;
import java.awt.event.*;
public class TestEvent extends Frame implements ActionListener{
/**
* @param args
*/
Button b1, b2;
Label lbl;
public TestEvent() {
super("匿名类");
lbl = new Label("你好");
b1 = new Button("确定");
b2 = new Button("取消");
// b1.setActionCommand("cofirm");
setLayout(new FlowLayout());
add(lbl);
add(b1);
add(b2);
//MyListener my=new MyListener();
// b1.addActionListener(my);
// b2.addActionListener(my);
b1.addActionListener(this);
b2.addActionListener(this);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
setBounds(200, 200, 300, 200);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestEvent();
}
// class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
/*
Button b=new (Button)e.getSource();//获取事件源
if(b==b1){
lbl.setText("确定");
}
else{
lbl.setText("取消");
}
*/
String str = e.getActionCommand();// 获取动作命令,默认是按钮上的标签
if (e.equals("确定")) {
lbl.setText("确定");
} else {
lbl.setText("取消");
}
}
// }
}
- 选择组件的应用
- aspjpeg组件的应用
- 组件的基本应用
- 日历组件的应用
- cxLookupComboBox 组件的应用
- GridView组件的应用
- RatingBar组件的应用
- ListView组件的应用
- cxLookupComboBox 组件的应用
- ImageButton组件的应用
- JavaEE应用的组件
- 组合选择 的应用
- Tab组件与ListView组件的应用
- android: WheelView组件(滑轮组件)的应用!
- android: WheelView组件(滑轮组件)的应用!
- JFormattedTextField组件的文本选择问题
- 关于NSIS的选择组件描述
- 论数据库访问组件的选择
- Kindle DXG的一些使用方法及技巧
- Failed to create the Java Virtual Machine处理方法
- [荐]JavaMe 编程连载(3) - 也说MVC设计模式
- 世界著名大学校训(中英文版)
- PHP应用:如何清空mysql数据库中所有的表和数据
- 选择组件的应用
- 快速的批量修改重命名.net程序的命名空间
- Django使用技巧(自定义)
- 【中美教育的差异】
- NAND FLASH驱动
- phpmyadmin3安装配置
- 2.23-将A,B链表合并成C
- wdcp忘记密码
- Jcrontab - java定时程序进阶学习