这是我在主界面上添加的一个JComboBox private void initComb(){
String name = (String)cmbGreatname.getSelectedItem();
Vector v = S_CateDao.select(name);
for(int i=0;i<v.size();i++){
S_Cate s = (S_Cate)v.get(i);
cmbSmallname.addItem(s);
}
运行却报异常 java.lang.ClassCastException以下是和上面有关的类
import java.awt.*;import javax.swing.*;
import java.util.Vector;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.border.TitledBorder;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class AssetInsertDialog extends JDialog {
public AssetInsertDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
setSize(350,400);
} catch (Exception exception) {
exception.printStackTrace();
}
} public AssetInsertDialog() {
this(new Frame(), "AssetInsertDialog", false);
} private void jbInit() throws Exception {
jPanel1.setPreferredSize(new Dimension(350, 400));
jPanel1.setToolTipText("");
jPanel1.setLayout(null);
initCombo();
initComb();
jPanel2.setPreferredSize(new Dimension(350, 400));
jPanel3.setBackground(SystemColor.activeCaptionBorder);
jPanel3.setPreferredSize(new Dimension(350, 400));
jLabel1.setBorder(titledBorder1);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("资产编号");
jLabel1.setBounds(new Rectangle(23, 47, 74, 27));
txtAId.setEnabled(false);
txtAId.setDoubleBuffered(true);
txtAId.setEditable(false);
txtAId.setBounds(new Rectangle(111, 46, 99, 29));
jLabel2.setBorder(titledBorder2);
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("资产名称");
jLabel2.setBounds(new Rectangle(25, 90, 74, 27));
txtAName.setBounds(new Rectangle(111, 89, 249, 29));
cmbGreatname.setBounds(new Rectangle(106, 146, 94, 23));
cmbSmallname.setBounds(new Rectangle(289, 149, 94, 23));
jLabel3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setText("大类");
jLabel3.setBounds(new Rectangle(19, 141, 74, 27));
jLabel4.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setText("小类");
jLabel4.setBounds(new Rectangle(208, 143, 74, 27));
this.getContentPane().add(jTabbedPane1, java.awt.BorderLayout.NORTH);
jTabbedPane1.setPreferredSize(new Dimension(350, 400));
jTabbedPane1.add(jPanel1, "添加资产");
jPanel1.add(jLabel1);
jPanel1.add(txtAId);
jPanel1.add(jLabel2);
jPanel1.add(txtAName);
jPanel1.add(cmbGreatname);
jPanel1.add(jLabel3);
jPanel1.add(cmbSmallname);
jPanel1.add(jLabel4);
jTabbedPane1.add(jPanel2, "修改资产");
jTabbedPane1.add(jPanel3, "删除资产");
}
private void initCombo(){
Vector v = CateDao.select();
for(int i=0;i<v.size();i++){
Cate c = (Cate)v.get(i);
cmbGreatname.addItem(c);
}
}
private void initComb(){
String name = (String)cmbGreatname.getSelectedItem();
Vector v = S_CateDao.select(name);
for(int i=0;i<v.size();i++){
S_Cate s = (S_Cate)v.get(i);
cmbSmallname.addItem(s);
}
}
JTabbedPane jTabbedPane1 = new JTabbedPane();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JLabel jLabel1 = new JLabel();
TitledBorder titledBorder1 = new TitledBorder("");
JTextField txtAId = new JTextField();
JLabel jLabel2 = new JLabel();
TitledBorder titledBorder2 = new TitledBorder("");
JTextField txtAName = new JTextField();
JComboBox cmbGreatname = new JComboBox();
JComboBox cmbSmallname = new JComboBox();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
}import java.sql.*;
import java.util.*;public class S_CateDao {
public S_CateDao() {
}
public static Vector select(String name){
Vector v = new Vector();
try {
Connection con = DBC.getCon(); String sql = "select sname"
+" from great,small"
+" where great.gid = small.gid and gname = ?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1,name);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
S_Cate s = new S_Cate();
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
v.add(s);
}
rs.close();
pst.close();
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return v;
}
public class S_Cate {
private int sid;
private String sname; public S_Cate() {
} public void setSid(int sid) {
this.sid = sid;
} public void setSname(String sname) {
this.sname = sname;
} public int getSid() {
return sid;
} public String getSname() {
return sname;
}
public String toString(){
return sname;
}
}请各位大大指点下
String name = (String)cmbGreatname.getSelectedItem();
Vector v = S_CateDao.select(name);
for(int i=0;i<v.size();i++){
S_Cate s = (S_Cate)v.get(i);
cmbSmallname.addItem(s);
}
运行却报异常 java.lang.ClassCastException以下是和上面有关的类
import java.awt.*;import javax.swing.*;
import java.util.Vector;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.border.TitledBorder;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class AssetInsertDialog extends JDialog {
public AssetInsertDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
setSize(350,400);
} catch (Exception exception) {
exception.printStackTrace();
}
} public AssetInsertDialog() {
this(new Frame(), "AssetInsertDialog", false);
} private void jbInit() throws Exception {
jPanel1.setPreferredSize(new Dimension(350, 400));
jPanel1.setToolTipText("");
jPanel1.setLayout(null);
initCombo();
initComb();
jPanel2.setPreferredSize(new Dimension(350, 400));
jPanel3.setBackground(SystemColor.activeCaptionBorder);
jPanel3.setPreferredSize(new Dimension(350, 400));
jLabel1.setBorder(titledBorder1);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("资产编号");
jLabel1.setBounds(new Rectangle(23, 47, 74, 27));
txtAId.setEnabled(false);
txtAId.setDoubleBuffered(true);
txtAId.setEditable(false);
txtAId.setBounds(new Rectangle(111, 46, 99, 29));
jLabel2.setBorder(titledBorder2);
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("资产名称");
jLabel2.setBounds(new Rectangle(25, 90, 74, 27));
txtAName.setBounds(new Rectangle(111, 89, 249, 29));
cmbGreatname.setBounds(new Rectangle(106, 146, 94, 23));
cmbSmallname.setBounds(new Rectangle(289, 149, 94, 23));
jLabel3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setText("大类");
jLabel3.setBounds(new Rectangle(19, 141, 74, 27));
jLabel4.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setText("小类");
jLabel4.setBounds(new Rectangle(208, 143, 74, 27));
this.getContentPane().add(jTabbedPane1, java.awt.BorderLayout.NORTH);
jTabbedPane1.setPreferredSize(new Dimension(350, 400));
jTabbedPane1.add(jPanel1, "添加资产");
jPanel1.add(jLabel1);
jPanel1.add(txtAId);
jPanel1.add(jLabel2);
jPanel1.add(txtAName);
jPanel1.add(cmbGreatname);
jPanel1.add(jLabel3);
jPanel1.add(cmbSmallname);
jPanel1.add(jLabel4);
jTabbedPane1.add(jPanel2, "修改资产");
jTabbedPane1.add(jPanel3, "删除资产");
}
private void initCombo(){
Vector v = CateDao.select();
for(int i=0;i<v.size();i++){
Cate c = (Cate)v.get(i);
cmbGreatname.addItem(c);
}
}
private void initComb(){
String name = (String)cmbGreatname.getSelectedItem();
Vector v = S_CateDao.select(name);
for(int i=0;i<v.size();i++){
S_Cate s = (S_Cate)v.get(i);
cmbSmallname.addItem(s);
}
}
JTabbedPane jTabbedPane1 = new JTabbedPane();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JLabel jLabel1 = new JLabel();
TitledBorder titledBorder1 = new TitledBorder("");
JTextField txtAId = new JTextField();
JLabel jLabel2 = new JLabel();
TitledBorder titledBorder2 = new TitledBorder("");
JTextField txtAName = new JTextField();
JComboBox cmbGreatname = new JComboBox();
JComboBox cmbSmallname = new JComboBox();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
}import java.sql.*;
import java.util.*;public class S_CateDao {
public S_CateDao() {
}
public static Vector select(String name){
Vector v = new Vector();
try {
Connection con = DBC.getCon(); String sql = "select sname"
+" from great,small"
+" where great.gid = small.gid and gname = ?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1,name);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
S_Cate s = new S_Cate();
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
v.add(s);
}
rs.close();
pst.close();
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return v;
}
public class S_Cate {
private int sid;
private String sname; public S_Cate() {
} public void setSid(int sid) {
this.sid = sid;
} public void setSname(String sname) {
this.sname = sname;
} public int getSid() {
return sid;
} public String getSname() {
return sname;
}
public String toString(){
return sname;
}
}请各位大大指点下
解决方案 »
- 关于native方法的定义头,帮我选择下。
- 怎么用java语言判断登录邮箱成功啊
- 问个AJAX解析返回的responseXML,已解决,不过搞不懂
- long怎么转换为int,为什么提示long cannot be dereferenced
- 有创业想法的朋友来这里QQ群:10496416(创业中国)
- 比较烦的问题..
- 我想请问在xml数据格式(在内存中非xml文件)和java对象&集合之间互相转换的比较好的方法???
- 写了一个背包问题的算法,请斧正!
- 数据算法,寻求高手解答!高手,高手,高手..........高手在哪??
- 用JBuilder能将java程序生成exe可执行文件吗?如何生成?
- 找工作的问题!
- 跪求 des 标准算法的 java 实现
取出来就应该cast成S_Cate,而不是String
实际上里面是一个Vector维护的