创建按钮点击一类的新对象/ ActionEvent事件 [英] Creating new object of a class on button click / actionEvent
问题描述
找到解决方案
决定是容易只是让一个方法调用chairPrice的的ActionListener可以通过一个名为getItemPrice()方法来增加之外。这已被用于计算项目的总价格和工程100%
decided it was easier to simply make a method outside of the actionListener called chairPrice which can be incremented by a method called getItemPrice(). This has been used to calculate the total price of items and works 100%
推荐答案
您需要使用是Object.equals()
方法。
@Override
public void actionPerformed(ActionEvent buttonClick)
{
if(buttonClick.getSource().equals(guiButtons[0])) //if user clicks on 'add chair'
{
Chair chair = new Chair();
}
}
的 修改响应OP的评论的
Edit in response to the OP's comment
我不完全确定你想要什么。 myChair </ code>不是你的椅子的名称。它的变量的名称。它在所有对
主席
没有影响。如果你想使一个新的主席
对象,并把它供全班同学,你将需要或者添加新字段变量或使列表主席
。
I'm not exactly sure what you're wanting. myChair
isn't the name of your chair. It's the name of the variable. It has no effect on Chair
at all. If you want to make a new Chair
object and have it available for the whole class, you're going to need to either add a new field variable or make a list of Chair
.
public class GuiClass extends JPanel implements ActionListener
{
List<Chair> chairs = new ArrayList<Chair>(Arrays.asList(new Chair()));
Desk myDesk = new Desk();
Table myTable = new Table();
@Override
public void actionPerformed(ActionEvent buttonClick)
{
if(buttonClick.getSource().equals(guiButtons[0])) //if user clicks on 'add chair'
{
chairs.add(new Chair());
}
}
}
这篇关于创建按钮点击一类的新对象/ ActionEvent事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!