buttonlistener类的值

时间:2015-12-22 06:49:06

标签: java

我是否有办法从buttonlistener类中获取值?

有没有办法可以将值设置/存储到getPrice中?这样我就可以将这些值称为不同的类。

这是我的驱动程序类

((x y)) (λx.(x y)))))

PizzaClass

import javax.swing.*;

public class Driver 
{
    public static void main(String[] args)
    {
       JFrame frame = new JFrame("Pizza");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       JTabbedPane tp = new JTabbedPane();
       tp.addTab("Pizza", new PizzaPanel());




       frame.getContentPane().add(tp);
       frame.pack();
        frame.setVisible(true);
     } 
 }

一般课程

import java.awt.*;
import javax.swing.*;
public class PizzaPanel extends JPanel
 {
    private JRadioButton pep,com,pine,meat;
    public PizzaPanel()
    {

        JRadioButton th = new JRadioButton();
        setLayout(new BorderLayout());
       //ButtonListener gen = new ButtonListener();
        General gen1 = new General();
        Total tol = new Total();


        //add(gen,BorderLayout.CENTER);

        add(gen1, BorderLayout.SOUTH);
        add(tol, BorderLayout.EAST);



    }


}

总课程

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.EventObject;
import javax.swing.*;

public class General extends JPanel
{
    private static JRadioButton pep;
    private static JRadioButton pine;
    private static JRadioButton meat;
    private static JRadioButton com;
    private static JLabel cool, label;
    private static double money;
    private static double prices, total;
    double pepAmount = 0;
    private static Action e;

    public General()
    {

       cool = new JLabel("What type of pizza would you like?");

       label = new JLabel("Total "+money);
       pep = new JRadioButton("Pepperoni");
       pine = new JRadioButton("Pineapple");
       meat = new JRadioButton("Meat Lovers");
       com = new JRadioButton("Combination");


       ButtonGroup group = new ButtonGroup();
       group.add(pep);
       group.add(pine);
       group.add(meat);
       group.add(com);

    ButtonListener listener = new ButtonListener();
    pep.addActionListener(listener);
    pine.addActionListener(listener);
    com.addActionListener(listener);
    meat.addActionListener(listener);
    //Total tol = new Total();


    add(cool);
    add(pep);
    add(pine);
    add(com);
    add(meat);
    add(label);
    setPreferredSize(new Dimension(900, 700));
    setBackground(Color.white);

}

public static void setPrice(double price)
{
    money = price;

}

public static  double getPrice()
{
    return money;
}






public static class ButtonListener implements ActionListener
{




    public void actionPerformed(ActionEvent e) 
    {  
        if(e.getSource() == pep)
        {

         setPrice(14);
         label.setText("Pepperoni Cost $" +getPrice());

        }

        if(e.getSource() == pine)
        {
            money = 16;
        setPrice(16);
        label.setText("Pineapple Cost $" +getPrice());

        }

        if(e.getSource() == com)
        {
            money=18 ;
        setPrice(18);
        label.setText(" Cost $" +getPrice());

        }

        if(e.getSource() == meat)
        { 
            money = 20;
        setPrice(20);
        label.setText(" Cost $" +getPrice());
        }


    }



}   

}

1 个答案:

答案 0 :(得分:0)

由于您不熟悉Java,我会跳过您不需要(并且可能不应该使用)static的大部分类和变量的原因,但是你可以做类似以下的事情。

我认为您希望getPrice成为static的原因是您可以在Total中使用它吗?如果是这样,您也可以将General参数传递给Total

<强> ButtonListener

public class ButtonListener implements ActionListener
{
    private General g;

    public ButtonListener(General g) {
      this.g = g;
    }

    public void actionPerformed(ActionEvent e) 
    {  
        if(e.getSource() == pep) {
            g.setPrice(14);
            label.setText("Pepperoni Cost $" +getPrice());
        }

        // other if's (though should be "else if ()")
    }
}

常规

public class General extends JPanel
{
    private JRadioButton pep;
    private JRadioButton pine;
    private JRadioButton meat;
    private JRadioButton com;
    private JLabel cool, label;
    private double money;
    private double prices, total;
    double pepAmount = 0;

    public General() {
        // other stuff

        ButtonListener listener = new ButtonListener(this);
        pep.addActionListener(listener);
        // other addActionListener

        //Total tol = new Total();

        // other stuff
    }

    public void setPrice(double price) {
        money = price;
    }

    public double getPrice() {
        return money;
    }

}