gpt4 book ai didi

java - Java中按钮点击时读取URL未报告IO编译错误

转载 作者:行者123 更新时间:2023-12-02 00:10:08 25 4
gpt4 key购买 nike

虽然我构造了 try&catch 语句,但我收到了未报告的 IOException 错误。

此小程序尝试调用 phpscript 。所以我必须在单击按钮时读取 URL。

我是java初学者。你能解释一下哪里出了问题

错误44:必须捕获或声明抛出IO异常

URLConnection myURLConnection = myURL.openConnection();

错误46:必须捕获或声明抛出IO异常

myURLConnection.connect();

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class RegisterUser extends Applet{


//Applet components
TextField panel1 = new TextField(10);
TextField panel2 = new TextField(10);
TextField panel3 = new TextField(10);
Button save = new Button("Save");



public void init(){
//There is two text fields and a button
add(panel1);
add(new Label("Name:"));
addNewLine();
add(panel2);
add(new Label("Last:"));
addNewLine();
add(save);
addNewLine();

// Now tell the button what it should do when it clicked

save.addActionListener(new SaveListener());
}

class SaveListener implements ActionListener{

public void actionPerformed(ActionEvent event) {

try
{

URL myURL = new URL("http://www.myplace.com/save.php?name="+panel1.getText()+"&last_name="+panel2.getText()+"&email="+panel3.getText());

URLConnection myURLConnection = myURL.openConnection();

myURLConnection.connect();

}

}
catch (MalformedURLException e){
System.out.println(e.getMessage());
}
}
}


private void addHorizontalLine(Color c)
{
// Add a Canvas 10000 pixels wide but only 1 pixel high, which acts as
// a horizontal line to separate one group of components from the next.
Canvas line = new Canvas( );
line.setSize(10000,1);
line.setBackground(c);
add(line);
}


private void addNewLine( )
{
// Add a horizontal line in the background color. The line itself is
// invisible, but it serves to force the next Component onto a new line.
addHorizontalLine(getBackground( ));
}

}

最佳答案

你必须像这样捕获已检查的IOException

catch (IOException exc) {
// handle exception
}

在您的 try catch 中,您仅捕获 MalformedURLException,这就是您收到编译错误的原因。

关于java - Java中按钮点击时读取URL未报告IO编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13028972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com