gpt4 book ai didi

java - KeyListener/KeyListener 方法的标识符预期错误

转载 作者:行者123 更新时间:2023-11-30 03:12:28 27 4
gpt4 key购买 nike

我正在用 Java 开发一个非常非常简单的互联网浏览器。我已经把大部分内容都写下来了,但我在获取“按 Enter 键发送 URL”部分时遇到了困难。具体来说,当我使用 keyTyped() 方法时,我收到此错误:

Browser.java:34: 错误: ';'预期的
公共(public)无效keyTyped(KeyEvent e){
^
Browser.java:34: 错误: ';'预期的
公共(public)无效keyTyped(KeyEvent e){
^

我原本以为我在某个地方错过了几个“}”和“{”标签,但我检查了我的代码,一切似乎都在它应该在的地方,所以我被难住了。

我的代码:

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.*;
import java.net.*;
import java.io.*;

public class Browser extends JFrame {
public Browser(){
setTitle("The next Google Chrome");
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
setSize(600,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new BrowserPanel());
setVisible(true);
}

public String URL;

class BrowserPanel extends JPanel implements KeyListener{
public BrowserPanel()
{
JTextField field = new JTextField(20);
field.addKeyListener(this);
JTextArea area = new JTextArea(30, 50);
JTextArea dummy = new JTextArea();
JScrollPane scroll = new JScrollPane(area);
add(field);
add(scroll);
String response;
String checkTitle = "YA GOOFED.";
String mainBody = "REAL BAD.";

@Override
public void keyTyped(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
try
{
URL = field.getText();
String ind = "http://";
int check = URL.indexOf("http://");
int index = ind.length();
String sub = URL.substring(index);
String filepath = sub.substring(sub.lastIndexOf("/"));
String webaddress = URL.substring(index, URL.lastIndexOf("/"));
if(check == -1)
area.append("This is not a proper URL.\n Please add 'http://' to the beginning of your address and try again.");
else
{
Socket s = new Socket(webaddress, 80);
PrintWriter send = new PrintWriter(s.getOutputStream());
BufferedReader read = new BufferedReader(new InputStreamReader(s.getInputStream()));
send.print("GET " + filepath + " HTTP/1.1\r\n");
send.print("Host: " + webaddress + "\r\n");
send.print("\r\n");
send.flush();
while((response = read.readLine())!= null)
{
dummy.append(response + "\n");
}
checkTitle = dummy.getText();
int title1 = checkTitle.indexOf("<title>");
int title2 = checkTitle.indexOf("</title>");
int body1 = checkTitle.indexOf("<body class="+"\"innerpage\""+">");
int body2 = checkTitle.indexOf("</body>");
String preMainBody = checkTitle.substring(body1, body2);
String preTitle = checkTitle.substring(title1 , title2);
String title = preTitle.substring(7);
mainBody = preMainBody.replaceAll("\\<[^>]*>","");
area.append(mainBody);
setTitle(title);
System.out.println(title);
}
}
catch (IOException i)
{
i.printStackTrace();
}
}
}
}
}

public static void main (String[] args){
new Browser();
}
}

最佳答案

您目前正在有效地尝试在构造函数内声明这些方法。

在方法声明开始之前,您需要一个结束 } 。看起来您已经平衡了大括号,因此您可能还需要从类末尾删除一个大括号。

很难确切地知道您的意图,但我怀疑它会在 add(scroll)String response 行之间。

关于java - KeyListener/KeyListener 方法的标识符预期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33340865/

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