问题遇到的现象和发生背景
Java运行窗体出现101Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: setSelectedIndex: 3 out of bounds
用代码块功能插入代码,请勿粘贴截图
import java.awt.Dimension;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import com.sun.org.apache.bcel.internal.generic.*;
public class RoomFrm1 extends JFrame implements ActionListener {
public RoomFrm1() {
initComponents();
}
private void initComponents() {
Lroomid = new javax.swing.JLabel();
Troomid = new javax.swing.JTextField();
Lroomtype = new javax.swing.JLabel();
Croomtype = new javax.swing.JComboBox();
Lfoor = new javax.swing.JLabel();
Cfloor = new javax.swing.JComboBox();
Lnum = new javax.swing.JLabel();
Cnum = new javax.swing.JComboBox();
Lprice = new javax.swing.JLabel();
Cprice = new javax.swing.JComboBox();
Lflag = new javax.swing.JLabel();
Checkboxflag = new javax.swing.JCheckBox();
Lnote = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
Tnote = new javax.swing.JTextArea();
Badd = new javax.swing.JButton();
Blast = new javax.swing.JButton();
Bnext = new javax.swing.JButton();
Bedit = new javax.swing.JButton();
Bdelete = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Lroomid.setText("\u623f\u95f4\u53f7\uff1a");
Troomid.setColumns(5);
Lroomtype.setText("\u623f\u95f4\u7c7b\u578b\uff1a");
Croomtype.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"一人间", "二人间", "三人间" }));
Lfoor.setText("\u697c\u5c42\uff1a");
Cfloor.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"1", "2", "5" }));
Lnum.setText("\u53ef\u4f4f\u4eba\u6570\uff1a");
Cnum.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1",
"2", "3" }));
Lprice.setText("\u4ef7\u683c\uff1a");
Cprice.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"300", "400", "600" }));
Lflag.setText("\u623f\u95f4\u72b6\u6001\uff1a");
Checkboxflag.setText("\u7a7a\u623f");
Lnote.setText("\u5907\u6ce8\uff1a");
Tnote.setColumns(20);
Tnote.setRows(5);
jScrollPane1.setViewportView(Tnote);
Badd.setText("\u6dfb\u52a0");
Blast.setText("\u4e0a\u4e00\u4e2a");
Bnext.setText("\u4e0b\u4e00\u4e2a");
Bnext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BnextActionPerformed(evt);
}
});
Bedit.setText("\u4fee\u6539");
Bdelete.setText("\u5220\u9664");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING,
false)
.addGroup(
layout
.createSequentialGroup()
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
Lroomid)
.addComponent(
Lfoor)
.addComponent(
Lprice))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
Troomid,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
Cfloor,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
Cprice,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(
30,
30,
30)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addComponent(
Lflag)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
Checkboxflag))
.addGroup(
layout
.createSequentialGroup()
.addComponent(
Lroomtype)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
Croomtype,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(
layout
.createSequentialGroup()
.addComponent(
Lnum)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
Cnum,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(
layout
.createSequentialGroup()
.addComponent(
Lnote)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(
jScrollPane1)))
.addGroup(
layout
.createSequentialGroup()
.addComponent(
Badd)
.addGap(
18,
18,
18)
.addComponent(
Blast)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
Bnext)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
Bedit)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(
Bdelete)))
.addContainerGap(30, Short.MAX_VALUE)));
layout
.setVerticalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
Troomid,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
Lroomtype)
.addComponent(
Croomtype,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(Lroomid))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Lfoor)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
Cfloor,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
Lnum)
.addComponent(
Cnum,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Lprice)
.addComponent(
Cprice,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Lflag)
.addComponent(
Checkboxflag))
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(
66,
66,
66)
.addComponent(
Lnote))
.addGroup(
layout
.createSequentialGroup()
.addGap(
30,
30,
30)
.addComponent(
jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Badd)
.addComponent(Blast)
.addComponent(Bnext)
.addComponent(Bedit)
.addComponent(Bdelete))
.addContainerGap(32, Short.MAX_VALUE)));
//pack();
this.setLocation(200, 400);
this.setSize(600,500);
this.setTitle("酒店客房");
showfirst();
Badd.addActionListener(this);
Bdelete.addActionListener(this);
Bnext.addActionListener(this);
Blast.addActionListener(this);
Bedit.addActionListener(this);
showend();
Badd.addActionListener(this);
Bdelete.addActionListener(this);
Bnext.addActionListener(this);
Blast.addActionListener(this);
Bedit.addActionListener(this);
}
private void BnextActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RoomFrm1().setVisible(true);
}
});
}
private javax.swing.JButton Badd;
private javax.swing.JButton Bdelete;
private javax.swing.JButton Bedit;
private javax.swing.JButton Blast;
private javax.swing.JButton Bnext;
private javax.swing.JComboBox Cfloor;
private javax.swing.JCheckBox Checkboxflag;
private javax.swing.JComboBox Cnum;
private javax.swing.JComboBox Cprice;
private javax.swing.JComboBox Croomtype;
private javax.swing.JLabel Lflag;
private javax.swing.JLabel Lfoor;
private javax.swing.JLabel Lnote;
private javax.swing.JLabel Lnum;
private javax.swing.JLabel Lprice;
private javax.swing.JLabel Lroomid;
private javax.swing.JLabel Lroomtype;
private javax.swing.JTextArea Tnote;
private javax.swing.JTextField Troomid;
private javax.swing.JScrollPane jScrollPane1;
private void showfirst() {
try{
DBConnection dbc=new DBConnection();
Connection con=dbc.getConnection();
Statement sm=con.createStatement();
String sql="select * from RoomDetail order by roomId asc";
ResultSet rs=sm.executeQuery(sql);
String id="";
if(rs.next())
id=rs.getString("roomId");
System.out.print(id);
sql="select * from RoomDetail where roomId='"+id+"'";
rs=sm.executeQuery(sql);
if(rs.next()){
Troomid.setText(rs.getString("roomId"));
Croomtype.setSelectedItem(rs.getString("roomType"));
Cfloor.setSelectedItem(rs.getString("roomFloor"));
Cnum.setSelectedIndex(rs.getInt("personNum"));
Cprice.setSelectedItem(rs.getString("price"));
Tnote.setText(rs.getString("note"));
if(rs.getBoolean("flag"))
Checkboxflag.setSelected(true);
else
Checkboxflag.setSelected(false);
}
}catch(SQLException e2){
System.out.print("e2-first:"+e2);
}
}
private void showend() {
try{
DBConnection dbc=new DBConnection();
Connection con=dbc.getConnection();
Statement sm=con.createStatement();
String sql="select * from RoomDetail order by roomId ";
ResultSet rs=sm.executeQuery(sql);
String id="";
if(rs.next())
id=rs.getString("roomId");
System.out.print(id);
sql="select * from RoomDetail where roomId='"+id+"'";
rs=sm.executeQuery(sql);
if(rs.next()){
Troomid.setText(rs.getString("roomId"));
Croomtype.setSelectedItem(rs.getString("roomType"));
Cfloor.setSelectedItem(rs.getString("roomFloor"));
Cnum.setSelectedIndex(rs.getInt("personNum"));
Cprice.setSelectedItem(rs.getString("price"));
Tnote.setText(rs.getString("note"));
if(rs.getBoolean("flag"))
Checkboxflag.setSelected(true);
else
Checkboxflag.setSelected(false);
}
}catch(SQLException e2){
System.out.print("e2-first:"+e2);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Badd){//添加
if( Badd.getText().equals("添加")){
Troomid.setText("");
Croomtype.setSelectedIndex(0);
Cfloor.setSelectedIndex(0);
Cnum.setSelectedIndex(0);
Cprice.setSelectedIndex(0);
Tnote.setText("");
Checkboxflag.setSelected(true);
Badd.setText("确定");
}else{
try{
DBConnection dbc=new DBConnection();
Connection con=dbc.getConnection();
Statement sm=con.createStatement();
//roomId roomType roomFloor personNum price note1 flag
String roomid,roomtype,roomfloor,note;
int personnum,price;
roomid=Troomid.getText();
roomtype=Croomtype.getSelectedItem().toString();
roomfloor=Cfloor.getSelectedItem().toString();
personnum=Integer.parseInt(Cnum.getSelectedItem().toString().charAt(0)+"");
price=Integer.parseInt(Cprice.getSelectedItem().toString());
note=Tnote.getText();
boolean flag;
if(Checkboxflag.isSelected())
flag=true;
else
flag=false;
String sql="insert into Room values('"+roomid+"','"+roomtype+"','"+roomfloor+ "',"+personnum+","+price+",'"+note+"',"+flag+")";
System.out.println(sql);
if(sm.executeUpdate(sql)>=1)
JOptionPane.showMessageDialog(this, "房间信息添加成功!");
con.close();
}catch(SQLException e1){
System.out.println("e1-add:"+e1);
}
}
}
if(e.getSource()==Bedit){//修改
String roomid=Troomid.getText();
if(!roomid.equals("")){
try{
DBConnection dbc=new DBConnection();
Connection con=dbc.getConnection();
Statement sm=con.createStatement();
String roomtype,roomfloor,note;
int personnum,price;
boolean flag;
if(Checkboxflag.isSelected())
flag=true;
else
flag=false;
roomtype=Croomtype.getSelectedItem().toString();
roomfloor=Cfloor.getSelectedItem().toString();
personnum=Integer.parseInt(Cnum.getSelectedItem().toString().charAt(0)+"");
price=Integer.parseInt(Cprice.getSelectedItem().toString());
note=Tnote.getText();
String sql="update RoomDetail set roomType='"+roomtype+"',roomFloor='"+roomfloor+"',personNum='"+personnum+"',price='"+price+"',note='"+note+"',flag='"+flag+" where roomId='"+roomid+"'";
System.out.println(sql);
if(sm.executeUpdate(sql)>=1)
JOptionPane.showMessageDialog(this, "数据修改成功!");
con.close();
}catch(SQLException e1){
System.out.println("e1-update:"+e1);
}
}
}
if(e.getSource()==Bdelete){//删除
String roomid=Troomid.getText();
DBConnection dbc;
Connection con;
Statement sm;
String sql;
if(!roomid.equals("")){
try{
dbc=new DBConnection ();
con=dbc.getConnection();
sm=con.createStatement();
sql="delete * from Room where roomId='"+roomid+"',";
if(sm.executeUpdate(sql)>=1){
JOptionPane.showMessageDialog(this, "数据删除成功!");
showend();
}
con.close();
}catch(SQLException e1){
System.out.println("e1-delete:"+e1);
}
}
}
if(e.getSource()==Blast){
String roomid=Troomid.getText();
DBConnection dbc;
Connection con;
Statement sm;
ResultSet rs;
String sql;
if(!roomid.equals("")){
try{
dbc=new DBConnection();
con=dbc.getConnection();
sm=con.createStatement();
String roomType,roomFloor,note;
int personNum,price;
boolean flag;
sql="select * from RoomDetail where roomId<'"+roomid+"' order by roomId desc";
rs=sm.executeQuery(sql);
if(rs.next()){
Troomid.setText(rs.getString("roomId"));
Croomtype.setSelectedItem(rs.getString("roomType"));
Cfloor.setSelectedItem(rs.getString("roomFloor"));
//compersonnum.setSelectedIndex(rs.getInt("personNum")-1);
Cnum.setSelectedItem(rs.getInt("personNum")+"人");
Cprice.setSelectedItem(rs.getString("price"));
Lnote.setText(rs.getString("note"));
if(rs.getBoolean("flag"))
Checkboxflag.setSelected(true);
else
Checkboxflag.setSelected(false);
}
}catch(SQLException e1){
System.out.println("e1-pre:"+e1);
}
}
}
else{
showfirst();
}
if(e.getSource()==Bnext){
String roomid=Troomid.getText();
DBConnection dbc;
Connection con;
Statement sm;
ResultSet rs;
String sql;
if(!roomid.equals("")){
try{
dbc=new DBConnection();
con=dbc.getConnection();
sm=con.createStatement();
String roomType,roomFloor,note;
int personNum,price;
boolean flag;
sql="select * from RoomDetail where roomId>'"+roomid+"' order by roomId desc";
rs=sm.executeQuery(sql);
if(rs.next()){
Troomid.setText(rs.getString("roomId")); Croomtype.setSelectedItem(rs.getString("roomType")); Cfloor.setSelectedItem(rs.getString("roomFloor"));
//compersonnum.setSelectedIndex(rs.getInt("personNum")-1);
Cnum.setSelectedItem(rs.getInt("personNum")+"人");
Cprice.setSelectedItem(rs.getString("price"));
Lnote.setText(rs.getString("note"));
if(rs.getBoolean("flag"))
Checkboxflag.setSelected(true);
else
Checkboxflag.setSelected(false);}
}catch(SQLException e1){
System.out.println("e1-delete:"+e1);
}
}
}
}
}
运行结果及报错内容
101Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: setSelectedIndex: 3 out of bounds
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:601)
at RoomFrm1.showfirst(RoomFrm1.java:386)
at RoomFrm1.initComponents(RoomFrm1.java:324)
at RoomFrm1.<init>(RoomFrm1.java:10)
at RoomFrm1$2.run(RoomFrm1.java:344)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
我的解答思路和尝试过的
我想要达到的结果
希望大佬们可以帮我看一下!!!!谢谢!!!