JFrame将按钮单击的变量传递给另一个类

时间:2016-11-04 23:09:43

标签: java jframe

让我们说我有两个类,如下所示。在我的第一个名为Product的类中,我有产品实例变量,在第二个类中我有我的JFrame组件。现在我试图从JTextField titemName获取值,并在单击按钮时将其分配给setProductName?

有人可以给我一个如何做的提示吗?到目前为止,我得到getWineName的null calue,因为setWineName超出了范围我猜?

第一堂课:

public class Wine {
  private String wineName;
  private double winePrice;
  private int wineQnty;

public Wine(String wineName, double winePrice, int wineQnty)
{
    this.wineName = wineName;
    this.winePrice = winePrice;
    this.wineQnty = wineQnty;
}

public Wine()
{
    this ("", 0, 0);
}


// Getters (accessor methods) and setters(modifier methods) 
public String getWineName() {
    return wineName;
}

public void setWineName(String wineName) {
    this.wineName = wineName;
}

public double getWinePrice() {
    return winePrice;
}

public void setWinePrice(double winePrice) {
    this.winePrice = winePrice;
}

public int getWineQnty() {
    return wineQnty;
}

public void setWineQnty(int wineQnty) {
    this.wineQnty = wineQnty;
}

}

第二课:

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class LWMGUI extends JFrame
implements ActionListener
{ 
    private Wine wine;
    private CustomerAccount custAcct;
    private JButton psale, preturn;
    private JLabel litemName, litemQuantity, litemPrice, ltransAmount, lcurrBalance; 
    private JTextField titemName, titemQuantity, titemPrice, ttransAmount, tcurrBalance;

public LWMGUI(Wine w, CustomerAccount c)
{
    wine = w;
    custAcct = c;


    // Creating Dialog box to get customer's name
    String custName = JOptionPane.showInputDialog("Please enter the customer name: ");
    if (custName.isEmpty() || custName == null){
        System.exit(0);
    }


    //Creating dialog box to get the current balance for the given customer
    String currBalanceTxt;
    while (true){
         currBalanceTxt = JOptionPane.showInputDialog("Please enter the current balance for this customer: ");

         try {
                Double currBalance = Double.parseDouble(currBalanceTxt);
                break;

            } catch (NumberFormatException ex)
            {
                JOptionPane.showMessageDialog(null, "Please enter a numeric value!", "Non-numeric value error", JOptionPane.ERROR_MESSAGE);
            }
    }
    Double currBalance = Double.parseDouble(currBalanceTxt);

    //Setting customer account values
    c.setAccountName(custName);
    c.setCurrBalance(currBalance);

    setTitle("Lilybank Wine Merchants: " + custAcct.getAccountName());
    setSize(560, 150);
    setLocation(200, 200);

    createTop();
    createButtons();
    createBottom();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);


}

    private void createTop() {
        litemName = new JLabel("Name:");
        litemQuantity = new JLabel("Quantity:");
        litemPrice = new JLabel("Price: £");

        titemName = new JTextField(15);
        titemName.addActionListener(this);
        titemQuantity = new JTextField(6);
        titemQuantity.addActionListener(this);
        titemPrice = new JTextField(7);
        titemPrice.addActionListener(this);

        JPanel topPan = new JPanel();
        topPan.add(litemName);
        topPan.add(titemName);
        topPan.add(litemQuantity);
        topPan.add(titemQuantity);
        topPan.add(litemPrice);
        topPan.add(titemPrice);
        add(topPan, "North");
    }

    private void createBottom() {
        ltransAmount = new JLabel("Amount of Transaction:");
        lcurrBalance = new JLabel("Current balance:");
        ttransAmount = new JTextField(6);
        tcurrBalance = new JTextField(6);
        ttransAmount.setEnabled(false);
        tcurrBalance.setEnabled(false);

        JPanel lowerPan = new JPanel();
        lowerPan.add(ltransAmount);
        lowerPan.add(ttransAmount);
        lowerPan.add(lcurrBalance);
        lowerPan.add(tcurrBalance);
        add(lowerPan, "South");
    }

    private void createButtons() {
        // Setting up buttons
        psale = new JButton("Process Sale");
        preturn = new JButton("Process Return");
        psale.addActionListener(this);
        preturn.addActionListener(this);
        JPanel buttonPan = new JPanel();
        buttonPan.add(psale);
        buttonPan.add(preturn);
        add(buttonPan, "Center");
    }


    private class saleButton implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

        }

    }

    public void actionPerformed(ActionEvent e) 
    {

        if (e.getSource() == psale)
        {

            Wine w = wine;
            CustomerAccount ct = custAcct;

            System.out.println("Sale button pressed!");
            String wName = titemName.getText();
            int wQnty = Integer.parseInt(titemQuantity.getText());
            Double wPrice = Double.parseDouble(titemPrice.getText());

            w.setWineName(wName);
            w.setWineQnty(wQnty);
            w.setWinePrice(wPrice);

            String saleAmount = Double.toString(ct.sale());
            System.out.println(saleAmount);
            ttransAmount.setText(saleAmount);


        }

    }

}

第3课:

import javax.swing.JFrame;

公共类MainCl {

public static void main(String[] arg)
{
    Wine wine = new Wine();
    CustomerAccount cuAcct = new CustomerAccount();
    LWMGUI g = new LWMGUI(wine, cuAcct);

    System.out.println(wine.getWineName());
    System.out.println(wine.getWinePrice());
}

}

2 个答案:

答案 0 :(得分:0)

您应该在Product p = new Product();方法中用Product p = product;替换行actionPerformed

答案 1 :(得分:0)

Shrotly只是尝试在葡萄酒课上工作,你必须改变你的代码,并在主类中删除'wine.get ....',因为葡萄酒类是空的或现在不工作!你需要从动作监听器方法(工作期间)打印价格或名称,如下所示:

    public void actionPerformed(ActionEvent e) 
    {

        if (e.getSource() == psale)
        {

            Wine w = wine;


            System.out.println("Sale button pressed!");
            String wName = titemName.getText();
            int wQnty = Integer.parseInt(titemQuantity.getText());
            Double wPrice = Double.parseDouble(titemPrice.getText());

            w.setWineName(wName);
            w.setWineQnty(wQnty);
            w.setWinePrice(wPrice);

            System.out.println("Name of wine : "+ wName +", "+"Quanty : "+ wQnty +", "+"Price : "+ wPrice);


        }

    }

}

我希望这对你有所帮助。祝你好运

相关问题