Java awt意外的重复问题

时间:2017-02-26 20:07:37

标签: java user-interface events awt

我遇到了使用java awt的问题,特别是与非预期的重复有关。 我的目标是使用累加器增加每次输入的库存量,但是在第一次添加库存后这不会按预期工作。详情如下:

导致问题的代码从投资组合类开始,但我已经发布了所有代码。

在投资组合和Action2内部类(从buy()方法调用)中,它表示st1.increaseAmount(数量),它增加了某个产品的数量。我第一次点击购买它工作正常。但第二次出现两次,然后是第三次,三次等等。

我在Action2课上测试了这个我的打印“测试”,同样我第一次想要购买更多的股票它打印一次,第二次打印两次,第三次打印三次等等。

所以,例如如果我通过购买股票3次,这就是印刷品:

测试

测试
测试

测试
测试
测试

虽然我希望它只是打印:

测试

测试

测试

可以正确添加正确的库存量。

非常感谢任何帮助。

谢谢!

import java.awt.*;
import java.awt.event.*;

public class run
{
    public static void main (String[] args)
    {
        new simulation();
    }

}


public class simulation implements ActionListener
{
    private Frame f;
    private TextField t = new TextField(10);


    public simulation()
    {
        f = new Frame("portfolio");
        f.setSize(400,200);
        f.setLayout(new FlowLayout());
        Label l = new Label("Enter your name");

        Button b = new Button("Continue");
        b.addActionListener(this);
        f.add(l); f.add(t); f.add(b);
        f.setVisible(true);


        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            };
        });

    }

    public void actionPerformed(ActionEvent e)
    {
        String name = t.getText();
        portfolio p = new portfolio(name);
        f.removeNotify();

    }
}


public class portfolio implements ActionListener
{


    private stock st1 = new Volksdragon();

    private String name;
    private Frame f;
    private Button buyShares = new Button("Buy shares");
    private Button sellShares = new Button("Sell shares");

    private Frame f1 = new Frame("Buy stocks");
    private List s = new List(1, false);
    private List s1 = new List(1, false);
    private Button b = new Button("Buy");
    private String share = "";
    private TextField tf = new TextField("Amount");

    public portfolio(String name)
    {
        this.name = name;
        tradeDisplay();
    }

    public void tradeDisplay()
    {
        f = new Frame(name + "'s portfolio");
        f.setSize(400,200);
        f.setLayout(new FlowLayout());

        buyShares.addActionListener(this);
        sellShares.addActionListener(this);

       f.add(buyShares); f.add(sellShares);

        f.setVisible(true);
        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            };
        }); 
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("Buy shares"))
        {
            buy();
        }
        else if (e.getActionCommand().equals("Sell shares"))
        {

        }
    }

    public void buy()
    {
        f1.setSize(400,200);
        f1.setLayout(new FlowLayout());

        Label p1 = new Label("Volksdragon price: \u00A3" + st1.getPrice());


        s.add("Cars");

        s.addActionListener(new Action1());
        s1.addActionListener(new Action3());
        b.addActionListener(new Action2(p1));


        f1.add(s); f1.add(s1); f1.add(b); f1.add(tf);
        f1.add(p1);
        f1.setVisible(true);

        f1.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            };
        });
    }


    class Action1 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String choice = e.getActionCommand();

            if (choice.equals("Cars"))
            {
                s1.removeAll();
                s1.add("Volksdragon");
            }
        }
    }

     class Action2 implements ActionListener
     {
        Label l1;

        public Action2(Label p1)
        {
           l1 = p1;
        }

        public void actionPerformed(ActionEvent e1)
        {
            f1.remove(l1);
            String choice1 = e1.getActionCommand();
            double quantity = Double.parseDouble(tf.getText());

            if (choice1.equals("Buy"))
            {                                    
                st1.increaseAmount(quantity);
            }

            f1.dispose();

            System.out.println("test");
        }
    }

    class Action3 implements ActionListener
    {
        public void actionPerformed(ActionEvent e2)
        {
            share = e2.getActionCommand();
        }
    }  

}

public class stock
{
    private double price;
    private double amount;
    private double market;
    private boolean boom;
    private final String name;

    public stock(double amount, String name)
    {
        this.amount = amount;
        this.name = name;
        boom = (Math.random() < 0.5 ? true : false);
        setMarket(0.6);
    }

    public void setMarket(double stockValue)
    {
        market = stockValue*Math.random();

        if(boom == true)
        {
            price = market*100;
        }
        else
        {
            price = market*40;
        }
    }

    public double getPrice()
    {
        return price;
    }

    public String getName()
    {
        return name;
    }

    public double getAmount()
    {
        return amount;
    }

    public void increaseAmount(double value)
    {
        amount += value;
    }

    public void decreaseAmount(double value)
    {
        amount -= value;
    }
}

    public class carStock extends stock
    {
        private double carMarket = Math.random();

        public carStock(double amount, String name)
        {
        super(amount, name);
        }
    }

    public class Volksdragon extends carStock
    {
        public Volksdragon()
        {
            super(0, "Volksdragon");
        }
    }

1 个答案:

答案 0 :(得分:2)

您正在重新添加ActionListeners(正如我之前在类似的已删除问题中提到的那样)。在这里,每次调用模拟监听器时,都会创建一个新的组合,调用tradeDisplay创建一个新的Frame,但使用相同的旧buyShares按钮并重新添加一个ActionListener到buyShares按钮。这个过程。

解决方案:不要重新创建框架和组件,而是重新使用。我自己,我用CardLayout交换组件。可以在此处找到该教程:CardLayout tutorial

更重要的是,学会精神上通过代码来查看正在运行的情况。如果您要开始编程,这是非常宝贵的,实际上是必要的技能。

此外,您还需要学习将代码缩减到最小,足以编译,运行和显示问题,而不是其他任何内容......并遵循Java命名约定,以便您的代码易于理解。例如,如果我减少了您的代码并更正了大写:

/tmp/

您将看到将f1 JFrame创建交换到Portfolio构造函数可以解决您的问题。现在ActionListener只添加一次:

import java.awt.*;
import java.awt.event.*;

public class Run {
    public static void main(String[] args) {
        new Portfolio("John Smith");
    }
}

class Portfolio implements ActionListener {
    private String name;
    private Frame f;
    private Button buyShares = new Button("Buy shares");
    private Frame f1 = new Frame("Buy stocks");
    private Button b = new Button("Buy");

    public Portfolio(String name) {
        this.name = name;
        tradeDisplay();
    }

    public void tradeDisplay() {
        f = new Frame(name + "'s portfolio");
        f.setSize(400, 200);
        f.setLayout(new FlowLayout());
        buyShares.addActionListener(this);
        f.add(buyShares);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            };
        });
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Buy shares")) {
            buy();
        }
    }

    public void buy() {
        f1.setSize(400, 200);
        f1.setLayout(new FlowLayout());
        b.addActionListener(new Action2());
        f1.add(b);
        f1.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            };
        });
        f1.setVisible(true);
    }

    class Action2 implements ActionListener {

        public void actionPerformed(ActionEvent e1) {
            f1.dispose();
            System.out.println("test");
        }
    }
}