使用 JTextField 和 JButton 打开一个 url
Using JTextField and JButton to open an url
我正在尝试在 JTextField、JButton 和 url 之间创建连接,如果我单击该按钮,代码会打开 url 以进行计数。我在我的一次尝试中尝试了几种方法。
我收到一个错误。如何解决?
@Override
public void actionPerformed(ActionEvent event) {
String input = textField.getText();
URL book = null;
try {
book = new URL("input");
} catch (MalformedURLException e) {
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(book.openStream(), StandardCharsets.ISO_8859_1));
} catch (IOException e) {
e.printStackTrace();
}
我收到一条很长的错误消息,这里是其中的一部分:
java.net.MalformedURLException: no protocol: input
at java.net.URL.<init>(URL.java:593)
at java.net.URL.<init>(URL.java:490)
at java.net.URL.<init>(URL.java:439)
at Main.actionPerformed(Main.java:44)
您正在尝试打开 URL "input"(字符串)而不是您从文本字段中读取的内容。请比较
book = new URL("input");
到
book = new URL(input);
我正在尝试在 JTextField、JButton 和 url 之间创建连接,如果我单击该按钮,代码会打开 url 以进行计数。我在我的一次尝试中尝试了几种方法。
我收到一个错误。如何解决?
@Override
public void actionPerformed(ActionEvent event) {
String input = textField.getText();
URL book = null;
try {
book = new URL("input");
} catch (MalformedURLException e) {
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(book.openStream(), StandardCharsets.ISO_8859_1));
} catch (IOException e) {
e.printStackTrace();
}
我收到一条很长的错误消息,这里是其中的一部分:
java.net.MalformedURLException: no protocol: input
at java.net.URL.<init>(URL.java:593)
at java.net.URL.<init>(URL.java:490)
at java.net.URL.<init>(URL.java:439)
at Main.actionPerformed(Main.java:44)
您正在尝试打开 URL "input"(字符串)而不是您从文本字段中读取的内容。请比较
book = new URL("input");
到
book = new URL(input);