📄 cant.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
//登陆模块
class login extends JFrame implements ActionListener
{
private JLabel ly,lm;
private JTextField tfy;
private JPasswordField pfm;
private JButton btq,btqu;
public login(String title)
{
super(title);
Container c=getContentPane();
c.setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
gbc.insets=new Insets(10,10,10,10);
gbc.weightx=0.5;
gbc.weighty=0.5;
gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.WEST;
ly=new JLabel("用户名");
c.add(ly,gbc);
gbc.gridx=1;
gbc.gridy=0;
gbc.gridwidth=2;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
tfy=new JTextField(20);
c.add(tfy,gbc);
gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.WEST;
lm=new JLabel("密码");
c.add(lm,gbc);
gbc.gridx=1;
gbc.gridy=1;
gbc.gridwidth=2;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
pfm=new JPasswordField(20);
c.add(pfm,gbc);
gbc.gridx=1;
gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
btq=new JButton("登陆");
c.add(btq,gbc);
btq.addActionListener(this);
gbc.gridx=2;
gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
btqu=new JButton("退出");
c.add(btqu,gbc);
btqu.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100,100,350,350);
this.show();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null,"数据库连接失败");
}
try{
Connection con=DriverManager.getConnection("jdbc:odbc:cantdb");
Statement state=con.createStatement();
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"数据库连接失败");
}
}
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals("登陆"))
{
String username="",passwords="1",password="123456";
username=tfy.getText();
passwords=pfm.getText();
String sql="SELECT password FROM t2 WHERE 用户名='"+username+"'";
int y=0;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:cantdb");
Statement state=con.createStatement();
ResultSet rs=state.executeQuery(sql);
int n=0;
while(rs.next())
{
password=rs.getString("password");
n++;
}
if(n==0)
{
JOptionPane.showMessageDialog(this,"用户名错误");
y++;
}
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,"查询失败");
}
if(passwords.equals(password))
{
this.dispose();
mainw mw=new mainw("主窗口");
}
else
{
if(y==0)
JOptionPane.showMessageDialog(this,"密码错误");
}
}
if(command.equals("退出"))
{
System.exit(0);
}
}
public static void main(String args[])
{
login log=new login("登陆");
}
}
//主窗口
class mainw extends JFrame implements ActionListener
{
private JMenuBar mb;
private JMenu function,system;
private JMenuItem append,demand,exit,delete,amend,appendp,demandp,exitp,deletep,amendp;
private JPopupMenu pm;
static JLabel ln,lp,la;
static JTextField tfn,tfp,tfa;
public mainw(String title)
{
super(title);
Container c=getContentPane();
c.setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
gbc.insets=new Insets(10,10,10,10);
gbc.weightx=0.5;
gbc.weighty=0.5;
mb=new JMenuBar();
setJMenuBar(mb);
function=new JMenu("功能");
mb.add(function);
system=new JMenu("系统");
mb.add(system);
append=new JMenuItem("添加商品");
function.add(append);
append.addActionListener(this);
demand=new JMenuItem("查询商品信息");
function.add(demand);
demand.addActionListener(this);
amend=new JMenuItem("修改商品信息");
function.add(amend);
amend.addActionListener(this);
delete=new JMenuItem("删除商品");
function.add(delete);
delete.addActionListener(this);
exit=new JMenuItem("退出");
system.add(exit);
exit.addActionListener(this);
appendp=new JMenuItem("添加商品");
appendp.addActionListener(this);
demandp=new JMenuItem("查询商品信息");
demandp.addActionListener(this);
amendp=new JMenuItem("修改商品信息");
amendp.addActionListener(this);
deletep=new JMenuItem("删除商品");
deletep.addActionListener(this);
exitp=new JMenuItem("退出");
exitp.addActionListener(this);
pm=new JPopupMenu();
pm.add(appendp);
pm.add(demandp);
pm.add(amendp);
pm.add(deletep);
pm.addSeparator();
pm.add(exitp);
gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
ln=new JLabel("商品名称");
c.add(ln,gbc);
gbc.gridx=1;
gbc.gridy=0;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
tfn=new JTextField(20);
tfn.setEditable(false);
c.add(tfn,gbc);
gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
lp=new JLabel("商品价格");
c.add(lp,gbc);
gbc.gridx=1;
gbc.gridy=1;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
tfp=new JTextField(20);
tfp.setEditable(false);
c.add(tfp,gbc);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
la=new JLabel("库存数量");
c.add(la,gbc);
gbc.gridx=1;
gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
tfa=new JTextField(20);
tfa.setEditable(false);
c.add(tfa,gbc);
this.addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent event)
{
if(event.isPopupTrigger())
{
pm.show(event.getComponent(),event.getX(),event.getY());
}
}
}
);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100,100,500,500);
this.show();
}
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals("添加商品"))
{
append ap=new append(this,"添加商品",false);
}
if(command.equals("查询商品信息"))
{
demand de=new demand(this,"查询商品信息",false);
}
if(command.equals("修改商品信息"))
{
amend am=new amend(this,"修改商品信息",false);
}
if(command.equals("删除商品"))
{
delete del=new delete(this,"删除商品",false);
}
if(command.equals("退出"))
{
exit ex=new exit(this,"退出",false);
}
}
}
//添加模块
class append extends JDialog implements ActionListener
{
private JLabel ln,lp,la;
private JTextField tfn,tfp,tfa;
private JButton bt,btqu;
public append(Frame owner, String title, boolean modal)
{
super(owner,title,modal);
Container c=getContentPane();
c.setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
gbc.insets=new Insets(10,10,10,10);
gbc.weightx=0.5;
gbc.weighty=0.5;
gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.WEST;
ln=new JLabel("商品名称");
c.add(ln,gbc);
gbc.gridx=1;
gbc.gridy=0;
gbc.gridwidth=2;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
tfn=new JTextField(20);
c.add(tfn,gbc);
gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.WEST;
lp=new JLabel("商品价格");
c.add(lp,gbc);
gbc.gridx=1;
gbc.gridy=1;
gbc.gridwidth=2;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
tfp=new JTextField(20);
c.add(tfp,gbc);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.WEST;
la=new JLabel("库存数量");
c.add(la,gbc);
gbc.gridx=1;
gbc.gridy=2;
gbc.gridwidth=2;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
tfa=new JTextField(20);
c.add(tfa,gbc);
gbc.gridx=1;
gbc.gridy=3;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
bt=new JButton("提交");
c.add(bt,gbc);
bt.addActionListener(this);
gbc.gridx=2;
gbc.gridy=3;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
btqu=new JButton("取消");
c.add(btqu,gbc);
btqu.addActionListener(this);
this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
this.setBounds(150,150,350,350);
this.show();
}
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals("提交"))
{
String name,price,amount;
name=tfn.getText();
price=tfp.getText();
amount=tfa.getText();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:cantdb");
String sql="INSERT INTO t1 VALUES(?,?,?)";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,price);
ps.setString(3,amount);
ps.executeUpdate();
con.close();
JOptionPane.showMessageDialog(this,"添加成功");
}
catch(Exception ex)
{
System.out.println(ex);
}
}
if(command.equals("取消"))
{
this.dispose();
}
}
}
//查询模块
class demand extends JDialog implements ActionListener
{
private JLabel ln;
private JTextField tfn;
private JButton bt,btqu;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -