Java 重写tostring()
我实现了一个基于给定GUI的类,我试图重写tostring(),以便在GUI上显示信息,但我无法让它正常工作 图形用户界面代码Java 重写tostring(),java,Java,我实现了一个基于给定GUI的类,我试图重写tostring(),以便在GUI上显示信息,但我无法让它正常工作 图形用户界面代码 private void updateDisplay() { try { if (game.isAccepted()) { String str = "Selected Applicant:\n\n" + currentApplicant.toString() + "\n\nwas
private void updateDisplay() {
try {
if (game.isAccepted()) {
String str = "Selected Applicant:\n\n"
+ currentApplicant.toString() + "\n\nwas ";
if (game.isBestApplicant()) {
str += "the BEST. You Win!";
} else {
str += "not the best available. You Lose.\n\nBest applicant was: \n\n"
+ game.getBestApplicant();
}
display.setText(str);
showNewGameControls();
} else {
display.setText("Current applicant is:\n\n"
+ currentApplicant.toString()
+ "\n\nDo you wish to accept or reject?");
showCurrentGameControls();
}
} catch (HiringException e) {
display.setText(e.getMessage());
} catch (Exception e) {
display.setText("Unandled Exception: " + e.toString());
throw new RuntimeException(e);
}
}
对于我正在实现的类,我编写了以下方法
public String tostring(){
return currentApplicant;
}
您需要使用
toString
,而不是toString
,因为Java区分大小写。因此:
public String toString()
tostring在技术上与Java中的tostring不同。注意拼写。如果编辑器支持,请使用
@Override
注释@Override
注释可防止您无法重写正确的方法 还有什么不起作用?您是否有任何错误?请尝试使用toString
。大小写很重要。Java是区分大小写的:尝试toString()
是否正确扩展了超类?如果要获取在JLabel
或JTextField
中显示的类信息,应使用.setText(CurrentApplicator)
。