此项目大体上也就是用java中的jdbc和GUI编程相结合,我实现的功能是运行代码进入到登录界面然后输入用户名和密码存到数据库中最后跳转到写字板界面,下面直接贴上这几天所做的项目源码。
登录界面和连接数据库。
package demo;
import javax.swing.*;
import com.mysql.jdbc.PreparedStatement;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class Log extends JFrame implements ActionListener{
public JLabel j1;
public JLabel j2;
public JLabel j3;
public JTextField jt1;
public JTextField jt2;
public JButton jb1;
public JButton jb2;
//从已创建好的jFrame窗口中取出一个内容面板
Container cp=this.getContentPane();
public Log()
{
super("写字板登录界面");
j1=new JLabel("用户名");
jt1=new JTextField(10);
j2=new JLabel("密码");
jt2=new JTextField(10);
jb1=new JButton("登录");
jb2=new JButton("取消");
j3=new JLabel("");
cp.add(j1);
cp.add(jt1);
cp.add(j2);
cp.add(jt2);
cp.add(jb1);
cp.add(jb2);
cp.add(j3);
cp.setLayout(new FlowLayout());// 采用流式布局
jb1.addActionListener(this); //添加监听器
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jt1.setText("");
jt2.setText("");
j3.setText("您已取消登录");
System.exit(0); //点击取消登录自动退出程序
}
});
setSize(200,200);
setVisible(true); //窗体可见
}
@Override
public void actionPerformed(ActionEvent jb1) {
if(jt1.getText()!=null && jt2.getText()!=null) {
j3.setText("登录成功");
System.out.println("欢迎"+jt1.getText()+"使用java写字板");
Connection conn = null;
PreparedStatement ps = null;
try {
// 采用输入流和类加载器读取文件的4个基本信息
InputStream is = com.mysql.jdbc.Connection.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pro = new Properties();
pro.load(is);
String user = pro.getProperty("user");
String password = pro.getProperty("password");
String url = pro.getProperty("url");
String Driverclass = pro.getProperty("Driverclass");
// 加载驱动
Class.forName(Driverclass);
conn = DriverManager.getConnection(url, user, password);
// 预编译sql语句,返回PreparedStatement对象的实例
String sql = "INSERT INTO user_info(name,password) VALUES(?,?)";
System.out.println("编译成功");
ps = (PreparedStatement)conn.prepareStatement(sql);
ps.setString(1,jt1.getText()); // 数据库的索引默认为1
ps.setString(2,jt2.getText());
ps.executeUpdate(); // 执行更新
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(ps != null)
ps.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(conn != null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
dispose();
new testEventMouse();
}
else
{
System.out.println("登录失败");
System.exit(0);
}
}
public static void main(String[] args) {
new Log();
}
}
写字板
package demo;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class testEventMouse extends JFrame{
Container contentPanel; //画布
JButton b1,b2,b3,b4,b5;
JRadioButton backGroundRadio,foreGroundRadio; //单选按钮
ButtonGroup radioGroup; //容器
int xValue;
int yValue;
JFrame jf=this;
public testEventMouse() {
super("模拟写字板");
b1=new JButton("黑色");
b2=new JButton("蓝色");
b3=new JButton("红色");
b4=new JButton("白色");
b5=new JButton("黄色");
//获取内容格
contentPanel=this.getContentPane();
//按钮组件设置为流式布局
contentPanel.setLayout(new FlowLayout());
//实例化单选框
backGroundRadio=new JRadioButton("背景");
foreGroundRadio=new JRadioButton("画笔");
contentPanel.add(backGroundRadio);
contentPanel.add(foreGroundRadio);
contentPanel.add(b1);
contentPanel.add(b2);
contentPanel.add(b3);
contentPanel.add(b4);
contentPanel.add(b5);
//监听事件
MouseListenerHandler mou=new MouseListenerHandler();
b1.addMouseListener(mou);
b2.addMouseListener(mou);
b3.addMouseListener(mou);
b4.addMouseListener(mou);
b5.addMouseListener(mou);
//实例单选按钮组
radioGroup=new ButtonGroup();
radioGroup.add(backGroundRadio);
radioGroup.add(foreGroundRadio);
addMouseMotionListener(
new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
xValue=e.getX();
yValue=e.getY();
repaint();
}
}
);
setSize(1000,500);
setVisible(true);
}
//监听事件
public class MouseListenerHandler implements MouseListener{
public void mousePressed(MouseEvent e){
//b1-黑色
if(e.getSource()==b1){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.BLACK);
else
jf.setForeground(Color.BLACK);
}
//b2-蓝色
if(e.getSource()==b2){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.BLUE);
else
jf.setForeground(Color.BLUE);
}
//b3-红色
if(e.getSource()==b3){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.RED);
else
jf.setForeground(Color.RED);
}
//b4-白色
if(e.getSource()==b4){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.WHITE);
else
jf.setForeground(Color.WHITE);
}
//b5-黄色
if(e.getSource()==b5){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.YELLOW);
else
jf.setForeground(Color.YELLOW);
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
public void paint(Graphics g){
g.fillOval(xValue, yValue, 3, 3);
System.out.println("("+xValue+","+yValue+")");
}
}
jdbc.properties配置文件内容(注:这个配置文件要放在src下面与子包的同级目录,要不然会报空指针异常)
user=root
password=123456
url=jdbc:mysql://127.0.0.1:3306/test
Driverclass=com.mysql.jdbc.Driver
效果图