第十二周Java总结

一、实验

图形界面

package Try;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.security.auth.login.LoginContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

class ActionHandle{
	private JFrame frame = new JFrame("Welcome to MLDN");
	private JButton submit = new JButton("登录");
	private JButton reset = new JButton("重置");
	private JLabel nameLab = new JLabel("用户名:");
	private JLabel passLab = new JLabel("密码:");
	private JLabel infoLab = new JLabel("用户登录系统");
	private JTextField nameText = new JTextField();
	private JPasswordField passText = new JPasswordField();
	
	public ActionHandle(){
		Font fnt = new Font("Serief",Font.BOLD,12);
		infoLab.setFont(fnt);
		submit.addActionListener(new ActionListener(){
			 public void actionPerformed(ActionEvent arg0){
				 if(arg0.getSource() == submit){
					 String tname = nameText.getText();
					 String tpass = new String(passText.getPassword());
					 if(tname.equals("Gi")&&tpass.equals("123456")){
						 infoLab.setText("登陆成功,欢迎光临!");
					 }else {
						 infoLab.setText("登录失败,错误的用户名或密码!");
					 }
				 }
			 } 
		});
		
	    reset.addActionListener(new ActionListener() {
				 public void actionPerformed(ActionEvent arg0) {
				 if(arg0.getSource()==reset) {
					 nameText.setText("");
					 passText.setText("");
					 infoLab.setText("用户登录系统!");
				 }
			 }
		});
	    
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				System.exit(1);
			}
		});
		frame.setLayout(null);
		nameLab.setBounds(5,5,60,20);
		passLab.setBounds(10,30,60,20);
		infoLab.setBounds(5,65,220,30);
		nameText.setBounds(65,5,100,20);
		passText.setBounds(65,30,100,20);
		submit.setBounds(165,5,60,20);
		reset.setBounds(165,30,60,20);
		frame.add(nameLab);
		frame.add(passLab);
		frame.add(infoLab);
		frame.add(nameText);
		frame.add(passText);
		frame.add(submit);
		frame.add(reset);
		frame.setSize(280,130);
		frame.setLocation(300,200);
		frame.setVisible(true);
	}
}
public class Gi {
      public static void main(String args[]) {
    	  new ActionHandle();
      }
}

运行结果:


登录:

重置:

二、总结

容器

JPanel类

JPanel类能够完成各种复杂的界面显示。

JSplitPane类

JSplitPane类的主要功能是分割面板,可以将一个窗体分为两个子窗体,可以是水平排列,也可以是垂直排列。

JTabbedpane类

JTabbedpane类是在一个面板上设置多个选项卡供用户选择。

JScrollPane类

JScrollPane类主要包括JViewport和JScrollBar两部分组成,前者主要是显示一个矩形的区域让用户浏览,而后者主要是形成水平或垂直的滚动条。

JDesktopPane类与JInternalFrame类

JDesktopPane类规定了一个父窗体的基本形式,而JInternalFrame类规定了各个子窗体,JInternalFrame类不能单独使用。

事件处理

EventObject类

定义格式:

public class EventObject extends Object implements Serializable{
	public EventObject(Object source) {
		//构造一个发生事件的对象
	}
	public Object getSource() {
		//返回一个事件对象
	}
	public String toString() {
		//得到信息
	}
} 
原文地址:https://www.cnblogs.com/chixue/p/11868676.html