文章目录
1、String(成员方法)
代码如下(示例):
public class String成员方法 {
public static void main(String[] args) {
String s1="hellolworld";
String s2="hellolworld";
String s3="Hellolworld";//首字母大写
String s4=" hellolworld ";//两侧有空格
String s5=" hello world ";//中间有空格
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
//equalsIgnoreCase不区分大小比较(如验证码)
System.out.println(s1.equalsIgnoreCase(s3));
//删除前后空格trim(),不能删除中间的
System.out.println(s4.trim());
System.out.println(s5.trim());
}
}
2、GUI案例1(用户登录实现)
代码如下(示例):
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserLogin {
public static void main(String[] args) {
//生成登录图形窗口
//定义一个窗口
JFrame jf = new JFrame();
//定义窗口大小
jf.setSize(400, 300);
//窗口标题
jf.setTitle("用户登录");
//窗口关闭时程序停止
jf.setDefaultCloseOperation(3);
//取消原来的位置
jf.setLayout(null);
//使窗口位于中间
jf.setLocationRelativeTo(null);
//是窗口一直浮在上面
jf.setAlwaysOnTop(true);
//定义用户名
JLabel jl1 = new JLabel("用户名");
jl1.setBounds(50, 50, 50, 20);
jf.add(jl1);
//用户名输入框
JTextField usernameField = new JTextField();
usernameField.setBounds(150, 50, 180, 20);
jf.add(usernameField);
//定义密码
JLabel jl2 = new JLabel("密码");
jl2.setBounds(50, 100, 50, 20);
jf.add(jl2);
//密码输入框
JTextField passwordField = new JTextField();
passwordField.setBounds(150, 100, 180, 20);
jf.add(passwordField);
//定义一个登录按钮
JButton loginButton = new JButton("登录");
loginButton.setBounds(50, 200, 280, 20);
jf.add(loginButton);
//已知的用户名和密码
String name = "itheima";
String pwd = "123456";
//将输入的用户名和已知的用户名绑定
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取输入的用户名和密码名
String username = usernameField.getText();
String password = passwordField.getText();
//判断输入的用户名和密码是否符合要求
//用户名和密码的长度都是6-12位
if (username.length() < 6 && username.length() > 12) {
// System.out.println("