Java 创建项目酒店管理系统时出错
我正在尝试创建一个项目,这是酒店管理系统。我被困在中途,因为我是新手,所以我需要你们的帮助。我正在netbeans和MySql上创建一个GUI项目Java 创建项目酒店管理系统时出错,java,user-interface,netbeans,Java,User Interface,Netbeans,我正在尝试创建一个项目,这是酒店管理系统。我被困在中途,因为我是新手,所以我需要你们的帮助。我正在netbeans和MySql上创建一个GUI项目 private void jTable2MouseClicked(java.awt.event.MouseEvent evt) { d = (DefaultTableModel)jTable2.getModel(); int selectIndex = jTab
private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {
d = (DefaultTableModel)jTable2.getModel();
int selectIndex = jTable2.getSelectedRow();
jLabel12.setText(d.getValueAt(selectIndex,0).toString());
txtname.setText(d.getValueAt(selectIndex,1).toString());
txtphone.setText(d.getValueAt(selectIndex,2).toString());
**txtcheckin.setDate(d.getValueAt(selectIndex,3));
txtcheckout.setDate(d.getValueAt(selectIndex,3));**
txtrnumber.setSelectedItem(d.getValueAt(selectIndex,5).toString());
txtrtype.setSelectedItem(d.getValueAt(selectIndex,6).toString());
txtbtype.setSelectedItem(d.getValueAt(selectIndex,7).toString());
txtamount.setText(d.getValueAt(selectIndex,8).toString());
jButton1.setEnabled(false);
}
上面有两行(用双星号括起来),其中txtcheckin属于签入日期,txtcheckout属于签出日期。基本上,我希望当我点击表格时,相关的数据显示在相关的文本字段中。签入和签出是一个jDateChooser,类型是String。
我还尝试过执行txtcheckin.setText和txtcheckout.setText,但仍然显示错误错误错误如下所示
无论如何……要获得当前必须使用的内容,鼠标单击的事件代码可能如下所示(请阅读代码中的注释):
为什么
txtcheckin
和txtcheckout
从所选表格行的同一表格列中检索日期?这是打字错误吗?错误信息非常直接:无论您从getValueOf
返回什么类型,都与setDate
所期望的类型不兼容。方钉,圆孔。顺便说一下,这两个Date
类都被JSR310中定义的现代java.time类可怕地取代了。搜索堆栈溢出以了解更多信息,因为这已经被讨论过很多次了。
private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {
// txtcheckin is a JDateChooser.
// txtcheckout is a JDateChooser.
javax.swing.table.DefaultTableModel d = (javax.swing.table.DefaultTableModel)jTable2.getModel();
// Get the selected JTable row index value.
int selectIndex = jTable2.getSelectedRow();
if (selectIndex == -1) { return; } // No JTable row selected so get outta here.
jLabel12.setText(d.getValueAt(selectIndex,0).toString());
txtname.setText(d.getValueAt(selectIndex,1).toString());
txtphone.setText(d.getValueAt(selectIndex,2).toString());
/* Get the dates contained in column 4 (index 3) and column 5 (index 4)
of the selected JTable row. To retrieve the data from these table
columns as string you need to use the 'toString()' method since the
getValueAt() method returns an Object even if the table model has
these columns set to String. */
String dateIN = d.getValueAt(selectIndex, 3).toString();
String dateOUT = d.getValueAt(selectIndex, 4).toString();
// Apply the date String from JTable cells to JDateChoosers Text Fields.
java.util.Date dateToSetIN;
java.util.Date dateToSetOUT;
// A Try/Catch must be used here due to parsing.
try {
/* The date format supplied to the SimpleDateFormat constructor
must be the same format that is used within the JTable cell.
However, the date format set in the JDateChooser's dateFormatString
property can be whatever format you like to be displayed within
its own Text Field. A similar format would be preferable. */
// Make sure there is actually a date string (the cell could be empty)
if (!dateIN.isEmpty()) {
dateToSetIN = new SimpleDateFormat("MMM dd, yyyy").parse(dateIN);
/* Ensure similar format in JDateChooser Text Field. This is
optional and can be removed! */
txtcheckin.setDateFormatString("MMM dd, yyyy");
// Set the selected JTable Date into JDateChooser...
txtcheckin.setDate(dateToSetIN);
}
else {
/* Clear the JDateChooser Text Field since there is
no date in the selected row anyways. */
txtcheckin.setDate(null);
}
// Make sure there is actually a date string (the cell could be empty)
if (!dateOUT.isEmpty()) {
dateToSetOUT = new SimpleDateFormat("MMM dd, yyyy").parse(dateOUT);
/* Ensure similar format in JDateChooser Text Field. This is
optional and can be removed! */
txtcheckout.setDateFormatString("MMM dd, yyyy");
// Set the selected JTable Date into JDateChooser...
txtcheckout.setDate(dateToSetOUT);
}
else {
/* Clear the JDateChooser Text Field since there is
no date in the selected row anyways. */
txtcheckout.setDate(null);
}
}
catch (ParseException ex) {
// Display error in Console.
System.err.println("Error Setting Date To Date Chooser!"
+ System.lineSeparator() + ex.getMessage());
}
txtrnumber.setSelectedItem(d.getValueAt(selectIndex,5).toString());
txtrtype.setSelectedItem(d.getValueAt(selectIndex,6).toString());
txtbtype.setSelectedItem(d.getValueAt(selectIndex,7).toString());
txtamount.setText(d.getValueAt(selectIndex,8).toString());
jButton1.setEnabled(false);
}