📄 cant2.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());
c.setBackground(Color.pink);
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="",password="";
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;
Connection con;
Statement st;
ResultSet r;
JTable table;
public mainw(String title)
{
super(title);
this.setSize(500,500);
Container c=getContentPane();
c.setLayout(null);
c.setSize(500,500);
//p.add(c);
/*GridBagConstraints gbc=new GridBagConstraints();
gbc.insets=new Insets(10,10,10,10);
gbc.weightx=0.5;
gbc.weighty=0.5;*/
NewPanel p=new NewPanel();
p.setLayout(null);
p.setSize(500,500);
c.add(p);
/*JLabel lb=new JLabel("欢迎来到网上购物中心");
lb.setSize(400,340);
lb.setLocation(100,150);
Font f1=new Font("华文彩云",Font.BOLD,30);
lb.setFont(f1);
lb.setForeground(Color.red);
p.add(lb);*/
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);
JLabel ln=new JLabel("商品名称");
ln.setSize(200,200);
ln.setLocation(10,10);
Font f1=new Font("华文彩云",Font.BOLD,30);
/*c.gridx=0;
c.gridy=0;
c.gridwidth=1;
c.gridheight=1;
c.fill=GridBagConstraints.NONE;
c.anchor=GridBagConstraints.CENTER;*/
ln.setFont(f1);
ln.setForeground(Color.red);
p.add(ln);
//c.add(ln);
/*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);
tfn.setSize(160,26);
tfn.setLocation(200,100);
p.add(tfn);
/*gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;*/
JLabel lp=new JLabel("商品价格");
lp.setSize(200,200);
lp.setLocation(30,100);
Font f2=new Font("华文彩云",Font.BOLD,30);
lp.setFont(f2);
lp.setForeground(Color.red);
//lp=new JLabel("商品价格");
p.add(lp);
/*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);
tfp.setSize(160,26);
tfp.setLocation(200,200);
p.add(tfp);
/*gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;*/
JLabel la=new JLabel("库存数量");
la.setSize(200,200);
la.setLocation(50,200);
Font f3=new Font("华文彩云",Font.BOLD,30);
la.setFont(f3);
la.setForeground(Color.red);
//la=new JLabel("库存数量");
p.add(la);
/*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);
tfa.setSize(160,26);
tfa.setLocation(200,300);
p.add(tfa);
//table = new JTable();
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,"添加成功");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -