我有一个超类Car
,以及两个扩展Car
(CarToRent
和CarToSell
)的类
在类CarCompany
中,我在构造函数中实现了一个GUI。
我有一个按钮:
addCarToRentButton = new JButton("Add Car To Rent");
contentPane.add(addCarToRentButton);
addCarToRentButton.addActionListener(this);
如何添加动作侦听此功能,以便在单击按钮时,它会将3个文本字段中的数据输入到Car
类的数组中? (文本字段为dailyRateTextField
,descriptionTextField
和downPaymentTextField
)。
答案 0 :(得分:2)
答案 1 :(得分:1)
要首先浏览Java桌面应用程序,您需要执行以下操作:
以上所有内容都要记住一些Java知识。
答案 2 :(得分:0)
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//action (someCar.setSomeValue(textField1.getText());
}
});
答案 3 :(得分:0)
据推测,您的CarCompany
类实现了ActionListener
,因此有一个方法;
@Override
public void actionPerformed(ActionEvent evt) {
}
单击该按钮时,将调用此方法。您可能想要检查事件的来源是否为addCarToRentButton
,如果是,您可以从字段中获取文本,创建Car
并将其放入数组中。
String rate = dailyRateTextField.getText();
String desc = descriptionTextField.getText();
String payment = downPaymentTextField.getText();
//Instantiate car
//add to array
答案 4 :(得分:0)
要编写动作侦听器,请按照以下步骤操作:
1。声明一个事件处理程序类,并指定该类实现ActionListener接口或扩展实现ActionListener接口的类。例如:
public class CarCompany implements ActionListener {
2. 在一个或多个组件上注册事件处理程序类的实例作为侦听器。例如:
addCarToRentButton.addActionListener(this);
3. 包含在侦听器界面中实现方法的代码。例如:
public void actionPerformed(ActionEvent e) {
...//code that reacts to the action...
}