我的ControlGUI类出现问题。我想创建一个数组,存储在GUI JTextfield上输入的数据的整数值,并返回输入以便在另一个类中使用。我的代码是:
public class ControlGUI extends JFrame{
private TemperatureControlAgent myAgent;*emphasized text*
public static JTextField mouldTempField, meltTempField;
public ControlGUI() {
}
public static int[] getArray(){
int input[] = new int [2];
JTextField t[]=new JTextField [10];
//public TextFieldArray
JPanel panel=new JPanel(new GridLayout(5,2));
for(int i=0;i<10;i++)
{
t[i]= new JTextField(10);
panel.add(t[i]);
input[0] = Integer.parseInt(mouldTempField.getText());
input[1] = Integer.parseInt(meltTempField.getText());
System.out.println("Input mouldTempField"+ input[0]);
return input;}
ControlGUI (TemperatureControlAgent a)
{
super (a.getLocalName());
myAgent = a;
JPanel p = new JPanel();
p.setLayout(new GridLayout (3, 3));
p.add(new JLabel ("Mould Temperature Set Value: "));
mouldTempField = new JTextField (10);
p.add(mouldTempField);
p.add(new JLabel ("Melt temperature: "));
meltTempField = new JTextField (10);
p.add(meltTempField);
getContentPane().add(p, BorderLayout.CENTER);
JButton addButton = new JButton ("Enter");
addButton.addActionListener (new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
try {
String mouldTemp = mouldTempField.getText().trim();
String meltTemp = meltTempField.getText().trim();
myAgent.updateCatalogue(Integer.parseInt(mouldTemp), Integer.parseInt(meltTemp));
mouldTempField.setText("");
meltTempField.setText("");
}
catch (Exception e) {
JOptionPane.showMessageDialog(ControlGUI.this, "Invalid values. "+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
p = new JPanel();
p.add(addButton);
getContentPane().add(p, BorderLayout.SOUTH);
//Ensure termination of the agent when the user closes the GUI
//using "close" button
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
myAgent.doDelete();
}
});
setResizable(false);
}
public void showGUI(){
pack ();
Dimension screenSize;
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int CenterX = (int)screenSize.getWidth() / 2;
int CenterY = (int) screenSize.getHeight() / 2;
setLocation(CenterX - getWidth() / 2, CenterY - getHeight() / 2 );
super.setVisible(true);
}
}
我在代码中获取第39行和第40行的java.lang.NumberFormatException错误,也就是当我尝试提取JTextfield上输入的整数值时。我被困住了,不知道该怎么办。提前致谢
答案 0 :(得分:0)
发生此错误是因为其中一个JTextField
具有bot int值,因此您需要做的是捕获该异常,如:
try {
input[0] = Integer.parseInt(mouldTempField.getText().trim());//use trim to remove white spaces.
} catch(NumberFormatException e) {
//handle the exception
}
答案 1 :(得分:0)
actionPerformed()
正在您的座席上调用方法,然后清除文本字段:
String mouldTemp = mouldTempField.getText().trim();
String meltTemp = meltTempField.getText().trim();
myAgent.updateCatalogue(Integer.parseInt(mouldTemp), Integer.parseInt(meltTemp));
mouldTempField.setText(""); // no more data!
meltTempField.setText(""); // no more data!
从堆栈跟踪中,看起来TemperatureControlAgent.updateCatalogue()
在单独的线程中启动了一些分析。然后这个新线程稍后返回并作为静态方法运行ControlGUI.getArray()
,它尝试再次读取数据:
input[0] = Integer.parseInt(mouldTempField.getText());
input[1] = Integer.parseInt(meltTempField.getText());
但是回到你的GUI线程中,你已经吹走了你的数据,你现在正试图解析一个空字符串,导致NFE。