📄 shopinfo.java
字号:
package classfile;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Vector;
class ShopInfo extends JFrame implements ActionListener{
private int flag=1;
private Vector v = new Vector();
private static JLabel jl1,jl2,jl3,jl4,jl5;
private static JTextField jt1,jt2,jt3,jt4,jt5;
private static JComboBox jco;
private static JButton jb1,jb2,jb3,jb4,jb5;
private static JScrollPane js;
ShopInfo(){
jl1 = new JLabel("商 品 编 号 :");
jl2 = new JLabel("商 品 名 称 :");
jl3 = new JLabel("商 品 价 格 :");
jl4 = new JLabel("商 品 折 扣 :");
jl5 = new JLabel("商 品 数 量 :");
jt1 = new JTextField(10);
InputVerifier iv = new InputVerifier(){
public boolean verify(JComponent input){
JTextField t = (JTextField)input;
String num = t.getText();
try{
Integer.parseInt(num);
}catch(Exception e){
return false;
}
if( num.length() > 6)
return false;
return true;
}
};
jt1.setInputVerifier(iv);
jt2 = new JTextField(20);
jt3 = new JTextField(6);
jt4 = new JTextField(5);
jt5 = new JTextField(5);
jb1 = new JButton(" 上一个 ");
jb2 = new JButton(" 下一个 ");
jb3 = new JButton("清空");
jb4 = new JButton("保存");
jb5 = new JButton("删除");
jco = new JComboBox();
}
ShopInfo(JDesktopPane jd){
try{
if((jd.getAllFrames().length)>=1)
return;
new ShopInfo();
}catch(Exception eee){
System.out.println ("eee");
}
JInternalFrame shopInfo = new JInternalFrame("商品基本信息",false,true,false,true);
Container cs = getContentPane();
cs.setLayout(new FlowLayout());
readData();
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jco.addActionListener(this);
cs.add(jl1);
cs.add(jt1);
cs.add(new JLabel(" 不大于6位的数字"));
cs.add(jl2);
cs.add(jt2);
cs.add(jl3);
cs.add(jt3);
cs.add(new JLabel(" "));
cs.add(jl5);
cs.add(jt5);
cs.add(jl4);
cs.add(jt4);
cs.add(new JLabel(" (请输入0-10之间的折扣数)"));
cs.add(jco);
cs.add(new JLabel(" "));
cs.add(jb1);
cs.add(new JLabel(" "));
cs.add(jb2);
cs.add(jb3);
cs.add(jb4);
cs.add(jb5);
shopInfo.add(cs);
jd.add(shopInfo);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Commonable.screenCenter(this);
shopInfo.setSize(340,220);
shopInfo.setResizable(false);
shopInfo.setVisible(true);
System.out.println (jd.getAllFrames().length);
}
public void actionPerformed(ActionEvent e){
Object o = e.getSource();
if(o instanceof JComboBox){
MakeDatebase conn = new MakeDatebase();
conn.connectionSql();
flag=2;
if(jco.getItemCount()==0){
return;
}
String sql = "select sid,sname,sprice,scount,sdiscount from commodiy_id where sid=" + jco.getSelectedItem();
conn.selectDate(sql);
try {
if(conn.rct.next()){
jt1.setEditable(false);
jt1.setText(conn.rct.getString(1));
jt2.setText(conn.rct.getString(2));
jt3.setText(conn.rct.getString(3));
jt5.setText(conn.rct.getString(4));
jt4.setText(conn.rct.getString(5));
}
else{
clear();
}
}
catch (Exception ex) {
System.out.println ("error");
ex.printStackTrace();
}
conn.closeConnection();
}
if(e.getActionCommand()=="清空"){
flag = 1;
jt1.setEditable(true);
clear();
}
if(e.getActionCommand()==" 上一个 "){
if(jco.getSelectedIndex()>0){
jco.setSelectedIndex(jco.getSelectedIndex()-1);
}
}
if(e.getActionCommand()==" 下一个 "){
if(jco.getSelectedIndex()< jco.getItemCount()-1){
jco.setSelectedIndex(jco.getSelectedIndex()+1);
}
}
if(e.getActionCommand()=="保存"){
try{
Float.parseFloat(jt3.getText());
}
catch(Exception check){
JOptionPane.showMessageDialog(null,"价钱输入有误");
return;
}
try{
Integer.parseInt(jt5.getText());
}
catch(Exception check){
JOptionPane.showMessageDialog(null,"数量输入有误");
return;
}
try{
Float.parseFloat(jt4.getText());
if(Float.parseFloat(jt4.getText())>10)
throw new Exception();
}
catch(Exception check){
JOptionPane.showMessageDialog(null,"折扣为0-10之间的数字");
return;
}
if(flag==1){
if(jt1.getText().length()!=0 && jt2.getText().length()!=0 && jt3.getText().length()!=0 && jt4.getText().length()!=0 && jt5.getText().length()!=0){
try{
Float.parseFloat(jt3.getText());
}
catch(Exception check){
JOptionPane.showMessageDialog(null,"价钱输入有误");
return;
}
try{
Integer.parseInt(jt5.getText());
}
catch(Exception check){
JOptionPane.showMessageDialog(null,"数量输入有误");
return;
}
try{
Float.parseFloat(jt4.getText());
}
catch(Exception check){
JOptionPane.showMessageDialog(null,"折扣为0-10之间的数字");
return;
}
try{
MakeDatebase conn = new MakeDatebase();
conn.connectionSql();
String sql = "insert into commodiy_id values("+ jt1.getText() + ",'" + jt2.getText() + "'," + jt3.getText() + "," + jt5.getText() +",'" +jt4.getText() + "')";
System.out.println (sql);
conn.updateDate(sql);
if(MakeDatebase.dataCleck){
JOptionPane.showMessageDialog(null,"添加成功");
jt1.setEditable(false);
}
conn.closeConnection();
}catch(Exception ee){
System.out.println ("添加失败");
}
readData();
}
else{
JOptionPane.showMessageDialog(null,"数据不完整"); }
}
else{
MakeDatebase conn = new MakeDatebase();
conn.connectionSql();
String sql = "update commodiy_id set sname='" + jt2.getText() + "',sprice=" + jt3.getText() + ",scount="+ jt5.getText() + ",sdiscount='" +jt4.getText() + "' where sid=" + jt1.getText();
System.out.println (sql);
conn.updateDate(sql);
JOptionPane.showMessageDialog(null,"修改成功");
jt1.setEditable(false);
conn.closeConnection();
}
}
if(e.getActionCommand()=="删除"){
MakeDatebase conn = new MakeDatebase();
conn.connectionSql();
String sql = "delete from commodiy_id where sid=" + jt1.getText();
int flag = JOptionPane.showConfirmDialog(null,"确定删除吗?","delete",JOptionPane.YES_NO_OPTION);
if(flag == JOptionPane.YES_OPTION){
try{
conn.updateDate(sql);
}
catch(Exception ex1){
System.out.println ("error");
}
clear();
readData();
}
conn.closeConnection();
}
}
public void ifLoad(String id,String name,String price,String count,String discount){
try{
Class.forName("ShopInfo");
}
catch(Exception e){
System.out.println ("初使化失败");
}
try{
flag = 2;
new ShopInfo().readData();
//JFrame jframe = new JFrame();
JDialog jframe = new JDialog(this,"商品基本信息",true);
JPanel jpane = new JPanel();
jt1.setEditable(false);
jt1.setText(id);
jt2.setText(name);
jt3.setText(price);
jt4.setText(discount);
jt5.setText(count);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jco.addActionListener(this);
jpane.add(jl1);jpane.add(jt1);jpane.add(new JLabel(" 不大于6位的数字"));
jpane.add(jl2);jpane.add(jt2);
jpane.add(jl3);jpane.add(jt3);jpane.add(new JLabel(" "));
jpane.add(jl5);jpane.add(jt5);
jpane.add(jl4);jpane.add(jt4);jpane.add(new JLabel(" (请输入0-10之间的折扣数)"));
jpane.add(jco);
jpane.add(jb1);
jpane.add(new JLabel(" "));
jpane.add(jb2);
jpane.add(jb3);
jpane.add(jb4);
jpane.add(jb5);
jframe.add(jpane);
jframe.setSize(340,220);
Commonable.screenCenter(jframe);
jframe.setVisible(true);
}
catch(Exception e){
System.out.println ("加载失败");
}
}
private void readData(){
jco.removeAllItems();
MakeDatebase conn = new MakeDatebase();
conn.connectionSql();
String sql = "select sid from commodiy_id";
conn.selectDate(sql);
try {
v.removeAllElements();
while(conn.rct.next()){
v.add(conn.rct.getString(1));
}
jco.setToolTipText("请选择商品");
for(int i=0;i<v.size(); i++){
jco.addItem(v.get(i).toString());
}
}
catch (Exception ex) {
ex.printStackTrace();
}finally{
conn.closeConnection();
}
}
private void clear(){
jt1.setText("");
jt2.setText("");
jt3.setText("");
jt4.setText("");
jt5.setText("");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -