API章节



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("
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值