【问题标题】:Swing: how to get value from JTextfield in every JPanel?Swing:如何从每个 JPanel 中的 JTextfield 中获取价值?
【发布时间】:2014-12-12 15:35:44
【问题描述】:

我的学校项目是创建一个采购系统,为此我创建了一个JPanel 数组来列出所有产品信息,并允许用户为每个项目输入一些内容。而且我不知道如何通过单击一个按钮从 jtextfields 中获取所有值。 actionPerformed() 方法总是需要一个 final 变量,这对我来说很麻烦。

private JButton payBtn;

public void shoppingCartTab(Customer userIn){
    contentPanel.removeAll();
    bottomPanel.removeAll();
    final Customer USER = userIn;
    ArrayList<Product> pArray = new ArrayList<Product>();
    pArray = loadCartFile.loadCartFile(userIn);
    JLabel tabLabel  = new JLabel("Shopping Cart");
    JPanel cartItems = new JPanel();
    cartItems.setLayout(new BoxLayout(cartItems, BoxLayout.Y_AXIS));
    final JPanel CONSTANT_CART = cartItems;
    JScrollPane scroller = new JScrollPane(cartItems);  

    if(pArray != null){
        JPanel item[] = new JPanel[pArray.size()];
        for(int i = 0; i< pArray.size(); i++){

            item[i] = new JPanel(); 
            final JPanel JPANEL_TO_DEL = item[i];
            item[i].setLayout(new GridBagLayout());
            GridBagConstraints gBC = new GridBagConstraints();
            gBC.weightx = 0.3;
            item[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));

            JLabel icon_small = new JLabel(new ImageIcon("Icons\\" + pArray.get(i).getID() + "_small.jpg"));
            JLabel itemID = new JLabel(pArray.get(i).getID());  

            final String CONSTANT_ID = pArray.get(i).getID();
            JLabel itemName = new JLabel(pArray.get(i).getName());
            JLabel itemPrice = new JLabel("$" + pArray.get(i).getPrice());
            JPanel setQuantity = new JPanel();
            JButton plusBtn = new JButton("+");plusBtn.setPreferredSize(new Dimension(45,30));
            final JTextField QUANTITY = new JTextField("0");QUANTITY.setColumns(3);

            QUANTITY.setPreferredSize(new Dimension(45,30));QUANTITY.setHorizontalAlignment(JTextField.CENTER);
            JButton minusBtn = new JButton("-");minusBtn.setPreferredSize(new Dimension(45,30));

            plusBtn.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent event){
                        if(Integer.parseInt(QUANTITY.getText())<100)
                            QUANTITY.setText(Integer.toString(Integer.parseInt(QUANTITY.getText())+1));
                    }
                });
            minusBtn.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent event){
                        if(Integer.parseInt(QUANTITY.getText())>0)
                            QUANTITY.setText(Integer.toString(Integer.parseInt(QUANTITY.getText())-1));
                    }
                }); 

            setQuantity.add(plusBtn);
            setQuantity.add(QUANTITY);
            setQuantity.add(minusBtn);
            JButton delBtn = new JButton("Delete");
            delBtn.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent event){
                        int dialogResult = JOptionPane.showConfirmDialog(null, "Are you sure to remove this item from your cart?", "Confirm", JOptionPane.YES_NO_OPTION);
                        if(dialogResult == JOptionPane.YES_OPTION){
                            CONSTANT_CART.remove(JPANEL_TO_DEL);
                            revalidate();
                            repaint();
                            ShoppingCart.removeItem(USER, CONSTANT_ID);
                        }
                    }
                }); 
            gBC.gridx = 0;
            gBC.gridy = 0;
            item[i].add(icon_small,gBC);
            gBC.gridx = 1;
            gBC.gridy = 0;
            item[i].add(itemID,gBC);
            gBC.gridx = 2;
            gBC.gridy = 0;
            item[i].add(itemName,gBC);
            gBC.gridx = 3;
            gBC.gridy = 0;
            item[i].add(itemPrice,gBC);
            gBC.gridx = 4;
            gBC.gridy = 0;
            item[i].add(setQuantity,gBC);
            gBC.gridx = 5;
            gBC.gridy = 0;
            item[i].add(delBtn,gBC);
            cartItems.add(item[i]);
        }

        contentPanel.add(tabLabel); 
        contentPanel.add(scroller);

        payBtn = new JButton("Pay");
        bottomPanel.add(payBtn); payBtn.addActionListener(this);
    }else{
        JLabel emptyMsg = new JLabel("Your cart is empty!");
        contentPanel.add(emptyMsg);
    }

    revalidate();
    repaint();
}

【问题讨论】:

  • SO 不是编码服务...而且我们不会为您完成您的项目。如果您在调试某些东西时需要帮助,请发布一些代码,我们会尽力提供帮助。
  • 我们可以提供帮助,但您必须首先提供源示例并说明问题所在。
  • final 变量并不难制作,只需让另一个变量说 finalwhat=notfinalwhatever;但是您不需要做任何最终的事情,只需将侦听器与 JTextFeild 放在同一类中,或者让相同的侦听器侦听 JtextField 更改
  • 通过展示你的努力(代码示例)和你被阻止的地方很容易获得关于 SO 的帮助?
  • 超出主题,但JList 和自定义JCellRenderer 肯定比JPanel 更适合创建产品列表。

标签: java swing jpanel jbutton jtextfield


【解决方案1】:

一种解决方案是创建一个扩展 JPanel 的类型,并公开一个公共属性,该属性在调用时返回存储在 JTextField 中的值。比如:

public class TextFieldPanel extends JPanel {

    private JTextField myTextField;

    public TextFieldPanel()
    {

        //layout init code here

    }

    public String getText()
    {
        return myTextField.getText();       
    }

}

然后只使用这个类而不是常规的 JPanel。然后,当您单击按钮时,遍历集合中的每个对象,并在它们上调用 getText() 以获取您的值。

【讨论】:

  • 非常感谢!今天晚些时候我会试试的。
猜你喜欢
  • 2015-06-01
  • 2018-11-06
  • 2022-01-10
  • 2012-03-27
  • 2011-08-10
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多