| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1320 人关注过本帖
标题:不能添加学生信息
只看楼主 加入收藏
垃圾Swing
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-1-8
收藏
 问题点数:0 回复次数:0 
不能添加学生信息
在swing中Jtable
我先是输入菜品编号或者菜品名称,然后点查询按钮,然后就会清空表,把我查询的信息显示出来,但是我这个是直接点查询,表数据就没了,没添加成功,该怎么办呢????
package view;

import dao.GoodsDao;
import model.Goods;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class GoodsSel extends JFrame implements ActionListener {
    JFrame s;
    DefaultTableModel model;
    JTable table;
    JScrollPane sp;
    String[] heads;
    JButton btnSel;
    JComboBox<String> box;
    JTextField textId,textName;
    public GoodsSel(){
        s = new JFrame("查询菜品");
        s.setLayout(null);
        box = new JComboBox<>();

        JLabel labId = new JLabel("菜品编号");
        s.add(labId);
        labId.setBounds(60,30,65,45);
        textId = new JTextField();
        s.add(textId);
        textId.setBounds(130,40,100,25);
        JLabel labName = new JLabel("菜品名称:");
        s.add(labName);
        labName.setBounds(250,30,65,45);
        textName = new JTextField();
        s.add(textName);
        textName.setBounds(320,40,100,25);

        btnSel = new JButton("查询");
        s.add(btnSel);
        btnSel.setBounds(600,39,65,25);

        //导入表数据
        JPanel jpTable = new JPanel();
        jpTable.setLayout(null);
        jpTable.setBounds(40,80,700,160);
        //蚀刻边框
        jpTable.setBorder(BorderFactory.createEtchedBorder());
        s.add(jpTable);

        box.addItem("菜品编号");
        box.addItem("菜品名称");
        box.addItem("菜品价格");
        box.addItem("菜品描述");
        //设置表格列名
        heads = new String[]{"菜品编号","菜品名称","菜品价格","菜品描述"};
        GoodsDao dao = new GoodsDao();
        List<Goods> goodsList = dao.findAll();
        Object[][] arr=new Object[goodsList.size()][4];
        for (int i=0;i<goodsList.size();i++){
            Goods goods = goodsList.get(i);
            arr[i][0]=goods.getd_id();
            arr[i][1]=goods.getd_name();
            arr[i][2]=goods.getd_price();
            arr[i][3]=goods.getd_describe();

        }
        //实例化表格模型
        model = new DefaultTableModel(arr,heads);
        //实例化表格
        table = new JTable(model);
        //实例化滚动面板
        sp = new JScrollPane(table);
        sp.setBounds(0,0,700,160);
        jpTable.add(sp);
        s.add(jpTable);

        btnSel.addActionListener(this);

        s.setSize(780,320);
        s.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        s.setVisible(true);

    }
    //清空表格
    public void clearModel(){
        while (model.getRowCount()>0){
            model.removeRow(0);
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(btnSel)){
            GoodsDao dao = new GoodsDao();
            List<Goods> list = new ArrayList<>();
            Goods c = new Goods();
            if (box.getSelectedItem().equals("菜品编号")){
                c = dao.findByID(textId.getText());
                System.out.println("菜品编号");
                clearModel();
                list.add(c);

            }else if (box.getSelectedItem().equals("菜品名称")){
                list = dao.findByName(textName.getText());
                System.out.println("菜品名称");
                clearModel();
               
            }
        }

    }
    public static void main(String[] args) {
        new GoodsSel();
    }
}
搜索更多相关主题的帖子: model box import new add 
2021-01-08 02:43
快速回复:不能添加学生信息
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015818 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved