package gui;
import java.awt.*;
public class TestCardLayout extends Frame{
/**
* @param args
*/
Label[] b=new Label[4];
public TestCardLayout(){
super("卡片布局");
CardLayout c1=new CardLayout();
setLayout(c1);
for(int i=0;i<b.length;i++){
b[i]=new Label("第"+i+"页");
add(b[i],"card"+i);
}
//c1.show(this, name);
b[0].setBackground(Color.green);
b[1].setBackground(Color.red);
b[2].setBackground(Color.pink);
b[3].setBackground(Color.blue);
setSize(200,100);
setLocation(300,600);
setVisible(true);
while(true)
{
try{Thread.sleep(1000);}
catch(InterruptedException e){
e.printStackTrace();
}
c1.next(this);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestCardLayout();
}
}
/*CardLayout:卡片布局
布局效果:将多个组件在同一容器区域内交替显示,
相当于多张卡片摞在一起,
只有最上面的卡片是可见的。
构造方法
public CardLayout()
public CardLayout(int?hgap, int?vgap)
其他方法
public void first(Container parent)—显示第一张卡片
public void last(Container parent)—显示最后一张卡片
public void previous(Container parent)—显示前一张卡片
public void next(Container parent)—显示后一张卡片
public void show(Container parent,String name*/
package gui;
import java.awt.*;
import java.awt.event.*;
public class SecondFrame extends Frame{
/**
* @param args
*/
Button b;Label lbl;
public SecondFrame(){
super("我的第二个窗体");
b=new Button("确认");
lbl=new Label("我不喜欢你啦 呵呵呵呵 ");
setLayout(new FlowLayout());
add(lbl);
add(b);
//实例化一个监听器对象
MyEventListener my=new MyEventListener();
//在button上注册监听器
b.addActionListener(my);//使用前先实例化对象,然后在传入
//b1.addActionListener(my);
this.setSize(200,100);
this.setLocation(300,100);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SecondFrame ();
} //定义监听器类
public class MyEventListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println("按钮被按下啦");
lbl.setText("大家好对不起");
}
}
}
/*监听器:实现了相应接口的类,要实现啥接口,要看处理啥事件
事件源(Event Source):能够产生事件的GUI组件对象,如按钮、文本框等。
事件处理方法(Event Handler):能够接收、解析和处理事件类对象,实现与用户交互功能的方法。
事件监听器(Event Listener):调用事件处理方法的对象。
*/
package gui;
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=new txtName.getText();
String paw=new 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.setLocale(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监听器,
以关联所需的处理逻辑*/