我照书上打的,可是编译就是错!请指点!!! ( 积分: 100 )

  • 主题发起人 主题发起人 gxhu
  • 开始时间 开始时间
G

gxhu

Unregistered / Unconfirmed
GUEST, unregistred user!
我用的是jbuilder8.0,就两个文件,请帮忙看看。
Frame1.java原代码如下:
package URLDemo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.io.*;
import java.net.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
Label label1 = new Label();
TextField textField1 = new TextField();
Button button1 = new Button();
TextArea textArea1 = new TextArea();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
label1.setText("URL地址");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
button1.setLabel("读入");
button1.addActionListener(new Frame1_button1_actionAdapter(this));
textField1.setText("");
textArea1.setText("");
contentPane.add(label1, new XYConstraints(52, 34, 35, 22));
contentPane.add(textField1, new XYConstraints(95, 34, 117, 24));
contentPane.add(button1, new XYConstraints(267, 39, 53, 23));
contentPane.add(textArea1, new XYConstraints(40, 73, 308, 204));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void button1_actionPerformed(ActionEvent e) {
try {
URL url = new URL(textField1.getText());
String inputLine = "";
InputStream is = url.openStream();
InputStream bis = new BufferedInputStream(is);
InputStreamReader r = new InputStreamReader(bis);
int c;
while ( (c = r.read()) != 1) {
inputLine += (char) c;
textArea1.setText(inputLine);
bis.close();
}
catch (MalformedURLException e) {
e.parintStackTrace();
}
}
}
}
class Frame1_button1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_button1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
URLDemo.java原代码如下:
package URLDemo;
import javax.swing.UIManager;
import java.awt.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class URLDemo {
boolean packFrame = false;
//Construct the application
public URLDemo() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else
{
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height >
screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width >
screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new URLDemo();
}
}
错误如下:
"Frame1.java": Error #: 212 : 'catch' without 'try' at line 76, column 7
"Frame1.java": Error #: 209 : 'try' without 'catch' or 'finally' at line 64, column 5
"URLDemo.java": Error #: 300 : class Frame1 not found in class URLDemo.URLDemo at line 20, column 5
"URLDemo.java": Error #: 300 : class Frame1 not found in class URLDemo.URLDemo at line 20, column 24
 
我用的是jbuilder8.0,就两个文件,请帮忙看看。
Frame1.java原代码如下:
package URLDemo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.io.*;
import java.net.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
Label label1 = new Label();
TextField textField1 = new TextField();
Button button1 = new Button();
TextArea textArea1 = new TextArea();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
label1.setText("URL地址");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
button1.setLabel("读入");
button1.addActionListener(new Frame1_button1_actionAdapter(this));
textField1.setText("");
textArea1.setText("");
contentPane.add(label1, new XYConstraints(52, 34, 35, 22));
contentPane.add(textField1, new XYConstraints(95, 34, 117, 24));
contentPane.add(button1, new XYConstraints(267, 39, 53, 23));
contentPane.add(textArea1, new XYConstraints(40, 73, 308, 204));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void button1_actionPerformed(ActionEvent e) {
try {
URL url = new URL(textField1.getText());
String inputLine = "";
InputStream is = url.openStream();
InputStream bis = new BufferedInputStream(is);
InputStreamReader r = new InputStreamReader(bis);
int c;
while ( (c = r.read()) != 1) {
inputLine += (char) c;
textArea1.setText(inputLine);
bis.close();
}
catch (MalformedURLException e) {
e.parintStackTrace();
}
}
}
}
class Frame1_button1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_button1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
URLDemo.java原代码如下:
package URLDemo;
import javax.swing.UIManager;
import java.awt.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class URLDemo {
boolean packFrame = false;
//Construct the application
public URLDemo() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else
{
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height >
screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width >
screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new URLDemo();
}
}
错误如下:
"Frame1.java": Error #: 212 : 'catch' without 'try' at line 76, column 7
"Frame1.java": Error #: 209 : 'try' without 'catch' or 'finally' at line 64, column 5
"URLDemo.java": Error #: 300 : class Frame1 not found in class URLDemo.URLDemo at line 20, column 5
"URLDemo.java": Error #: 300 : class Frame1 not found in class URLDemo.URLDemo at line 20, column 24
 
package URLDemo;
URLDemo 必须在URLDemo目录下
另外注意大小写,以及try..catch是否匹配
 
谢谢您的回答!
我反复看了看,try..catch是匹配的呀,但是它老提示不匹配,怎么回事啊?
请帮我看看!
 
补充一下:
我把void button1_actionPerformed(ActionEvent e)里面的代码屏蔽掉就可以编译运行。
所以问题出在这里面的代码,可我就是查不出错在哪?
 
void button1_actionPerformed(ActionEvent e) {
try {
URL url = new URL(textField1.getText());
String inputLine = "";
InputStream is = url.openStream();
InputStream bis = new BufferedInputStream(is);
InputStreamReader r = new InputStreamReader(bis);
int c;
while ( (c = r.read()) != 1) {
inputLine += (char) c;
textArea1.setText(inputLine);
bis.close();
}
catch (MalformedURLException e) {
e.parintStackTrace();
}
}
}
}
是否多了很多 } ?
 
void button1_actionPerformed(ActionEvent e) 这个方法中的catch上面的大括号}是对应while()的大括号的,少了一个大}.对于你后面的错误看看import的路径对否或者是否存在fram1.class
 
首先谢谢大家的回答!
我已经找出问题了。问题出在异常处理。本题除了要俘获MalformedURLException异常外还要俘获IOException异常。
 
后退
顶部