?? cant.java
字號:
public demand(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=1;
gbc.gridy=1;
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=1;
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,400,200);
this.show();
}
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals("提交"))
{
String name="",price="",amount="";
name=tfn.getText();
String sql="SELECT * FROM t1 WHERE 商品名稱='"+name+"'";
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())
{
name=rs.getString("商品名稱");
price=rs.getString("商品價格");
amount=rs.getString("庫存數量");
n++;
}
if(n==0)
{
JOptionPane.showMessageDialog(this,"沒有查到相關資料");
}
else
{
mainw.tfn.setText(name);
mainw.tfp.setText(price);
mainw.tfa.setText(amount);
}
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,"查詢失敗");
}
this.dispose();
}
if(command.equals("取消"))
{
this.dispose();
}
}
}
//刪除模塊
class delete extends JDialog implements ActionListener
{
private JLabel ln;
private JTextField tfn;
private JButton bt,btqu;
public delete(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=1;
gbc.gridy=1;
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=1;
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(130,150,450,200);
this.show();
}
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals("提交"))
{
String name="";
name=tfn.getText();
String sql="DELETE FROM t1 WHERE 商品名稱='"+name+"'";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:cantdb");
PreparedStatement ps=con.prepareStatement(sql);
ps.executeUpdate();
JOptionPane.showMessageDialog(this,"刪除成功");
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,"查詢失敗");
}
this.dispose();
}
if(command.equals("取消"))
{
this.dispose();
}
}
}
//修改模塊
class amend extends JDialog implements ActionListener
{
private JLabel lnc,ln,lp,la;
private JTextField tfnc,tfn,tfp,tfa;
private JButton btc,bt,btqu;
public amend(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;
lnc=new JLabel("請輸入要修改的商品名稱");
c.add(lnc,gbc);
gbc.gridx=1;
gbc.gridy=0;
gbc.gridwidth=2;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
tfnc=new JTextField(20);
c.add(tfnc,gbc);
gbc.gridx=1;
gbc.gridy=1;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
btc=new JButton("查詢");
c.add(btc,gbc);
btc.addActionListener(this);
gbc.gridx=0;
gbc.gridy=2;
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=2;
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=3;
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=3;
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=4;
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=4;
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=5;
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=5;
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,500,500);
this.show();
}
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals("查詢"))
{
String name="",price="",amount="";
name=tfnc.getText();
String sql="SELECT * FROM t1 WHERE 商品名稱='"+name+"'";
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())
{
name=rs.getString("商品名稱");
amount=rs.getString("庫存數量");
n++;
}
if(n==0)
{
JOptionPane.showMessageDialog(this,"沒有查到相關資料");
}
else
{
tfn.setText(name);
tfp.setText(price);
tfa.setText(amount);
}
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,"查詢失敗");
}
}
if(command.equals("提交"))
{
String name="",price="",amount="",namec="";
namec=tfnc.getText();
name=tfn.getText();
price=tfp.getText();
amount=tfa.getText();
String sql="UPDATE t1 SET 商品名稱='"+name+"',商品價格='"+price+"',庫存數量='"+amount+"'"+"WHERE 商品名稱='"+namec+"'";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:cantdb");
PreparedStatement ps=con.prepareStatement(sql);
ps.executeUpdate();
JOptionPane.showMessageDialog(this,"修改成功");
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,"查詢失敗");
}
this.dispose();
}
if(command.equals("取消"))
{
this.dispose();
}
}
}
//退出模塊
class exit extends JDialog implements ActionListener
{
private JLabel lq;
private JButton bt,btqu;
public exit(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=2;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
lq=new JLabel("你確定要退出系統嗎?");
c.add(lq,gbc);
gbc.gridx=0;
gbc.gridy=1;
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=1;
gbc.gridy=1;
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,200);
this.show();
}
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals("取消"))
{
this.dispose();
}
if(command.equals("確定"))
{
System.exit(0);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -