多窗体

                                                     多窗体

     

   多窗体,这是小编结合前面发的几篇而写成的。通过登录页面登录后,进入到另一个页面。java的窗体一般是通过 java.awt.*  awt 这个类来实现的。 

 

登录页面代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JOptionPane;

public class UserLogin implements ActionListener {
	
	 Frame app = new Frame("登录窗体");
	  
	 Label lblName = new Label("登录名:");
	 static TextField txtName = new TextField(); 
	  
	  Label lblPass = new Label("密码:");
	  static TextField txtPass = new TextField();
	   
	  //   新添加一个Button 按钮
	   Button bt = new Button("确定登录");
	   Button bt2 = new Button("退出登录");
	   
	 public UserLogin(){
		 app.setSize(300, 200);
		 app.setLayout(null);
		  
		  lblName.setBounds(60, 50, 70, 20);
		  txtName.setBounds(135, 50, 100, 20);
		  
		  
		  txtName.addKeyListener(new KeyHandler());
		  txtName.addFocusListener(new FocusHandler());
		    
		  lblPass.setBounds(60, 90, 70, 20);
		  txtPass.setBounds(135, 90, 100, 20);
		   
		   txtPass.addKeyListener(new KeyHandler());
		   txtPass.addFocusListener(new FocusHandler());
		   
		   app.add(lblName);
		   app.add(txtName);
		   app.add(lblPass);
		   app.add(txtPass);
		   
		   // 将Button 按钮放入窗体中去。
		   app.add(bt);
		   app.add(bt2);
		   bt.setBounds(55, 140, 60, 30);
		   bt2.setBounds(182, 140, 60, 30);
		   bt.addActionListener(this);
		   bt2.addActionListener(this);
		  
		   app.setLocation(200, 100);
		   app.setBackground(Color.orange);
		   app.setResizable(false);
		   app.setVisible(true);
	 }
	
	
	public static void main(String[] args)
	{
		UserLogin tft = new UserLogin();
		 
	}
	 
	 public void actionPerformed(ActionEvent e){
		 Object ob = e.getSource();
		 if (ob == bt2){
			 System.exit(0);
		 }
		 
		 else if(ob == bt)
		  
		 {
              
              new  ScrollbarTest();
            
               
		 }
		  
		 
	 }
	
	

}

class KeyHandler implements KeyListener{

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO 自动生成的方法存根
		Object ob = e.getSource();
		 if(( ob == UserLogin.txtName )&&( e.getKeyCode() == 10 )){
//			 System.out.println(UserLogin.txtName.getText());
		 }
		 else if(( ob == UserLogin.txtPass )&&( e.getKeyCode() == 10 )){
//			 System.out.println( UserLogin.txtPass.getText() );
		 }
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO 自动生成的方法存根
		
	}
  
	 
}
class FocusHandler implements FocusListener{

	@Override
	public void focusGained(FocusEvent e) {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void focusLost(FocusEvent e) {
		// TODO 自动生成的方法存根
		
		Object ob = e.getSource();
		 
		if(( ob == UserLogin.txtName ) ){
//			 System.out.println(UserLogin.txtName.getText());
		 }
		 else if(( ob == UserLogin.txtPass ) ){
//			 System.out.println( UserLogin.txtPass.getText() );
		 }
		
	}
	
}

 

 

滚动条窗体页面 code:
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

public class ScrollbarTest implements AdjustmentListener,ActionListener {
	   
	Frame app = new Frame("滚动条");
	  
	 // 三个标签数组
	 Label lblColor[] = {new Label("Red:"),new Label("Green:"),new Label("Blue:")};
	  
	    
	 // 三个滚动条
	 Scrollbar  scbColor[] = {
			 new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,255),
			 new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,255),
			 new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,255),};
	                                                                                                   
	   // Button 按钮
	    Button bt1 = new Button("提交");  
	    Button bt2 = new Button("退出");
	       public ScrollbarTest(){
	    	   sbinit();
	       }
	      
	     
	 
	   void sbinit(){
		   
		   app.setSize(260,300);
		   app.setLayout(null);
		    // 设置滚动条和标签的大小
		   for(int k = 0;k<3;k++){
			   lblColor[k].setBounds(110, 60+60*k, 50, 20);
			   scbColor[k].setBounds(30, 85+60*k, 200, 20);
			   scbColor[k].addAdjustmentListener(this);
		   }
		      
		    //  该for循环用来设置标签和滚动条的背景颜色
		     for(int i = 0;i<3;i++){
		    	 lblColor[0].setBackground(Color.red);
		    	 scbColor[0].setBackground(Color.red);
		    	 lblColor[1].setBackground(Color.green);
		    	 scbColor[1].setBackground(Color.green);
		    	 lblColor[2].setBackground(Color.blue);
		    	 scbColor[2].setBackground(Color.blue);
		     }
		    	 
		     
		   
		     //  三个标签和三个滚动条添加到窗体中。
		    for(int k = 0;k<3;k++){
		    	app.add(lblColor[k]);
		    	app.add(scbColor[k]);
		    }
		    
		    bt1.setBounds(30, 250, 75,35);
		    bt2.setBounds(160, 250, 75,35);
		    bt1.setBackground(Color.orange);
		    bt2.setBackground(Color.MAGENTA);
		    bt1.addActionListener(this);
		    bt2.addActionListener(this);
		    
		    
		    
		    
		    app.setLocation(200, 100);
		    app.setVisible(true);
      	    app.setResizable(false);  
 		    app.setBackground(Color.gray);
 		    app.add(bt1);
 		    app.add(bt2);
	   }
	
	
	   
	   
	
	public static void main(String[] args){
		ScrollbarTest sbt = new ScrollbarTest();
		sbt.sbinit();
		
	}

	@Override
	  
	public void adjustmentValueChanged(AdjustmentEvent e) {
		// TODO 自动生成的方法存根
		Object ob = e.getSource();
		if(ob == scbColor[0]){
			System.out.println(scbColor[0].getValue()+" ");
		}
		else if(ob == scbColor[1]){
			System.out.println(scbColor[1].getValue()+" ");
		}
		else if(ob == scbColor[2]){
			System.out.println(scbColor[2].getValue()+" ");
		}
		
	}
	 
	
	 //  ActionEvent 事件
	 public void actionPerformed(ActionEvent e){
		 Object ob = e.getSource();
		  // 当点击退出按钮时,窗体就回退出。
		 if(ob == bt2){ 
			 System.exit(0);
		 }
		 else if(ob == bt1){
			 Object str = "选择你喜爱的滚动条?";
			 JOptionPane.showInputDialog(str);
		 }
		  
		 
	 }
	
	

}

    注意,一般通过Button来实现第二个窗体或者第三个或者更多窗体。一般调用上级窗体的构造函数方法来实现

 

posted @ 2016-04-11 23:19  PengWenHao  阅读(258)  评论(0编辑  收藏  举报