如何提出这个涉及列表组件的JAVA gui应用程序?

时间:2014-02-01 02:18:26

标签: java user-interface

我正在自己学习Java,而且我自己也在接受一个非常小的项目以获得乐趣,而我只是坚持项目的这一小部分。 所以我创建了一个文本文件,其中包含一小部分玩具名称和价格,例如:

芭比,12.95 乐高,15.99 风火轮,5.00 电力别动队,6.49

我想要的是我的应用程序来读取文件的内容并将玩具名称存储在列表组件中。然后我希望能够从列表中选择一个玩具名称并添加它 也是一个列表组件的购物车。我希望应用程序有菜单项和按钮,允许我从购物车中删除项目,清除所有购物车 选择,并检查。当我退房时,应用程序应计算并显示购物车中所有玩具名称的小计,即销售税(可能只占8%) 小计)和总数。

请帮助创建我刚刚编写的这个简单的应用程序示例,并且我将添加并使用此示例来自己创建更大的应用程序。我真的很感激。谢谢!

1 个答案:

答案 0 :(得分:1)

我帮助您创建了所有涉及的GUI组件,我相信这是您应用程序中最乏味的部分,除非您有Netbeans,您可以使用他们的GUI编辑器轻松创建JFrame表单。此外,使用组合框的列表更适合您选择不同的玩具,因为它可以动态调整大小,即使您有数百种不同的玩具也不会成为GUI的问题。

现在你所要做的就是:

  1. 创建一个BufferedReader以读入文本
  2. 使用String split()方法拆分CSV,并将文本解析为两个独立的数组 - 一个用于玩具的名称,另一个用于其值
  3. 更新toysComboBox以显示文本文件的输入
  4. 更新addToCartButton以更新shoppingCartTable以显示第一列下的项目名称和第二列中的费用
  5. 更新addToCartButton以重新计算shoppingCartTable中的所有费用,然后更新subtotalValueLabel,salesTaxValueLabel和totalValueLabel
  6. 更新clearCartButton以将整个shoppingCartTable设置为“”,并将subtotalValueLabel,salesTaxValueLabel和totalValueLabel设置为$ 0.00
  7. 更新removeSelectedItem以删除shoppingCartTable中的选定行,然后将已删除行下方的所有值推高以填补空白,之后它将重新计算shoppingCartTable中的所有成本,然后更新subtotalValueLabel,salesTaxValueLabel和totalValueLabel
  8. 按checkOutButton后发生了一些事情 - 这完全取决于你
  9. 希望您从中学到一些东西,并为您的项目提供最好的帮助!

    代码:

    package test; // Rename with your package name
    
    public class JavaProject extends javax.swing.JFrame { //Replace class name with yours
    
    
        public JavaProject() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        private void initComponents() {
    
            toysComboBox = new javax.swing.JComboBox();
            addToyLabel = new javax.swing.JLabel();
            addToCartButton = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            shoppingCartTable = new javax.swing.JTable();
            shoppingCartLabel = new javax.swing.JLabel();
            subtotalLabel = new javax.swing.JLabel();
            subtotalValueLabel = new javax.swing.JLabel();
            salesTaxLabel = new javax.swing.JLabel();
            salesTaxValueLabel = new javax.swing.JLabel();
            totalLabel = new javax.swing.JLabel();
            totalValueLabel = new javax.swing.JLabel();
            checkOutButton = new javax.swing.JButton();
            removeSelectedButton = new javax.swing.JButton();
            toyStoreLabel = new javax.swing.JLabel();
            clearCartButton = new javax.swing.JButton();
            splitLabel = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            toysComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Barbie - $12.95", "Lego - $15.99", "Hot Wheels - $5.00", "Power Rangers - $6.49" }));
            toysComboBox.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    toysComboBoxActionPerformed(evt);
                }
            });
    
            addToyLabel.setText("Add Toy:");
    
            addToCartButton.setText("Add to Cart");
    
            shoppingCartTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null}
                },
                new String [] {
                    "Item", "Cost"
                }
            ));
            jScrollPane1.setViewportView(shoppingCartTable);
    
            shoppingCartLabel.setText("Shopping Cart");
    
            subtotalLabel.setText("Subtotal:");
    
            subtotalValueLabel.setText("$0.00");
    
            salesTaxLabel.setText("Sales Tax:");
    
            salesTaxValueLabel.setText("$0.00");
    
            totalLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
            totalLabel.setText("Total:");
    
            totalValueLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
            totalValueLabel.setText("$0.00");
    
            checkOutButton.setText("Check Out");
            checkOutButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    checkOutButtonActionPerformed(evt);
                }
            });
    
            removeSelectedButton.setText("Remove Selected Item");
    
            toyStoreLabel.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
            toyStoreLabel.setText("Toy Store");
    
            clearCartButton.setText("Clear Cart");
    
            splitLabel.setText("_____________________");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(14, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(addToyLabel)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(toysComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(addToCartButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(58, 58, 58)
                                            .addComponent(toyStoreLabel))))
                                .addComponent(shoppingCartLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(clearCartButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(removeSelectedButton))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(salesTaxLabel)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(salesTaxValueLabel)
                                            .addGap(257, 257, 257))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(subtotalLabel)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(subtotalValueLabel))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(totalLabel)
                                                .addGap(18, 18, 18)
                                                .addComponent(totalValueLabel))))))
                            .addContainerGap(12, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(splitLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(checkOutButton, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(30, 30, 30))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(9, 9, 9)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(checkOutButton, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(toyStoreLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(addToyLabel)
                                .addComponent(toysComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(addToCartButton, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addComponent(shoppingCartLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(removeSelectedButton)
                                .addComponent(clearCartButton))
                            .addGap(27, 27, 27)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(subtotalLabel)
                                .addComponent(subtotalValueLabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(salesTaxLabel)
                                .addComponent(salesTaxValueLabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(splitLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(totalLabel)
                                .addComponent(totalValueLabel))))
                    .addContainerGap(41, Short.MAX_VALUE))
            );
    
            pack();
        }                      
    
        private void toysComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                             
            // TODO add your handling code here:
        }                                            
    
        private void checkOutButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
    
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(JavaProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(JavaProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(JavaProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(JavaProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new JavaProject().setVisible(true);
                }
            });
        }
    
        private javax.swing.JButton addToCartButton;
        private javax.swing.JLabel addToyLabel;
        private javax.swing.JButton checkOutButton;
        private javax.swing.JButton clearCartButton;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JButton removeSelectedButton;
        private javax.swing.JLabel salesTaxLabel;
        private javax.swing.JLabel salesTaxValueLabel;
        private javax.swing.JLabel shoppingCartLabel;
        private javax.swing.JTable shoppingCartTable;
        private javax.swing.JLabel splitLabel;
        private javax.swing.JLabel subtotalLabel;
        private javax.swing.JLabel subtotalValueLabel;
        private javax.swing.JLabel totalLabel;
        private javax.swing.JLabel totalValueLabel;
        private javax.swing.JLabel toyStoreLabel;
        private javax.swing.JComboBox toysComboBox;
    }