您的位置:首页 > 编程语言 > Java开发

NIIT实训 java笔记--3.18

2016-03-19 22:47 671 查看

1, Eclipse 安装插件

离线安装:

Help ->Install New Software -> Work with: ... Add ->在Location 上点击Archie,选择提前下载好的插件压缩包,OK -> 选择复选框,去除Contact .....,更新软件前的复选框 -> next.

在线安装:

Help ->Eclipse Marketplace.. -> 搜索插件

2,为类添加get,set 方法:

在程序编辑框中右键 -> Source -> Generate Getters and Setters..

3, 把代码排版格式化:

在程序编辑框中右键 -> Source -> Format

4, == 与 equals方法:

对于引用数据类型来说, == 是判断两个引用变量是否引用同一个对象,string.equals(),判断值是否相等。对于引用类型,new() 会产生一个新的对象。

对于自己定义的普通的对象来说,object.equals() 相当于调用了 == 。 当然我们也可以重写equals方法,让它判断值是否相等。

5,if - else if - else 语句

对于一个判断多个条件满足时才符合的情况,可以使用if语句嵌套(if条件为满足规定),也可以使用 if - else if - else 语句(if条件为不满足规定),这样当满足第一条规定时接着判断else if 条件的第二个规定,直到最后。 使用if - else if - else结构看起来要比if条件嵌套的可读性强。

例如一个注册用户时判断输入是否合法:

package com.yrs.app.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginWindow extends JFrame{
private JTextField txtName, txtEmail, txtPpdMoney;
private JPasswordField txtPwd, txtRePwd;
private JLabel lblName, lblPwd, lblRePwd, lblEmail, lblPpdMoney ;
private JButton btnOK, btnCancel;

public LoginWindow() {
txtName = new JTextField();
txtEmail = new JTextField();
txtPpdMoney = new JTextField();
txtPwd = new JPasswordField();
txtRePwd = new JPasswordField();
lblName = new JLabel("用户名");
lblPwd = new JLabel("密码");
lblRePwd = new JLabel("重复密码");
lblEmail = new JLabel("Email");
lblPpdMoney = new JLabel("充值金额");
btnOK = new JButton("确认");
btnCancel = new JButton("取消");

//设置组件位置与大小
lblName.setBounds(20, 20, 55, 25);
txtName.setBounds(lblName.getX()+lblName.getWidth()+5, lblName.getY(), 250, 25);
lblPwd.setBounds(lblName.getX(), txtName.getY()+txtName.getHeight()+15, lblName.getWidth(), lblName.getHeight());
txtPwd.setBounds(txtName.getX(), lblPwd.getY(), txtName.getWidth(), txtName.getHeight());
lblRePwd.setBounds(lblName.getX(), txtPwd.getY()+txtPwd.getHeight()+15, lblName.getWidth(), lblName.getHeight());
txtRePwd.setBounds(txtName.getX(), lblRePwd.getY(), txtName.getWidth(), txtName.getHeight());
lblEmail.setBounds(lblName.getX(), txtRePwd.getY()+txtRePwd.getHeight()+15, lblName.getWidth(), lblName.getHeight());
txtEmail.setBounds(txtName.getX(), lblEmail.getY(), txtName.getWidth(), txtName.getHeight());
lblPpdMoney.setBounds(lblName.getX(), txtEmail.getY()+txtEmail.getHeight()+15, lblName.getWidth(), lblName.getHeight());
txtPpdMoney.setBounds(txtName.getX(), lblPpdMoney.getY(), txtName.getWidth(), txtName.getHeight());
btnOK.setBounds(txtName.getX()+45, txtPpdMoney.getY()+txtPpdMoney.getHeight()+15, 70, 25);
btnCancel.setBounds(btnOK.getX()+btnOK.getWidth()+20, btnOK.getY(), 70, 25);

//添加监听事件

btnOK.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
//isValidData();  //以if语句嵌套的形式
isValidData2();  //以if - else if - else 形式,可读性更强
}
});

btnCancel.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
int rst = JOptionPane.showConfirmDialog(btnCancel,
"确认退出?", "确认",
JOptionPane.OK_CANCEL_OPTION);
if (rst == JOptionPane.OK_OPTION) {
LoginWindow.this.dispose();
}
}
});

this.add(lblName);
this.add(txtName);
this.add(lblPwd);
this.add(txtPwd);
this.add(lblRePwd);
this.add(txtRePwd);
this.add(lblEmail);
this.add(txtEmail);
this.add(lblPpdMoney);
this.add(txtPpdMoney);
this.add(btnOK);
this.add(btnCancel);

this.setTitle("注册");
this.setLayout(null);
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
}

public void isValidData() {
String name = txtName.getText();
char [] pwd = txtPwd.getPassword();
char [] rePwd = txtRePwd.getPassword();
//System.out.println(pwd);
String emaile = txtEmail.getText();
String ppdMoney = txtPpdMoney.getText();
//判断输入的内容是否符合要求
if(name.matches("^[A-Za-z][A-Za-z0-9]+$")) {  //用户名是由字母和数字组成,要有字母开头
if(String.valueOf(pwd).matches("^[A-Za-z]\\w{5,12}$")){
boolean judge = true;
for(int i = 0; i < pwd.length; i++) {
if(pwd[i] != rePwd[i]) {
JOptionPane.showMessageDialog(LoginWindow.this, "两次输入密码不同");
judge = false;
break;
}
}
if(judge) {
if(emaile.matches("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?")){
if(ppdMoney.matches("^[1-9]\\d*|0$")) {
double rslMoney = 2.0 * (Double.parseDouble(ppdMoney));
JOptionPane.showMessageDialog(LoginWindow.this, "注册成功,现有余额"+rslMoney);
txtName.setText("");
txtPwd.setText("");
txtRePwd.setText("");
txtEmail.setText("");
txtPpdMoney.setText("");
}
else {
JOptionPane.showMessageDialog(LoginWindow.this, "充值金额不正确");
}
}
else {
JOptionPane.showMessageDialog(LoginWindow.this, "邮箱格式不正确");
}

}

}
else{
JOptionPane.showMessageDialog(LoginWindow.this, "密码格式不正确");
}
}
else {
JOptionPane.showMessageDialog(LoginWindow.this, "用户名格式不正确");
}

}

public void isValidData2() {
String name = txtName.getText();
char [] pwd = txtPwd.getPassword();
char [] rePwd = txtRePwd.getPassword();
String emaile = txtEmail.getText();
String ppdMoney = txtPpdMoney.getText();
if(!name.matches("^[A-Za-z][A-Za-z0-9]+$")){
JOptionPane.showMessageDialog(LoginWindow.this, "用户名格式不正确");
}
else if(!String.valueOf(pwd).matches("^[A-Za-z]\\w{5,12}$")) {
JOptionPane.showMessageDialog(LoginWindow.this, "密码格式不正确");
}
else if(!new String(pwd).equals(new String(rePwd))) {
JOptionPane.showMessageDialog(LoginWindow.this, "两次输入密码不同");
}
else if(!emaile.matches("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?")) {
JOptionPane.showMessageDialog(LoginWindow.this, "邮箱格式不正确");
}
else if(!ppdMoney.matches("^[1-9]\\d*|0$")) {
JOptionPane.showMessageDialog(LoginWindow.this, "充值金额不正确");
}
else {
double rslMoney = 2.0 * (Double.parseDouble(ppdMoney));
JOptionPane.showMessageDialog(LoginWindow.this, "注册成功,现有余额"+rslMoney);
txtName.setText("");
txtPwd.setText("");
txtRePwd.setText("");
txtEmail.setText("");
txtPpdMoney.setText("");
}
}

public static void main(String[] args) {
new LoginWindow().setVisible(true);
}

}


6,抽象类和接口

抽象类:abstract class , 不能直接new抽象类

如果一个类继承了一个有抽象方法的抽象类,作为子类,该类必须实现抽象方法(Override),除非这个子类也是抽象类。普通方法可以不实现。

抽象类是指类是什么,接口是指类能做什么

抽象类有一般类所有的特性:属性、构造器、普通方法

在接口中出现的方法全部是public abstract的,在接口中只能定义常量

抽象类是单继承的,类实现接口可以是多实现,接口继承接口可以多继承

类继承抽象类extends,接口继承接口extends

类实现接口implements,类要实现接口中的所有方法。

相同点:抽象类和接口都不能实例化,都不能new一个抽象类或者接口

7,内部类

内部的类,可以直接调用外部类的任何属性方法,内部类调用外部类的this需要加上外部类的名称,例如:MyFrame.this

8,基本数据类型的Wrapper

byte Byte

short Short

int Integer

long Long

char Character

float Float

double Double

boolean Boolean

装箱Boxing:把基本数据类型转换成引用数据类型

拆箱Unboxing:把引用数据类型转换成基本数据类型

只有引用数据才能用new

9,异常

引发异常

抛出异常

方法内部throw,方法声明处throws

子类和父类的throw:

如果子类覆盖父类有throws的方法,子类的方法可以不写throws,要throws的话必须是父类throws异常的本身或着是子类(必须有继承关系的异常)

异常抛出后不做任何处理,性能高

处理异常:

try {

//可能会出现异常的代码

} catch{

//处理异常

}finally {

//不管抛不抛异常,该语句都会执行

}

多重异常处理:

1:

Catch(Exception1 | Exception2 | ..... | Exception e) {

// e.printStackTrace() 在控制台打印出异常

}

2.1:多重catch块

Catch(Exception1e) {

//

}

Catch(Exception2e) {

//

}

.....

多重catch块中,如果异常有继承关系,那么子类异常要写在前面。

10, final关键字

final标记的变量不能重新赋值(变成常量),而且必须要初始化,只能赋值一次。(如果是普通数据类型,这个变量不能被重新赋值,如果是引用类型,不能重新赋值的是引用,它的属性(对象属性)是可以改变的。

final 方法不能被覆盖(重写)

final 类不能被继承
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: