Java花卉管理系统 Java实现花卉管理系统

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java花卉管理系统 Java实现花卉管理系统

华墨1024   2021-06-24 我要评论
想了解Java实现花卉管理系统的相关内容吗,华墨1024在本文为您仔细讲解Java花卉管理系统的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java花卉管理系统,java花卉管理,java管理系统,下面大家一起来学习吧。

这是一个稍微综合一点的小小小项目,算是给Java的学习画一个小结点。

直接开门见山来看设计吧。

1.上传的这个版本是通过文本存储数据(PS:别问我为啥不用数据库,因为当时事情多.....懒得实现)

2.这个版本有欢迎界面、增加花卉界面、删除花卉界面、修改花卉信息界面、查询花卉界面。

3.对文本的增删查改过程全部定义在独立的类的方法中,通过对类中方法的调用来实现。

以下是代码的文件表:

在这个程序中的绝大多数提示我用的都是具有鲜明时代特色的表情包,哈哈哈哈哈哈

先让你们小小欣赏下(哈哈哈哈哈,前方小高能,非战斗人员请尽快撤退!!):

1. 当有未填信息时

2. 当编号中包含非数字符时(单价非数字时等提示图片一样)

3.当输入正确时

主函数代码(Test.java):

public class Test {
      /**
       * @param args
       */
      public static void main(String[] args) {
            Enter enter = new Enter();
            //AddFlower add = new AddFlower();
            //SearchFlower s = new SearchFlower();
            //DeleteFlower d = new DeleteFlower();
            //ModifyFlower m = new ModifyFlower();
      }
}

欢迎界面(Enter.java):

import javax.swing.*;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.*;
 
 
public class Enter extends JFrame implements ActionListener
{
 JLabel PhotoLabel = new JLabel(new ImageIcon("Flower.jpg"));
 JButton loginbt;//登录进入按钮
 JLabel InformationJLabel;//欢迎语
 
 public Enter()
 {
  this.setSize(500, 500);
  this.setTitle("花卉信息管理系统");
  this.setLocation(700,200);
  this.setVisible(true);
  this.setLayout(null);
  //设置照片为背景
  String Background = "Flower.jpg";
  ImageIcon background = new ImageIcon(Background);
  JLabel label = new JLabel(background);
  label.setBounds(0, 0,this.getWidth() , this.getHeight());
  JPanel imagePanel = (JPanel) this.getContentPane();
  imagePanel.setOpaque(false);
  this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.init();
 }
 
 private void init()
 {
  loginbt=new JButton("登录");
  loginbt.setSize(100,50);
  loginbt.setLocation(180, 330);
  loginbt.addActionListener(this);
  this.add(loginbt);
  InformationJLabel = new JLabel("欢迎使用!");
  InformationJLabel.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 50));
  InformationJLabel.setSize(300, 50);
  InformationJLabel.setLocation(120,150);
  this.add(InformationJLabel);
  
 }
 
 @Override
 public void actionPerformed(ActionEvent e) {
 AddFlower addflower = new AddFlower(); 
  
 }
 
}

增加花卉界面(AddFlower.java): 

import javax.swing.*;
import javax.swing.event.AncestorListener;
 
import java.awt.Font;
import java.awt.event.*;
 
public class AddFlower extends JFrame implements ActionListener
{
 JLabel tableLabel;
 
 JLabel NumLabel;//编号
 JLabel NameLabel;//名称
 JLabel PriceLabel;//单价
 JLabel UnitLabel;//计价单位
 
 JTextField NumText;//编号文本框
 JTextField NameText;//名称文本框
 JTextField PriceText;//单价文本框
 JTextField UnitText;//计价单位文本框
 
 JButton[] rbts;//按钮数组对象
 ButtonGroup btGroup;//按钮组,从逻辑上来涵盖所有的单选按钮
 JButton Addbt;//增加按钮
 
 public AddFlower()
 {
  this.setSize(500, 500);
  this.setTitle("花卉信息管理系统");
  this.setLocation(700,200);
  this.setVisible(true);
  this.setLayout(null);
  this.init();
 }
 
