Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java awt意外重复问题_Java_User Interface_Events_Awt - Fatal编程技术网

Java awt意外重复问题

Java awt意外重复问题,java,user-interface,events,awt,Java,User Interface,Events,Awt,我在使用JavaAWT时遇到了一个问题,特别是与意外重复相关的问题。 我的目标是通过累加器将库存增加到每次输入的金额,但是在第一次添加库存后,这并没有达到预期效果。详情如下: 导致问题的代码从portfolio类开始,但我已经发布了所有代码 在portfolio and Action2 innerclass(从buy()方法调用)中,它表示st1.increaseAmount(quantity),它增加了特定产品的数量。第一次单击“购买”时,效果很好。但是第二次发生两次,然后第三次,三次等等 我

我在使用JavaAWT时遇到了一个问题,特别是与意外重复相关的问题。 我的目标是通过累加器将库存增加到每次输入的金额,但是在第一次添加库存后,这并没有达到预期效果。详情如下:

导致问题的代码从portfolio类开始,但我已经发布了所有代码

在portfolio and Action2 innerclass(从buy()方法调用)中,它表示st1.increaseAmount(quantity),它增加了特定产品的数量。第一次单击“购买”时,效果很好。但是第二次发生两次,然后第三次,三次等等

我在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");
        }
    }
import java.awt.*;
导入java.awt.event.*;
公营课
{
公共静态void main(字符串[]args)
{
新模拟();
}
}
公共类模拟实现ActionListener
{
专用帧f;
私有文本字段t=新文本字段(10);
公共模拟()
{
f=新框架(“组合”);
f、 设置大小(400200);
f、 setLayout(新的FlowLayout());
标签l=新标签(“输入您的姓名”);
按钮b=新按钮(“继续”);
b、 addActionListener(此);
f、 添加(l);f.添加(t);f.添加(b);
f、 setVisible(真);
f、 addWindowListener(新的WindowAdapter()
{
公共无效窗口关闭(WindowEvent e)
{
系统出口(0);
};
});
}
已执行的公共无效操作(操作事件e)
{
String name=t.getText();
组合p=新组合(名称);
f、 removeNotify();
}
}
公共类公文包实现ActionListener
{
私募股权st1=新大众传媒();
私有字符串名称;
专用帧f;
私人按钮购买股份=新按钮(“购买股份”);
私有按钮sellShares=新按钮(“出售股份”);
私有帧f1=新帧(“购买股票”);
私有列表s=新列表(1,false);
私有列表s1=新列表(1,false);
私人按钮b=新按钮(“购买”);
私有字符串共享=”;
私有文本字段tf=新文本字段(“金额”);
公共投资组合(字符串名称)
{
this.name=名称;
tradeDisplay();
}
公开展览()
{
f=新框架(名称+““的投资组合”);
f、 设置大小(400200);
f、 setLayout(新的FlowLayout());
buyShares.addActionListener(此);
sellShares.addActionListener(本);
f、 添加(购买股份);f.添加(出售股份);
f、 setVisible(真);
f、 addWindowListener(新的WindowAdapter()
{
公共无效窗口关闭(WindowEvent e)
{
系统出口(0);
};
}); 
}
已执行的公共无效操作(操作事件e)
{
如果(例如getActionCommand().equals(“购买股票”))
{
购买();
}
否则如果(例如getActionCommand()等于(“出售股份”))
{
}
}
公开无效购买()
{
f1.设置尺寸(400200);
f1.setLayout(新的FlowLayout());
标签p1=新标签(“大众龙价格:\u00A3”+st1.getPrice());
s、 添加(“汽车”);
s、 addActionListener(新Action1());
s1.addActionListener(新Action3());
b、 addActionListener(新Action2(p1));
f1.添加(s);f1.添加(s1);f1.添加(b);f1.添加(tf);
f1.添加(p1);
f1.setVisible(真);
f1.addWindowListener(新的WindowAdapter()
{
公共无效窗口关闭(WindowEvent e)
{
系统出口(0);
};
});
}
类Action1实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
字符串选择=e.getActionCommand();
if(选择等于(“汽车”))
{
s1.removeAll();
s1.添加(“大众传媒”);
}
}
}
类Action2实现ActionListener
{
标签l1;
公共行动2(标签p1)
{
l1=p1;
}
已执行的公共无效操作(操作事件e1)
{
f1.移除(l1);
字符串choice1=e1.getActionCommand();
double quantity=double.parseDouble(tf.getText());
如果(选择1.等于(“购买”))
{                                    
st1.增加金额(数量);
}
f1.dispose();
系统输出打印(“测试”);
}
}
类Action3实现ActionListener
{
已执行的公共无效操作(操作事件e2)
{
share=e2.getActionCommand();
}
}  
}
公开股
{
私人双价;
私人双倍金额;
私人双市场;
私人布尔繁荣;
私有最终字符串名;
公开股票(双倍金额,字符串名称)
{
这个。金额=金额;
this.name=名称;
boom=(Math.random()<0.5?真:假);
setMarket(0.6);
}
公开市场(股票价值加倍)
{
市场=股票价值*数学随机();
如果(动臂==真)
{
价格=市场*100;
}
其他的
{
价格=市场*40;
}
}
公开双价
{
退货价格;
}
公共字符串getName()
{
返回名称;
}
公共双getAmount()
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");
        }
    }
}
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) {
        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);
            };
        });

        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");
        }
    }
}