我正在尝试生成包含当前日期的名称、分支机构和城市的发票编号。我从 JComboboxes 和 SimpleDateFormat 收集了信息。但最后在我们连接所有值并将它们设置为 Jtextfield 之后,我得到字符串索引超出范围异常。
我是 java 新手,对它了解不多,无论我为此付出了多少努力,如下所示。我会感谢有兴趣支持我的人。靴子和花束都受欢迎。
private void bt_generateActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == bt_generate) {
if ((cb_bkname.getSelectedItem()
.equals("<html><font color=\"red\">SELECT NAME</font></html>"))
|| (cb_brname.getSelectedItem()
.equals("<html><font color=\"red\">SELECT BRANCH</font></html>"))
|| (cb_plname.getSelectedItem()
.equals("<html><font color=\"red\">SELECT PLACE</font></html>"))
){
} else {
String datePrefix = new SimpleDateFormat("MMMM dd, YYYY")
.format(new Date());
tf_rm_dt.setText(datePrefix);
Object name = cb_bkname.getSelectedItem();
String bn = name.toString().substring(0, 3);
Object branch = cb_brname.getSelectedItem();
String br = branch.toString().substring(0, 4);
Object city = cb_bkname.getSelectedItem();
String pl = city.toString().substring(0, 4);
String curdt = new SimpleDateFormat("dd-MM-YY")
.format(new Date());
tf_rm_id.setText("" + bn + "/" + br + "/" + "/" + curdt);
}
}
}
StackTrace 如下:
run:
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.substring(String.java:1955)
at myproj.DATAENTRY.bt_generateActionPerformed(DATAENTRY.java:2215)
at myproj.DATAENTRY.access$2300(DATAENTRY.java:20)
at myproj.DATAENTRY$24.actionPerformed(DATAENTRY.java:597)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)