 private void init()
 {
  //增删查改功能入口按钮
  btGroup=new ButtonGroup();
  rbts=new JButton[4];
  String[] strs = {"增加","删除","搜索","修改"};
  for(int i = 0;i<strs.length;i++)
  {
   rbts[i]=new JButton(strs[i]);
   rbts[i].setSize(70, 30);
   rbts[i].setLocation(100*i+60, 30);
   btGroup.add(rbts[i]);//从逻辑上将单选按钮添加到一个按钮数组中
   this.add(rbts[i]);//从物理位置上将单选按钮添加到窗体中
   rbts[i].addActionListener(this);
  }
  
  //编号
  NumLabel=new JLabel("编号");
  NumLabel.setFont(NumLabel.getFont().deriveFont(Font.BOLD, 18));
  NumLabel.setSize(90,30);
  NumLabel.setLocation(70, 110);
  this.add(NumLabel);
  NumText=new JTextField();
  NumText.setSize(180,30);
  NumText.setLocation(170, 110);
  this.add(NumText);
  
  //名称
  NameLabel=new JLabel("名称");
  NameLabel.setFont(NameLabel.getFont().deriveFont(Font.BOLD, 18));
  NameLabel.setSize(90,30);
  NameLabel.setLocation(70, 170);
  this.add(NameLabel);
  NameText=new JTextField();
  NameText.setSize(180,30);
  NameText.setLocation(170, 170);
  this.add(NameText);
  
  //单价
  PriceLabel=new JLabel("单价");
  PriceLabel.setFont(PriceLabel.getFont().deriveFont(Font.BOLD, 18));
  PriceLabel.setSize(90,30);
  PriceLabel.setLocation(70, 230);
  this.add(PriceLabel);
  PriceText=new JTextField();
  PriceText.setSize(180,30);
  PriceText.setLocation(170, 230);
  this.add(PriceText);
 
  //计价单位
  UnitLabel=new JLabel("计价单位");
  UnitLabel.setFont(UnitLabel.getFont().deriveFont(Font.BOLD, 18));
  UnitLabel.setSize(90,30);
  UnitLabel.setLocation(70, 290);
  this.add(UnitLabel);
  UnitText=new JTextField();
  UnitText.setSize(180,30);
  UnitText.setLocation(170, 290);
  this.add(UnitText);
  
  //添加按钮
  Addbt=new JButton("添加"); 
  Addbt.setSize(90,50);
  Addbt.setLocation(200, 360);
  Addbt.addActionListener(
    new ActionListener()
    {
     public void actionPerformed(ActionEvent e)
     {
      
       if(!checkIsNull())
           {
         final ImageIcon icon3 = new ImageIcon("question.png");
        JOptionPane.showMessageDialog(null,"有未填信息?", "提示", JOptionPane.INFORMATION_MESSAGE, icon3);
           }else
           {
            if(!checkInput().equals(""))
            {
             final ImageIcon icon2 = new ImageIcon("NotOk.png");
          JOptionPane.showMessageDialog(null,checkInput(), "提示", JOptionPane.INFORMATION_MESSAGE, icon2);
            }else
            {
             FlowerDAO dao = new FlowerDAO();
             //引用添加花朵信息的方法
             StringBuffer FloInfo=new StringBuffer();
             FloInfo.append(NumText.getText().trim()+",");
             FloInfo.append(NameText.getText().trim()+",");
             FloInfo.append(PriceText.getText().trim()+",");
             FloInfo.append(UnitText.getText().trim());
           // dao.AddFlower(FloInfo);
             if(dao.AddFlower(FloInfo.toString()))
         {
              final ImageIcon icon = new ImageIcon("Ok2.png");
          JOptionPane.showMessageDialog(null,"添加成功", "提示", JOptionPane.INFORMATION_MESSAGE, icon);
          NumText.setText("");
          NameText.setText("");
          PriceText.setText("");
          UnitText.setText("");
         }
         else
         {
          final ImageIcon icon1 = new ImageIcon("NotOk2.png");
          JOptionPane.showMessageDialog(null,"信息重复", "提示", JOptionPane.INFORMATION_MESSAGE, icon1);
         }
            }
           }
     }
    }
  );
  this.add(Addbt);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
   String buttonName = e.getActionCommand();
      if(buttonName.equals("增加")){
       System.out.println("已处在增加界面上");
      }else if(buttonName.equals("删除")){
       DeleteFlower deleteflower = new DeleteFlower();
      }else if(buttonName.equals("搜索")){
       SearchFlower searchflower = new SearchFlower();
      }else{
       ModifyFlower modifyflower = new ModifyFlower();
      }
     
 }
 
 private boolean checkIsNull() 
 {
  boolean flag=true;
  if(NumText.getText().trim().equals(""))
  {
   flag = false;
  }else if(NameText.getText().trim().equals(""))
  {
   flag=false;
  }else if(PriceText.getText().trim().equals(""))
  {
   flag=false;
  }else if(UnitText.getText().trim().equals(""))
  {
   flag=false;
  }
  return flag;
 }
 
 private Object checkInput() 
 { 
  String result="";
  String result1="";
   for(int i=0;i<NumText.getText().trim().length();i++)
   {
    char ch=NumText.getText().trim().charAt(i);
    if((ch<'0')||(ch>'9'))
    {
     result="编号中包含非数字字符";
     break;
    }
   }
   for(int i=0;i<PriceText.getText().trim().length();i++)
   {
    char ch=PriceText.getText().trim().charAt(i);
    if((ch<'0')||(ch>'9'))
    {
     result1="单价中包含非数字字符";
     break;
    }
   }
  if(result=="编号中包含非数字字符" && result1=="")
  {
   return result;
  }else if(result1=="单价中包含非数字字符" && result=="")
  {
   return result1;
  }else if(result=="编号中包含非数字字符" && result1=="单价中包含非数字字符")
  {
   return "编号和单价中均包含非数字字符";
  }else
  {
   return "";
  }
  
 }
 
 
}

