代码之家  ›  专栏  ›  技术社区  ›  Thomas Yamakaitis

Java Swing-JTable未显示在滚动窗格中

  •  0
  • Thomas Yamakaitis  · 技术社区  · 8 年前

    Cart 里面是一个JTable和两个ArrayList。由于某些原因,我的JTable无法显示。

    这是我的购物车课程:

    class Cart {
        ArrayList<Product> products = new ArrayList<>(); // Holds the products themselves
        ArrayList<Integer> quantities = new ArrayList<>(); // Holds the quantities themselves
        JTable prdTbl = new JTable(); // The GUI Product Table
        DefaultTableModel prdTblModel = new DefaultTableModel(); // The Table Model
        Object[] columns = {"Description","Price","Quantity","Total"}; // Column Identifiers
        DecimalFormat fmt = new DecimalFormat("$#,##0.00;$-#,##0.00"); // Decimal Format for formatting USD ($#.##)
    
        Cart() {
            setTableStyle();
        }
    
        void renderTable() {        
            // Re-initialize the Table Model
            this.prdTblModel = new DefaultTableModel();
    
            // Set the Table Style
            setTableStyle();
    
            // Create a row from each list entry for product and quantity and add it to the Table Model
            for(int i = 0; i < products.size(); i++) {
                Object[] row = new Object[4];
    
                row[0] = products.get(i).getName();
                row[1] = products.get(i).getPrice();
                row[2] = quantities.get(i);
                row[3] = fmt.format(products.get(i).getPrice() * quantities.get(i));
    
                this.prdTblModel.addRow(row);
            }
    
            this.prdTbl.setModel(this.prdTblModel);
        }
    
        void setTableStyle() {
            this.prdTblModel.setColumnIdentifiers(columns);
            this.prdTbl.setModel(this.prdTblModel);
            this.prdTbl.setBackground(Color.white);
            this.prdTbl.setForeground(Color.black);
            Font font = new Font("Tahoma",1,22);
            this.prdTbl.setFont(font);
            this.prdTbl.setRowHeight(30);
        }
    
        JTable getTable() {
            renderTable(); // Render Table
            return this.prdTbl;
        }
    }
    

    下面是我对Swing应用程序窗口的initialize()方法:

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 590, 425);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        Cart cart = new Cart();
    
        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        frame.getContentPane().add(tabbedPane, "cell 0 0,grow");
    
        JPanel cartPanel = new JPanel();
        tabbedPane.addTab("Cart", null, cartPanel, null);
        cartPanel.setLayout(new MigLayout("", "[grow]", "[][grow]"));
    
        JScrollPane scrollPane = new JScrollPane();
        cartPanel.add(scrollPane, "cell 0 1,grow");
    
        table = new JTable();
        scrollPane.setViewportView(table);
    
        JButton btnAdd = new JButton("Add");
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String[] item = {"Macadamia", "Hazelnut", "Almond", "Peanut", "Walnut", "Pistachio", "Pecan", "Brazil"};
                Double[] price = {2.00, 1.90, 1.31, 0.85, 1.12, 1.53, 1.25, 1.75};
    
                int choice = (int) (Math.random() * item.length);
    
                Product p = new Product(item[choice], price[choice]);
    
                cart.addProduct(p);
                table = cart.getTable();
            }
        });
        cartPanel.add(btnAdd, "flowx,cell 0 0");
    
        JButton btnRemove = new JButton("Remove");
        cartPanel.add(btnRemove, "cell 0 0");
    
        JButton btnClear = new JButton("Clear");
        cartPanel.add(btnClear, "cell 0 0");
    
    }
    

    我不确定我是否遗漏了什么?在过去,它像这样工作得很好?我还尝试在 table = cart.getTable(); ,它似乎很好地接受了这些值,因此我相信这与摆动有关 initialize() 而不是我的 运货马车 运货马车 课堂也是如此。

    2 回复  |  直到 8 年前
        1
  •  1
  •   ugo    8 年前

    table = new JTable();
    scrollPane.setViewportView(table);
    

    我看不到表的声明位置,而且表什么也不包含,并没有行,并没有列,而在actionListener中,您使用一个新实例初始化表:

    table = cart.getTable();
    

        2
  •  1
  •   Hans    8 年前

    看起来您从未将购物车与cartPanel相关联;我认为你的问题在于:

     JPanel cartPanel = new JPanel();
    

    你做了一个新的面板,但从来没有钩住你的购物车。否则看起来不错。