最简单的java演示实例(最简单的java演示实例)

发布时间:2022-11-13

本文目录一览:

  1. 急!求一个简单Java程序实例,十分感谢!
  2. 用java设计一个简单的界面设计,越简单越好,谢谢
  3. 用java编写一个简单例子,题目如下
  4. [如何编写一个简单的java web前后端实例](#如何编写一个简单的java web前后端实例)

急!求一个简单Java程序实例,十分感谢!

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
class MyException extends Exception {
    MyException(String message) {
        System.out.println(message);
    }
}
public class TDemo {
    public static void main(String[] args) throws Exception {
        File file = new File("d:\\test.txt");
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String str;
            while ((str = br.readLine()) != null) {
                if (str == "error") {
                    throw new MyException("自定义异常");
                }
                System.out.println(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

用java设计一个简单的界面设计,越简单越好,谢谢

用java设计一个简单的界面可以参考如下实例:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
public class UserLogIn extends JFrame {
    public JPanel pnluser;
    public JLabel lbluserLogIn;
    public JLabel lbluserName;
    public JLabel lbluserPWD;
    public JTextField txtName;
    public JPasswordField pwdPwd;
    public JButton btnSub;
    public JButton btnReset;
    public UserLogIn() {
        pnluser = new JPanel();
        lbluserLogIn = new JLabel();
        lbluserName = new JLabel();
        lbluserPWD = new JLabel();
        txtName = new JTextField();
        pwdPwd = new JPasswordField();
        btnSub = new JButton();
        btnReset = new JButton();
        userInit();
    }
    public void userInit() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 200);
        this.setResizable(false);
        this.setTitle("用户登录");
        this.pnluser.setLayout(null);
        this.pnluser.setBackground(Color.cyan);
        this.lbluserLogIn.setText("用户登录");
        this.lbluserLogIn.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 14));
        this.lbluserLogIn.setForeground(Color.RED);
        this.lbluserName.setText("用户名:");
        this.lbluserPWD.setText("密    码:");
        this.btnSub.setText("登录");
        this.btnReset.setText("重置");
        this.lbluserLogIn.setBounds(120, 15, 60, 20);
        this.lbluserName.setBounds(50, 55, 60, 20);
        this.lbluserPWD.setBounds(50, 85, 60, 25);
        this.txtName.setBounds(110, 55, 120, 20);
        this.pwdPwd.setBounds(110, 85, 120, 20);
        this.btnSub.setBounds(85, 120, 60, 20);
        this.btnSub.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnsub_ActionEvent(e);
            }
        });
        this.btnReset.setBounds(155, 120, 60, 20);
        this.btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnreset_ActionEvent(e);
            }
        });
        this.pnluser.add(lbluserLogIn);
        this.pnluser.add(lbluserName);
        this.pnluser.add(lbluserPWD);
        this.pnluser.add(txtName);
        this.pnluser.add(pwdPwd);
        this.pnluser.add(btnSub);
        this.pnluser.add(btnReset);
        this.add(pnluser);
        this.setVisible(true);
    }
    public void btnsub_ActionEvent(ActionEvent e) {
        String name = txtName.getText();
        String pwd = String.valueOf(pwdPwd.getPassword());
        if (name.equals("")) {
            JOptionPane.showMessageDialog(null, "账号不能为空", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        } else if (pwd.equals("")) {
            JOptionPane.showMessageDialog(null, "密码不能为空", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        } else if (true) {
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "账号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
    }
    public void btnreset_ActionEvent(ActionEvent e) {
        txtName.setText("");
        pwdPwd.setText("");
    }
    public static void main(String[] args) {
        new UserLogIn();
    }
}

用java编写一个简单例子,题目如下

package test;
public class Student {
    private String name;
    private String id;
    private String clazz;
    private int age;
    private String address;
    /**
     * sayHello方法
     */
    public void sayHello() {
        System.out.println("学号为" + this.id + "的同学的具体信息如下:");
        System.out.println("姓名:" + this.name);
        System.out.println("班级:" + this.clazz);
        System.out.println("年龄:" + this.age);
        System.out.println("家庭住址:" + this.address);
    }
    /**
     * 测试方法
     *
     * @param args
     */
    public static void main(String[] args) {
        // 第一问
        Student student = new Student();
        student.setAddress("百度知道");
        student.setAge(1);
        student.setClazz("一班");
        student.setId("071251000");
        student.setName("lsy605604013");
        student.sayHello();
        // 第二问
        Student studentNew = new Student();
        studentNew.setAddress("搜搜知道");
        studentNew.setAge(2);
        studentNew.setClazz("二班");
        studentNew.setId("071251001");
        studentNew.setName("lady");
        if (student.getAge() > studentNew.getAge())
            studentNew.sayHello();
        else if (student.getAge() < studentNew.getAge())
            student.sayHello();
        else
            System.out.println("两人一样大");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getClazz() {
        return clazz;
    }
    public void setClazz(String clazz) {
        this.clazz = clazz;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

如何编写一个简单的java web前后端实例

你说的是一个完整的BS请求--响应模式,很多种方式都可以实现。 最简单的方法:写一个 form 表单,然后配置servlet ,提交数据到servlet,在servlet中实现你的逻辑如你保存数据库的操作,然后由servlet 转发到jsp 页面进行网页响应就可以了。ITjob的朋友跟我分享过的,发给你了不用谢~