删除花卉界面(DeleteFlower.java):

import javax.swing.*;
 
import java.awt.Font;
import java.awt.event.*;
import java.util.ArrayList;
 
public class DeleteFlower extends JFrame implements ActionListener
{
 JLabel tableLabel;//标题
 JScrollPane tablePane;//滚动视口
 JTable table;//花卉列表
 JLabel Labelnum;//删除提示
 
 JTextField DeleteText;//删除文本框
 JButton Deletebt;//删除按钮
 JButton[] rbts;//按钮数组对象
 ButtonGroup btGroup;//按钮组,从逻辑上来涵盖所有的单选按钮
 
 
 public DeleteFlower()
 {
  this.setLayout(null);
  this.setSize(700, 580);
  this.setTitle("花卉信息管理系统");
  this.setLocation(600,250);
  this.init();
  this.refreshTable();
  this.setVisible(true);
  new Thread().start();
 }
 private void init()
 {
  //增删查改功能入口按钮
  btGroup=new ButtonGroup();
  rbts=new JButton[4];
  String[] strs = {"增加","删除","搜索","修改"};
  for(int i = 0;i<strs.length;i++)
  {
   rbts[i]=new JButton(strs[i]);
   rbts[i].setSize(70, 30);
   rbts[i].setLocation(150*i+80, 10);
   btGroup.add(rbts[i]);//从逻辑上将单选按钮添加到一个按钮数组中
   this.add(rbts[i]);//从物理位置上将单选按钮添加到窗体中
   rbts[i].addActionListener(this);
  }
  
  Labelnum = new JLabel("编号");
  Labelnum.setFont(Labelnum.getFont().deriveFont(Font.BOLD, 16));
  Labelnum.setSize(100,30); 
  Labelnum.setLocation(100,70);
  this.add(Labelnum);
  
  DeleteText = new JTextField();
  DeleteText.setSize(180,30);
  DeleteText.setLocation(170, 70);
  this.add(DeleteText);
  
  Deletebt = new JButton("删除");
  Deletebt.setSize(70, 30);
  Deletebt.setLocation(380, 70);
  Deletebt.addActionListener(new ActionListener()
  {
   @Override
   public void actionPerformed(ActionEvent e) {
    int str=JOptionPane.showConfirmDialog(null, "是否删除?", "警告", JOptionPane.YES_NO_OPTION); 
    if(str==JOptionPane.YES_OPTION)
    {
     //System.out.println("进入");
     if(checkIsNull()){
      if(FlowerDAO.DeleteFlower(DeleteText.getText().trim()))
      {
       final ImageIcon icon = new ImageIcon("Ok.png");
       JOptionPane.showMessageDialog(null,"删除成功", "提示", JOptionPane.INFORMATION_MESSAGE, icon);
       DeleteText.setText("");
       refreshTable();
      }
      else
      {
       JOptionPane.showMessageDialog(null, "删除失败", "提示", JOptionPane.ERROR_MESSAGE);
      }
     }else{
       final ImageIcon icon1 = new ImageIcon("黑人问号3.png");
       JOptionPane.showMessageDialog(null,"未输入删除信息", "提示", JOptionPane.INFORMATION_MESSAGE, icon1);
     }
    }else{
     ;
    }
     
   }
  }
     
  );
  this.add(Deletebt); 
  
  
 }
 
 private void refreshTable()
 {
  
  tableLabel = new JLabel("花卉列表"); 
  tableLabel.setBounds(300,105, 100, 50);
  tableLabel.setFont(new Font("宋体", Font.BOLD , 20));
  this.add(tableLabel);
  String[] columnNames = {"编号","名称","单价","计价单位"};
  FlowerDAO dao=new FlowerDAO();
  ArrayList<String> flos=dao.findAllFlowers();
  Object[][] objs=new Object[flos.size()][4];
  for(int i=0;i<flos.size();i++)
  {
   String[] strs1=flos.get(i).split(",");
   objs[i][0]=strs1[0];
   objs[i][1]=strs1[1];
   objs[i][2]=strs1[2];
   objs[i][3]=strs1[3];
   System.out.println(strs1[0]);
  }
  
  table=new JTable(objs,columnNames);
  table.setSize(380,150);
  tablePane = new JScrollPane(table);
  tablePane.setSize(660, 350);
  tablePane.setLocation(10, 150);
  table.getTableHeader().setReorderingAllowed(false);
  table.getTableHeader().setResizingAllowed(false);
  table.enable(false);
  
  this.add(tablePane);
 }
 
 public void run() {
  while (true) {
   //每隔1秒钟更新JTable
   table.validate();
   table.updateUI();
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
 
 @Override
 public void actionPerformed(ActionEvent e) {
   String buttonName = e.getActionCommand();
       if(buttonName.equals("增加")){
        AddFlower addflower = new AddFlower(); 
       }else if(buttonName.equals("删除")){
        System.out.println("已处在删除界面上");
       }else if(buttonName.equals("搜索")){
        SearchFlower searchflower = new SearchFlower();
       }else{
        ModifyFlower modifyflower = new ModifyFlower();
       }
  
 }
 
 private boolean checkIsNull() 
 {
  boolean flag=true;
  if(DeleteText.getText().trim().equals(""))
  {
   flag = false;
  }
  return flag;
 }
 
 private Object checkInput() 
 { 
  String result="";
  String result1="";
   for(int i=0;i<DeleteText.getText().trim().length();i++)
   {
    char ch=DeleteText.getText().trim().charAt(i);
    if((ch<'0')||(ch>'9'))
    {
     result="编号中包含非数字字符";
     break;
    }else
    {
     result="";
     break;
    }
   }
   return result;
  
 }
 
 
}

搜索花卉界面(SearchFlower.java):

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
 
 
 
 
public class SearchFlower extends JFrame implements ActionListener
{
    
    JLabel tableLabel;//标题
    JScrollPane tablePane;//滚动视口
    JTable table;//花卉列表
    
    JLabel NumLabel;//编号标题
    JLabel NameLabel;//名称标题
    JLabel PriceLabel;//单价标题
    JLabel UnitLabel;//计价单位标题
    JPanel panel;
    JComboBox comboBox;//组合框
    JTextField SearchText;//搜索文本框
    JButton SearchButton;//搜索按键
    /*
    JTextField NumText;//编号文本框
    JTextField NameText;//名称文本框
    JTextField PriceText;//单价文本框
    JTextField UnitText;//计价单位文本框
    */
    JButton[] rbts;//按钮数组对象
    ButtonGroup btGroup;//按钮组,从逻辑上来涵盖所有的单选按钮
    
    public SearchFlower()
    {
        this.setLayout(null);
        this.setSize(700, 580);
        this.setTitle("花卉信息管理系统");
        this.setLocation(600,250);
        this.setBackground(Color.lightGray);
        this.init();
        this.refreshTable();
        this.setVisible(true);
    
        
    }
    private void init()
    {
        //增删查改功能入口按钮
        btGroup=new ButtonGroup();
        rbts=new JButton[4];
        String[] strs = {"增加","删除","搜索","修改"};
        for(int i = 0;i<strs.length;i++)
        {
            rbts[i]=new JButton(strs[i]);
            rbts[i].setSize(70, 30);
            rbts[i].setLocation(150*i+80, 10);
            btGroup.add(rbts[i]);//从逻辑上将单选按钮添加到一个按钮数组中
            this.add(rbts[i]);//从物理位置上将单选按钮添加到窗体中
            rbts[i].addActionListener(this);
        }
        
        
        String[] string = { "搜索条件", "编号", "名称" };
        comboBox = new JComboBox(string);
        comboBox.setSize(100,30);
        comboBox.setLocation(50,70);
        this.add(comboBox);
        
        SearchText = new JTextField();
        SearchText.setSize(180,30);
        SearchText.setLocation(170, 70);
        this.add(SearchText);
        
        SearchButton = new JButton("搜索");
        SearchButton.setSize(70, 30);
        SearchButton.setLocation(380, 70);
        SearchButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e) {
                String item = (String)comboBox.getSelectedItem();
                ArrayList<String> result=new ArrayList<String>();
                if("搜索条件".equals(item))
                {
                    SearchText.setText("");
                }else if("编号".equals(item)){
                    //根据编号进行查找的方法
                    if(checkIsNull()){
                        result.add(FlowerDAO.findFlowerBySno(SearchText.getText().trim()));
                        JOptionPane.showMessageDialog(null, result.clone());
                    }else{
                        JOptionPane.showMessageDialog(null, "未输入查询信息","提示", JOptionPane.ERROR_MESSAGE);
                    }
                    
                }else if("名称".equals(item)){
                    //根据名称进行查找的方法
                    if(checkIsNull())
                    {
                        result.add(FlowerDAO.findFlowerBySname(SearchText.getText().trim()));
                        JOptionPane.showMessageDialog(null, result.clone());
                    }else{
                        JOptionPane.showMessageDialog(null, "未输入查询信息", "提示", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }
                    
        );
        this.add(SearchButton);    
    }
    
    private void refreshTable()
    {
        tableLabel = new JLabel("花卉列表");    
        tableLabel.setBounds(300,105, 100, 50);
        tableLabel.setFont(new Font("宋体", Font.BOLD , 20));
        
        String[] columnNames = {"编号","名称","单价","计价单位"};
        FlowerDAO dao=new FlowerDAO();
        ArrayList<String> flos=dao.findAllFlowers();
        Object[][] objs=new Object[flos.size()][4];
        for(int i=0;i<flos.size();i++)
        {
            String[] strs1=flos.get(i).split(",");
            objs[i][0]=strs1[0];
            objs[i][1]=strs1[1];
            objs[i][2]=strs1[2];
            objs[i][3]=strs1[3];
            System.out.println(strs1[0]);
        }
        
        table=new JTable(objs,columnNames);
        table.setSize(380,150);
        tablePane = new JScrollPane(table);
        tablePane.setSize(660, 350);
        tablePane.setLocation(10, 150);
        table.getTableHeader().setReorderingAllowed(false);
        table.getTableHeader().setResizingAllowed(false);
        table.enable(false);
        
        //this.removeAll();
        //this.init();
        this.add(tableLabel);
        this.add(tablePane);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
         String buttonName = e.getActionCommand();
          if(buttonName.equals("增加")){
              AddFlower addflower = new AddFlower();
          }else if(buttonName.equals("删除")){
              DeleteFlower deleteflower = new DeleteFlower();
          }else if(buttonName.equals("搜索")){
              System.out.println("已处在搜索界面上");
          }else{
              ModifyFlower modifyflower = new ModifyFlower();
          }
        
    }
    
    private boolean checkIsNull()
    {
        boolean flag=true;
        if(SearchText.getText().trim().equals(""))
        {
            flag = false;
        }
        return flag;
    }
    
    private Object checkInput()
    {    
        String result="";
        String result1="";
            for(int i=0;i<SearchText.getText().trim().length();i++)
            {
                char ch=SearchText.getText().trim().charAt(i);
                if((ch<'0')||(ch>'9'))
                {
                    result="编号中包含非数字字符";
                    break;
                }else
                {
                    result="";
                    break;
                }
            }
            return result;
        
    }
 
 
}

修改花卉界面(ModifyFlower.java):

import javax.swing.*;
 
import java.awt.Font;
import java.awt.event.*;
import java.util.ArrayList;
 
public class ModifyFlower extends JFrame implements ActionListener
{
 JButton[] rbts;//按钮数组对象
 ButtonGroup btGroup;//按钮组,从逻辑上来涵盖所有的单选按钮
 
 JLabel tableLabel;//标题
 JScrollPane tablePane;//滚动视口
 JTable table;//花卉列表
 
 JLabel NumLabel;//编号
 JLabel NameLabel;//名称
 JLabel PriceLabel;//单价
 JLabel UnitLabel;//计价单位
 
 JTextField NumText;//编号文本框
 JTextField NameText;//名称文本框
 JTextField PriceText;//单价文本框
 JTextField UnitText;//计价单位文本框
 
 JButton Modifybt;//修改按钮
 public ModifyFlower()
 {
  this.setLayout(null);
  this.setSize(700, 580);
  this.setTitle("花卉信息管理系统");
  this.setLocation(600,250);
  this.init();
  this.refreshTable();
  this.setVisible(true);
 }
 private void init()
 {
  //增删查改功能入口按钮
  btGroup=new ButtonGroup();
  rbts=new JButton[4];
  String[] strs = {"增加","删除","搜索","修改"};
  for(int i = 0;i<strs.length;i++)
  {
   rbts[i]=new JButton(strs[i]);
   rbts[i].setSize(70, 30);
   rbts[i].setLocation(150*i+80, 10);
   btGroup.add(rbts[i]);//从逻辑上将单选按钮添加到一个按钮数组中
   this.add(rbts[i]);//从物理位置上将单选按钮添加到窗体中
   rbts[i].addActionListener(this);
  }
  
  //编号
  NumLabel=new JLabel("编号");
  NumLabel.setFont(NumLabel.getFont().deriveFont(Font.BOLD, 16));
  NumLabel.setSize(80,30);
  NumLabel.setLocation(80, 60);
  this.add(NumLabel);
  NumText=new JTextField();
  NumText.setSize(100,30);
  NumText.setLocation(140, 60);
  this.add(NumText);
  
  //名称
  NameLabel=new JLabel("名称");
  NameLabel.setFont(NameLabel.getFont().deriveFont(Font.BOLD, 16));
  NameLabel.setSize(80,30);
  NameLabel.setLocation(300,60);
  this.add(NameLabel);
  NameText=new JTextField();
  NameText.setSize(100,30);
  NameText.setLocation(380, 60);
  this.add(NameText);
  
  //单价
  PriceLabel=new JLabel("单价");
  PriceLabel.setFont(PriceLabel.getFont().deriveFont(Font.BOLD, 16));
  PriceLabel.setSize(80,30);
  PriceLabel.setLocation(80, 100);
  this.add(PriceLabel);
  PriceText=new JTextField();
  PriceText.setSize(100,30);
  PriceText.setLocation(140, 100);
  this.add(PriceText);
 
  //计价单位
  UnitLabel=new JLabel("计价单位");
  UnitLabel.setFont(UnitLabel.getFont().deriveFont(Font.BOLD, 16));
  UnitLabel.setSize(80,30);
  UnitLabel.setLocation(300, 100);
  this.add(UnitLabel);
  UnitText=new JTextField();
  UnitText.setSize(100,30);
  UnitText.setLocation(380, 100);
  this.add(UnitText);
  
  Modifybt = new JButton("修改");
  Modifybt.setSize(70, 30);
  Modifybt.setLocation(530, 100);
  Modifybt.addActionListener(new ActionListener()
  {
   @Override
   public void actionPerformed(ActionEvent e) {
    if(!checkIsNull())//如果用户输入为空的话
    {
      final ImageIcon icon = new ImageIcon("黑人问号.png");
      JOptionPane.showMessageDialog(null,"输入信息为空!", "提示", JOptionPane.INFORMATION_MESSAGE, icon);   }
    else
    {
     if(!checkInput().equals(""))
     {
      JOptionPane.showMessageDialog(null,checkInput());
     }
     else
     {
      FlowerDAO dao=new FlowerDAO();
      //创建一个操作学生信息文本文件的对象
      StringBuffer floInfo=new StringBuffer();
      floInfo.append(NumText.getText().trim()+",");
      floInfo.append(NameText.getText().trim()+",");
      floInfo.append(PriceText.getText().trim()+",");
      floInfo.append(UnitText.getText().trim());
      if(dao.ModifyFlower(floInfo.toString()))
      {
       final ImageIcon icon2 = new ImageIcon("OK2.png");
       JOptionPane.showMessageDialog(null,"修改成功", "提示", JOptionPane.INFORMATION_MESSAGE, icon2);
       NumText.setText("");
       NameText.setText("");
       PriceText.setText("");
       UnitText.setText("");
       refreshTable();
       //添加成功后刷新表格
      }
      else
      {
       JOptionPane.showMessageDialog(null,"修改记录失败");
      }
     }
    }
  }
  }  
  );
  this.add(Modifybt);
  
 }
 
 //表格
 private void refreshTable()
 {
  tableLabel = new JLabel("花卉列表"); 
  tableLabel.setBounds(300,140, 100, 50);
  tableLabel.setFont(new Font("宋体", Font.BOLD , 20));
  
  String[] columnNames = {"编号","名称","单价","计价单位"};
  FlowerDAO dao=new FlowerDAO();
  ArrayList<String> flos=dao.findAllFlowers();
  Object[][] objs=new Object[flos.size()][4];
  for(int i=0;i<flos.size();i++)
  {
   String[] strs1=flos.get(i).split(",");
   objs[i][0]=strs1[0];
   objs[i][1]=strs1[1];
   objs[i][2]=strs1[2];
   objs[i][3]=strs1[3];
   System.out.println(strs1[0]);
  }
  
  table=new JTable(objs,columnNames);
  table.setSize(380,150);
  tablePane = new JScrollPane(table);
  tablePane.setSize(660, 320);
  tablePane.setLocation(10, 190);
  table.getTableHeader().setReorderingAllowed(false);
  table.getTableHeader().setResizingAllowed(false);
  table.enable(false);
  
  this.add(tableLabel);
  this.add(tablePane);
 }
 
 //刷新表格
 public void run() {
  while (true) {
   //每隔1秒钟更新JTable
   table.validate();
   table.updateUI();
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
 
 @Override
 public void actionPerformed(ActionEvent e) {
   String buttonName = e.getActionCommand();
       if(buttonName.equals("增加")){
        AddFlower addflower = new AddFlower(); 
       }else if(buttonName.equals("删除")){
        DeleteFlower deleteflower = new DeleteFlower();
       }else if(buttonName.equals("搜索")){
        SearchFlower searchflower = new SearchFlower();
       }else{
        System.out.println("已处在修改界面上");
       }
  
 }
 
 private boolean checkIsNull() 
 {
  boolean flag=true;
  if(NumText.getText().trim().equals(""))
  {
   flag = false;
  }else if(NameText.getText().trim().equals(""))
  {
   flag=false;
  }else if(PriceText.getText().trim().equals(""))
  {
   flag=false;
  }else if(UnitText.getText().trim().equals(""))
  {
   flag=false;
  }
  return flag;
 }
 
 private Object checkInput() 
 { 
  String result="";
  String result1="";
   for(int i=0;i<NumText.getText().trim().length();i++)
   {
    char ch=NumText.getText().trim().charAt(i);
    if((ch<'0')||(ch>'9'))
    {
     result="编号中包含非数字字符";
     break;
    }
   }
   for(int i=0;i<PriceText.getText().trim().length();i++)
   {
    char ch=PriceText.getText().trim().charAt(i);
    if((ch<'0')||(ch>'9'))
    {
     result1="单价中包含非数字字符";
     break;
    }
   }
  if(result=="编号中包含非数字字符" && result1=="")
  {
   return result;
  }else if(result1=="单价中包含非数字字符" && result=="")
  {
   return result1;
  }else if(result=="编号中包含非数字字符" && result1=="单价中包含非数字字符")
  {
   return "编号和单价中均包含非数字字符";
  }else
  {
   return "";
  }
  
 }
 
}

增删查改方法实现(FlowerDAO.java):

import java.io.*;
import java.util.ArrayList;
 
public class FlowerDAO {
 //查找所有的花卉信息
 public ArrayList<String> findAllFlowers()
 {
  ArrayList<String> result=new ArrayList<String>();
  try {
   FileReader fr=new FileReader("Flower.txt");
   //产生了一个文件字符输入流对象
   BufferedReader br=new BufferedReader(fr);
   //以上一个对象作为参数来创建一个缓冲的字符输入流对象
   String str=br.readLine();//读取一行数据
   while(str!=null)
   {
    result.add(str);
    str=br.readLine();
   }
   br.close();
   fr.close();
  }
  catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   System.out.println("文件未找到");
  }
  catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("文件读错误");
  }
  return result;
 }
 
 //通过编号来查找花朵的基本信息
 public static String findFlowerBySno(String sno)
 {
  String result="不存在这个花朵";
  try {
   FileReader fr=new FileReader("Flower.txt");
   //产生了一个文件字符输入流对象
   BufferedReader br=new BufferedReader(fr);
   //以上一个对象作为参数来创建一个缓冲的字符输入流对象
   String str=br.readLine();//读取一行数据
   boolean flag=true;
   while(flag)
   {
    if(str.startsWith(sno.trim()))
    {
     result=str;
     flag=false;
    }
    str=br.readLine();//继续读取下一行
    if(str==null)
    {
     flag=false;
    }
   }
   br.close();
   fr.close();
  }
  catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   System.out.println("文件未找到");
  }
  catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("文件读错误");
  }
  return result;
 }
 
 
 //通过名称来查找花朵的基本信息
 public static String findFlowerBySname(String sname)
 {
  String result="不存在这个花朵";
  try {
   FileReader fr=new FileReader("Flower.txt");
   //产生了一个文件字符输入流对象
   BufferedReader br=new BufferedReader(fr);
   //以上一个对象作为参数来创建一个缓冲的字符输入流对象
   String str=br.readLine();//读取一行数据
   boolean flag=true;
   while(flag)
   {
    if(str.substring(4).startsWith(sname.trim()))
    {
     result=str;
     flag=false;
    }
    str=br.readLine();//继续读取下一行
    if(str==null)
    {
     flag=false;
    }
   }
   br.close();
   fr.close();
  }
  catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   System.out.println("文件未找到");
  }
  catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("文件读错误");
  }
  return result;
 }
 
 //通过名称来查找花朵的基本信息
 public String findFlowerByname(boolean bool,String name)
 {
  String result="不存在这个花朵";
  try {
   FileReader fr=new FileReader("Flower.txt");
   //产生了一个文件字符输入流对象
   BufferedReader br=new BufferedReader(fr);
   //以上一个对象作为参数来创建一个缓冲的字符输入流对象
   String str=br.readLine();//读取一行数据
   boolean flag=true;
   while(flag)
   {
    String temp=str;
    if(!bool)
    {
     temp=str.substring(4);
    }
    if(temp.startsWith(name.trim()))
    {
     result=str;
     flag=false;
    }
    str=br.readLine();//继续读取下一行
    if(str==null)
    {
     flag=false;
    }
   }
   br.close();
   fr.close();
  }
  catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   System.out.println("文件未找到");
  }
  catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("文件读错误");
  }
  return result;
 }
 
 //增加一条花朵记录
 public boolean AddFlower(String Floinfo)
 {
  String[] strs=Floinfo.split(",");
  if(!numIsExist(strs[0]) && !nameIsExist(strs[1]))
  {
  try {
   FileWriter fw=new FileWriter("Flower.txt",true);
   //创建一个文件的字符输出流,第二个参数表示是在文件尾部进行追加
   BufferedWriter bw=new BufferedWriter(fw);
   //以上面创建的对象为参数创建一个缓冲字符输出流
   bw.newLine();
   //创建新的一行
   bw.write(Floinfo);
   bw.close();
   fw.close(); 
  }
  catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   System.out.println("文件未找到");
  }
  catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("文件写错误");
  }
  return true;
  }
  else
  {
   return false;
  }
 }
 
 //判断是否已经存在重复的编号
 private boolean numIsExist(String num)
 {
  boolean result=false;
  try {
   FileReader fr=new FileReader("Flower.txt");
   //产生了一个文件字符输入流对象
   BufferedReader br=new BufferedReader(fr);
   //以上一个对象作为参数来创建一个缓冲的字符输入流对象
   String str=br.readLine();//读取一行数据
   while(str!=null)
   {
    if(str.startsWith(num))
    {
     result=true;
     break;
    }
    str=br.readLine();
   }
   
   br.close();
   fr.close();
  }
  catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   System.out.println("文件未找到");
  }
  catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("文件读错误");
  }
  return result;
 }
 
 //判断名称是否重复
 private boolean nameIsExist(String name)
 {
  boolean result=false;
  try {
   FileReader fr=new FileReader("Flower.txt");
   //产生了一个文件字符输入流对象
   BufferedReader br=new BufferedReader(fr);
   //以上一个对象作为参数来创建一个缓冲的字符输入流对象
   String str=br.readLine();//读取一行数据
   while(str!=null)
   {
    if(str.startsWith(name))
    {
     result=true;
     break;
    }
    str=br.readLine();
   }
   
   br.close();
   fr.close();
  }
  catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   System.out.println("文件未找到");
  }
  catch (IOException e) {
   // TODO Auto-generated catch block
   System.out.println("文件读错误");
  }
  return result;
 }
 
 //增加多条花朵记录
 public int AddFlowers(String[] Flos)
 {
  int result=0;
  //定义一个变量来保存添加成功的花卉记录个数
  if(Flos!=null)
  {
   if(Flos.length>0)
   {
    for(int i=0;i<Flos.length;i++)
    {
     if(AddFlower(Flos[i]))
     {
      result++;
     }
    }
   }
  }
  return result;
 }
 
 //删除一条花朵记录的方法
 public static boolean DeleteFlower(String num)
 {
  boolean result=false;
  //保存删除是否成功的变量
  try
  {
   FileReader fr=new FileReader("Flower.txt");
   BufferedReader br=new BufferedReader(fr);
   String str=br.readLine();//读取一行数据
   ArrayList<String> list=new ArrayList<String>();
   //保存读入的花朵信息
   boolean flag=true;
   while(flag)
   {
    list.add(str);
    str=br.readLine();//再次读取一行数据
    if(str==null)
    {
     flag=false;
    }
   }
   br.close();
   fr.close();
   for(int i=0;i<list.size();i++)
   {
    if(list.get(i).startsWith(num))
    {
     list.remove(i);
     result=true;
     break;
    }
   }//将对应的花朵记录移除
   FileWriter fw=new FileWriter("Flower.txt");
   //创建一个文件的字符输出流,不需要追加
   BufferedWriter bw=new BufferedWriter(fw);
   //以上面创建的对象为参数创建一个缓冲字符输出流
   for(int i=0;i<list.size();i++)
   {
    bw.write(list.get(i));
    //分别写入花卉记录
       bw.newLine();
   }
   bw.flush();
   bw.close();
   fw.close();
  }
  catch(IOException ex)
  {
   
  }
  return result;
 }
 //删除多条花朵记录的方法
 public int DeleteFlowers(String[] nums)
 {
  int result=0;
  //用来保存成功删除的花朵记录个数
  if(nums!=null)
  {
   if(nums.length>0)
   {
    for(int i=0;i<nums.length;i++)
    {
     if(DeleteFlower(nums[i]))
     {
      result++;
     }
    }
   }
  }
  return result;
 }
 
 //修改一条记录的方法
 public boolean ModifyFlower(String Floinfo)
 {
  String[] strs=Floinfo.split(",");
  if(numIsExist(strs[0]))
  {
   try
   {
    FileReader fr=new FileReader("Flower.txt");
    //产生了一个文件字符输入流对象
    BufferedReader br=new BufferedReader(fr);
    //以上一个对象作为参数来创建一个缓冲的字符输入流对象
    String str=br.readLine();//读取一行数据
    ArrayList<String> list=new ArrayList<String>();
    //保存读入的花朵信息
    boolean flag=true;
    while(flag)
    {
     list.add(str);
     str=br.readLine();//再次读取一行数据
     if(str==null)
     {
      flag=false;
     }
    }
    br.close();
    fr.close();
    
    for(int i=0;i<list.size();i++)
    {
     if(list.get(i).startsWith(strs[0]))
     {
      list.remove(i);
      //先从动态数组对象中移除
      list.add(Floinfo);
      //然后再添加修改后的花朵记录
      break;
     }
    }//将对应的花朵记录移除
    FileWriter fw=new FileWriter("Flower.txt");
    //创建一个文件的字符输出流,不需要追加
    BufferedWriter bw=new BufferedWriter(fw);
    //以上面创建的对象为参数创建一个缓冲字符输出流
    for(int i=0;i<list.size();i++)
    {
     bw.write(list.get(i));
     //分别写入花朵记录
        bw.newLine();
    }
    bw.flush();
    bw.close();
    fw.close();
   }
   catch(IOException ex)
   {
    
   }
   return true;
  }
  else
  {
   return false;
  }
 }
 //修改多条花朵记录
 public int ModifyFlowers(String[] Flos)
 {
  int result=0;
  if(Flos!=null)
  {
   if(Flos.length>0)
   {
    for(int i=0;i<Flos.length;i++)
    {
     if(ModifyFlower(Flos[i]))
     {
      result++;
     }
    }
   }
  }
  return result;
 }
 
}

这个代码量有点大,但难度倒不大。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们