我有一个JButton
的ActionListener
(我正在使用WidgetViewer)。这是我的代码:
import java.awt.event.Actionlistener;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Interface {
public static void main(String[] args) {
WidgetViewer wv = new WidgetViewer();
JLabel eventName = new JLabel("Event Name");
wv.add(eventName, 10, 30, 300, 20);
JTextField name = new JTextField();
wv.add(name, 10, 50, 300, 20);
// Similar for eventVenue, venueCapacity, eventDate, and on
JButton create = new JButton("Create an Event");
wv.add(create, 10, 320, 300, 20);
create.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
new Event();
String eventName = name.getText();
String eventVenue = venue.getText();
int venueCapacity = Integer.parseInt(capacity);
String eventDate = date.getText();
int ticketsSold = Integer.parseInt(sold);
int ticketPrice = Integer.parseInt(price);
int overhead = Integer.parseInt(costs);
}
});
}
}
我在另一个Java文件中有一个名为Event的类。在这个ActionListener
中,我希望创建对象并保存输入。我无法将字符串输入解析为整数,并且对于使用Integer.parseInt()的字符串输入,我会出现错误“类型为整数的parseInt()方法不适用于参数(JTextField)”;
发布于 2020-07-18 17:37:55
您应该正在解析JTextField
%s的文本。
int venueCapacity = Integer.parseInt(capacity.getText());
String eventDate = date.getText();
int ticketsSold = Integer.parseInt(sold.getText());
int ticketPrice = Integer.parseInt(price.getText());
int overhead = Integer.parseInt(costs.getText());
发布于 2020-07-18 17:44:35
从您得到的异常中,我认为您应该首先将这些变量转换为字符串。例如
int capacitystr = capacity.getText();
然后你可以将它们转换成
int venueCapacity = Integer.parseInt(capacitystr);
https://stackoverflow.com/questions/62971650
复制相似问题