importjavax.swing.*;importjava.awt.*;importjava.io.*;importjava.awt.event.*;importjava.sql.*;publicclassMyFrameimplementsActionListener{JFramef;JLabelname,Mima;JTextField...
import javax.swing.*;import java.awt.*;import java.io.*;import java.awt.event.*;import java.sql.*;public class MyFrame implements ActionListener{ JFrame f; JLabel name,Mima; JTextField tname,tMima; JPanel p1,p2,p3; JButton btlogin,btcancel,Zc; public MyFrame() { f=new JFrame("登录界面"); name=new JLabel("姓名:"); Mima=new JLabel("密码:"); tname=new JTextField(10); tMima=new JTextField(10); btlogin=new JButton("登陆"); btcancel=new JButton("取消"); Zc=new JButton("注册"); btlogin.addActionListener(this); btcancel.addActionListener(this); Zc.addActionListener(this); f.setLocation(100,100); f.setSize(300,400); p1=new JPanel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); p2=new JPanel(); p2.setLayout(new FlowLayout(FlowLayout.LEFT)); p3=new JPanel(); p3.setLayout(new FlowLayout(FlowLayout.LEFT)); p1.add(name); p1.add(tname); p2.add(Mima); p2.add(tMima); p3.add(btlogin); p3.add(Zc);p3.add(btcancel); JPanel p=new JPanel(); p.setLayout(new GridLayout(3,1)); p.add(p1); p.add(p2);p.add(p3); f.add(p); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new MyFrame(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==Zc) { String btnevent=e.getActionCommand(); String user=tname.getText(); String pass=tMima.getText(); String sql="insert into user values('"+user+"','"+pass+"')"; ConnDB conn=new ConnDB(); int num=conn.executeUpdate(sql); if(num>=1){ JOptionPane.showMessageDialog(null, "由于您"+btnevent+"了,注册成功,请登录!\n", "注册成功", JOptionPane.PLAIN_MESSAGE); }else{ JOptionPane.showMessageDialog(null, "鼠标按下,您注册失败,请重新注册!\n", "注册失败", JOptionPane.PLAIN_MESSAGE); } } else if(e.getSource()==btlogin) { String btnevent1=e.getActionCommand(); String user=tname.getText(); String pass=tMima.getText(); String sql= "select * from user where user='" +user+ "' and pass='"+pass+ "'"; ConnDB conn=new ConnDB(); ResultSet num1=conn.executeQuery(sql); if(num1==null) { Massages get=new Massages(); get.Mass(); }else{ JOptionPane.showMessageDialog(null, "您还不是用户,请先注册!\n", "登录失败", JOptionPane.PLAIN_MESSAGE); } } else if(e.getSource()==btcancel) { ConnDB conn=new ConnDB(); conn.closeConnDB(); } } }
展开