I have created buttons inside a panel using Java Swing. I have a button "Produce" that when clicked, I need it to create an object from type Produce (which is defined in another class and has items such as vendor, weight, and price).
我使用Java Swing在面板中创建了按钮。我有一个按钮“Produce”,点击后,我需要它来创建一个类型为Produce的对象(在另一个类中定义,并具有供应商,重量和价格等项目)。
When the button is clicked, the Produce object should be created using information that a user has typed in to the text fields in the same panel. So if a user typed in the vendor, weight, and price for an item into the textfields, the Produce object needs to be created with those values.
单击该按钮时,应使用用户键入同一面板中文本字段的信息创建Produce对象。因此,如果用户在文本字段中键入项目的供应商,权重和价格,则需要使用这些值创建Produce对象。
So far I have:
到目前为止我有:
public void createButtons() {
JButton produceBtn = new JButton("Produce");
JButton prepMealBtn = new JButton("Prepared Meal");
infoPanel.add(produceBtn, BorderLayout.SOUTH);
infoPanel.add(prepMealBtn, BorderLayout.SOUTH);
produceBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == produceBtn){
Produce myProduce = new Produce();
}
}
}
}
But I can't figure out how to do the mentioned part above.
但我无法弄清楚如何做上面提到的部分。
1 个解决方案
#1
2
the Produce object should be created using information that a user has typed in to the text fields in the same panel. So if a user typed in the vendor, weight, and price for an item into the textfields, the Produce object needs to be created with those values.
应使用用户在同一面板中的文本字段中键入的信息创建Produce对象。因此,如果用户在文本字段中键入项目的供应商,权重和价格,则需要使用这些值创建Produce对象。
First, you'll need to create some sort of list/array to hold the created objects.
首先,您需要创建某种列表/数组来保存创建的对象。
List<Produce> produce = new ArrayList<>(); // make this global within the class
then, you have two options, either overload the Produce
constructor and create another constructor to take 3 params (vendor
, weight
, and price
):
那么,你有两个选项,要么重载Produce构造函数,要么创建另一个构造函数来获取3个参数(供应商,重量和价格):
Example constructor:
public Produce(type param1, type param2, type param3){ //Constructor to take 3 params
// assign the values appropriately
}
or create setter methods:
或创建setter方法:
public void setVender(type vender){
// assign the values appropriately
}
public void setWeight(type weigth){
// assign the values appropriately
}
public void setPrice(type price){
// assign the values appropriately
}
then you can do this:
然后你可以这样做:
if(source == produceBtn){
String vender = someTextField.getText();
String weight = someTextField.getText();
String price = someTextField.getText();
//perform any conversion from string to numbers if needed.
/*
*/
//then create the object
Produce myProduce = new Produce(vender,weight,price);
// make sure the order in which you input the data into the arguments of the constructor above is the same as the order in which the constructor definition of the Produce class is.
produce.add(myProduce);
}
or you can use the setter methods.
或者你可以使用setter方法。
Lastly but not least, you seem to have a typo with addActionListener
, you're missing the closing )
.
最后但并非最不重要的是,你似乎有一个拼写错误的addActionListener,你错过了结束)。
Also, you can simplify your code by using lambda expression:
此外,您可以使用lambda表达式简化代码:
produceBtn.addActionListener(e -> {
Object source = e.getSource();
if(source == produceBtn){
// do something
}
});
#1
2
the Produce object should be created using information that a user has typed in to the text fields in the same panel. So if a user typed in the vendor, weight, and price for an item into the textfields, the Produce object needs to be created with those values.
应使用用户在同一面板中的文本字段中键入的信息创建Produce对象。因此,如果用户在文本字段中键入项目的供应商,权重和价格,则需要使用这些值创建Produce对象。
First, you'll need to create some sort of list/array to hold the created objects.
首先,您需要创建某种列表/数组来保存创建的对象。
List<Produce> produce = new ArrayList<>(); // make this global within the class
then, you have two options, either overload the Produce
constructor and create another constructor to take 3 params (vendor
, weight
, and price
):
那么,你有两个选项,要么重载Produce构造函数,要么创建另一个构造函数来获取3个参数(供应商,重量和价格):
Example constructor:
public Produce(type param1, type param2, type param3){ //Constructor to take 3 params
// assign the values appropriately
}
or create setter methods:
或创建setter方法:
public void setVender(type vender){
// assign the values appropriately
}
public void setWeight(type weigth){
// assign the values appropriately
}
public void setPrice(type price){
// assign the values appropriately
}
then you can do this:
然后你可以这样做:
if(source == produceBtn){
String vender = someTextField.getText();
String weight = someTextField.getText();
String price = someTextField.getText();
//perform any conversion from string to numbers if needed.
/*
*/
//then create the object
Produce myProduce = new Produce(vender,weight,price);
// make sure the order in which you input the data into the arguments of the constructor above is the same as the order in which the constructor definition of the Produce class is.
produce.add(myProduce);
}
or you can use the setter methods.
或者你可以使用setter方法。
Lastly but not least, you seem to have a typo with addActionListener
, you're missing the closing )
.
最后但并非最不重要的是,你似乎有一个拼写错误的addActionListener,你错过了结束)。
Also, you can simplify your code by using lambda expression:
此外,您可以使用lambda表达式简化代码:
produceBtn.addActionListener(e -> {
Object source = e.getSource();
if(source == produceBtn){
// do something
}
});