1.计算器计算系统登录页面代码
package log;
import javax.swing.*;
import java.awt.*;
public class login{
public static void main(String[] args) {
//创建一个主要框架,将其命名为”登录"
JFrame jFrame = new JFrame("登录");
jFrame.setSize(900,507);
jFrame.setLayout(null);
//添加标签【计算器计算系统】
JLabel textStudentManage = new JLabel("计算器计算系统");
textStudentManage.setForeground(new Color(0x0010FF));
textStudentManage.setFont(new Font("黑体", Font.PLAIN,50));
textStudentManage.setBounds(280,50,800,100);
jFrame.add(textStudentManage);
//添加标签【用户名】
JLabel textUser = new JLabel("用户名:");
textUser.setForeground(new Color(0xFF0000));
textUser.setFont(new Font("黑体", Font.PLAIN,30));
textUser.setBounds(200,140,200,100);
jFrame.add(textUser);
//添加输入框【用户名输入框】
JTextField user = new JTextField(20);
user.setFont(new Font("黑体", Font.PLAIN,18));
user.setSelectedTextColor(new Color(0xFF0000));
user.setBounds(330,170,280,40);
jFrame.add(user);
//添加标签【密码】
JLabel textPassword = new JLabel("密码 :");
textPassword.setForeground(new Color(0xFF0000));
textPassword.setFont(new Font("黑体", Font.PLAIN,30));
textPassword.setBounds(200,200,200,100);
jFrame.add(textPassword);
//添加密码输入框【密码】
JPasswordField password = new JPasswordField(20);
password.setBounds(330,230,280,40);
jFrame.add(password);
//添加按钮【登录】
JButton jButton = new JButton("登录");
jButton.setForeground(new Color(0x023BF6));
jButton.setBackground(new Color(0x38FF00));
jButton.setFont(new Font("黑体", Font.PLAIN,20));
jButton.setBorderPainted(false);
jButton.setBounds(300,330,100,50);
jFrame.add(jButton);
//添加按钮【注册】
JButton register = new JButton("注册");
register.setForeground(new Color(0x0029FF));
register.setBackground(new Color(0xECA871));
register.setFont(new Font("黑体", Font.PLAIN,20));
register.setBorderPainted(false);
register.setBounds(500,330,100,50);
jFrame.add(register);
//对按钮事件进行处理
jButton.addActionListener((e -> {
String pwd = new String(password.getPassword());
if(user.getText().equals("sleep")){
if(pwd.equals("123456")){
//密码账号正确,进入计算器计算系统
//进入计算器计算系统
jFrame.setVisible(false);//将登录界面设定为不可见
//new StudentManage().StudentMainInterface();
}else{
//密码不正确
JOptionPane.showMessageDialog(jFrame,"密码错误","提示",JOptionPane.INFORMATION_MESSAGE);
//将密码框置空
password.setText("");
}
}else{
//用户名错误
JOptionPane.showMessageDialog(jFrame,"用户名错误","提示",JOptionPane.INFORMATION_MESSAGE);
//将用户名和密码置空
user.setText("");
password.setText("");
}
}));
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setResizable(false);
jFrame.setVisible(true);
}
}
运行结果:


程序流程图如图所示:

2.计算器代码
package log;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Computer extends JFrame implements ActionListener {
private JPanel jp_north=new JPanel();
private JTextField input_text=new JTextField();
private JButton c_Bth=new JButton("C");
private JPanel jp_center=new JPanel();
public Computer() throws HeadlessException {
this.init();
this.addNorthComponent();
this.addCenterButton();
}
//初始化
public void init(){
this.setTitle("计算器");
this.setSize(500,500);
this.setLayout(new BorderLayout());
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//添加上边的按钮
public void addNorthComponent(){
this.input_text.setPreferredSize(new Dimension(420,70));
jp_north.add(input_text);
this.c_Bth.setForeground(Color.PINK);
jp_north.add(c_Bth);
c_Bth.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input_text.setText("");
}
});
this.add(jp_north,BorderLayout.NORTH);
}
//添加中间按钮
public void addCenterButton(){
String btn_text="123+456-789*0.=/";
String regex="[\\+\\-*/,=]";
this.jp_center.setLayout(new GridLayout(4,4));
for(int i=0;i<16;i++){
String temp=btn_text.substring(i,i+1);
JButton btn=new JButton();
btn.setText(temp);
if(temp.matches(regex)){
btn.setFont(new Font("粗体",Font.BOLD,20));
btn.setForeground(Color.PINK);
}
btn.addActionListener(this);
jp_center.add(btn);
}
this.add(jp_center,BorderLayout.CENTER);
}
public static void main(String[] args) {
Computer computer=new Computer();
computer.setVisible(true);
}
private String firstInput=null;
private String operator=null;
@Override
public void actionPerformed(ActionEvent e) {
String clickstr=e.getActionCommand();
if(".0123456789".indexOf(clickstr)!=-1) {
this.input_text.setText(input_text.getText()+clickstr);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
//JOptionPane.showMessageDialog(this,clickstr);
}else if(clickstr.matches("[\\+\\-*/]{1}")){
operator=clickstr;
firstInput=this.input_text.getText();
this.input_text.setText("");
}else if(clickstr.equals("=")){
Double a=Double.valueOf(firstInput);
Double b=Double.valueOf(this.input_text.getText());
Double result=null;
switch(operator) {
case "+":
result = a + b;
break;
case "-":
result = a - b;
break;
case "*":
result = a * b;
break;
case "/":
if (b != 0) {
result = a / b;
}
break;
}
this.input_text.setText(result.toString());
}
}
}
运行结果:

3.连接数据库代码
package log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class sql {
static Connection conn=null;
public Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.print("mySQL的驱动");//看数据库驱动是否加载完成
String url="jdbc:mysql:///liveshop?useUnicode=true&characterEncoding=UTF-8";//数据库地址
String password="123456";//数据库用户名的密码
String username="sleep";//数据库用户名的名字
conn=DriverManager.getConnection(url, username, password);//
System.out.println("与数据库连接");//
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static <CreateMysql> void main(String[] arg) {
sql mySQL=new sql ();
PreparedStatement preparedStatement = null;
ResultSet resultSet= null;
try {
mySQL.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql="select * from address where name = ?";
try {
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1,"myt");
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
System.out.println(resultSet.getString("name"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(resultSet!=null) {
try {
resultSet.close();//释放资源的细节 后开先关,先开后关,数据库连接数有限制的,如果连接不关闭,使用的人比较多,那么系统容易崩溃
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if(preparedStatement!=null) {
try {
preparedStatement.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();//释放资源
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4.编译环境
此程序代码使用IntelliJ IDEA 编写
5.实验分析
没有充分掌握如何将所有代码连接起来,代码仍有部分瑕疵