代码如下,想给自己的网络爬虫写一个图形界面,在图像界面上输入根网址和深度问题是:代码报错,不知道为什么图形界面的值无法读入带变量中packageztest9;importjavax.swing.*;import...
代码如下,想给自己的网络爬虫写一个图形界面,在图像界面上输入根网址和深度
问题是:代码报错,不知道为什么图形界面的值无法读入带变量中
package ztest9;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ztest9 extends JFrame{
private JFrame frame;
private JButton button;
private JTextArea textArea;
private JTextField textField;
private JComboBox comboBox;
public static int webDepth;
public static String strHomePage;
public ztest9(String theTitle){
//super(theTitle);
frame=new JFrame(theTitle);
Container contentPane=frame.getContentPane();
contentPane.setLayout(new FlowLayout());
textArea=new JTextArea(1,20);
textArea.setText("输入根地址:");
textField=new JTextField(20);
String str[]={"1","2","3"};
comboBox=new JComboBox(str);
button=new JButton("提交");
contentPane.add(textArea);
contentPane.add(textField);
contentPane.add(comboBox);
contentPane.add(button);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.exit(0);
}
});
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
webDepth=(int)comboBox.getSelectedItem();
strHomePage=textField.getText();
}
});
//调试代码,想查看输出
System.out.println(strHomePage);
System.out.println(webDepth);
}
public static void main(String args[]){
new ztest9("阳哥");
}
}
运行出的界面如下:
代码行中没错,是控制台中输出很多错误:
null
0
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at ztest9.ztest9$2.actionPerformed(ztest9.java:43)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
······
其中43行是 点击事件中的语句 webDepth=(int)comboBox.getSelectedItem();
我刚学JAVA,也是刚用eclipse,不知道这些是什么意思···十分感谢···
展开