- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 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/
C++ Primer 说: The identifier we define in our programs may not contain 2 consecutive underscores, no
标识符术语在文档 alongside constants 中定义。 , 使用几乎相同的用例,尽管术语在运行时计算它们的值,而常量在编译时得到它。潜在地,这可能会使术语使用全局变量,但这是一个遥远而丑陋
我想知道,.Net 标识符中接受哪些字符? 不是 C# 或 VB.Net,而是 CLR。 我问这个的原因是我正在查看 yield return 语句是如何实现的 (C# In Depth),并看到
在PowerShell中,当我专门使用Active Directory时,通常会编译一个包含一组人群列表的对象,通常使用$x = get-adgroup -filter {name -like "*"
使用 hibernate 时: 我必须为每个实体指定一个 ID 或复合 ID,如果我想使用没有任何主键且没有复合键的表怎么办... 提前致谢 最佳答案 没有键的表不是一个好的关系模型。我不会推荐它。
所以我有一些代码正在尝试编译,但我不断收到此错误: 3SATSolver.java:3: expected 这是代码。我只是没有看到什么吗? import java.util.ArrayList;
我正在寻找有关 C 标准(C99 和/或 C11)部分内容的一些说明,主要是关于标识符的使用。 上下文是一个完整的C99标准库的实现,我希望它完全符合标准。 基本问题是:C 标准允许我在多大程度上声明
我有这个 Shader.h 文件,我正在用这段代码制作它: #include #include #include #include in vec2 TexCoords; out vec4 co
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
这是我的代码: #include "stdafx.h" #include #include #include #include using namespace std; int _tmain(
pthread_create() 的第一个参数是一个thread 对象,用于标识新创建的线程。但是,我不确定我是否完全理解其中的含义。 例如,我正在编写一个简单的聊天服务器并且我计划使用线程。线程会随
我想从我的标识符中获得匹配项。 我在 {/describe:foo} 中有一个这样的字符串,我正在尝试匹配 {/describe:} 以返回 foo,但我没有得到正确的正则表达式,会有人介意指出我做错
我遇到了一个奇怪的问题,我似乎找不到答案,但我想我不妨问问。 我有一个执行碰撞检查的抽象类,它有一个“更新”函数,以及“updateX”和“updateY”函数。 class MapCollidabl
我正在尝试创建一个程序来将所有文件从一个目录复制到另一个目录。但我遇到了一个基本问题。它说当我尝试在第 52 行编译时需要标识符。 public bool RecursiveCopy() {
1>cb.c(51): error C2061: syntax error : identifier 'SaveConfiguration' 1>cb.c(51): error C2059: synt
我刚刚发现命名变量 arguments 是个坏主意。 var arguments = 5; (function () { console.log(arguments); })(); Outpu
我们对我们的网站进行了安全测试,并发现了一个漏洞。 问题 If the session identifier were known by an attacker who had access to t
为了估计程序在一次内核启动中可以处理多少数据,我尝试使用 cudaMemGetInfo() 获取一些内存信息。但是,编译器告诉我: 错误:标识符“cudaMemGetInfo”未定义 cudaGetD
我发现我需要使用 xpath 查询来定位几乎是 regexp 类型的字符串,但无法看到如何管理它。我正在使用的当前查询是: $result = $xpath->query('//ul/li[sta
我正在创建我的学生计划表的虚拟版本,它基本上可以让你记下你有哪些科目的作业。 这是界面: 用户从组合框中选择主题,并在相邻的备忘录中输入一些注释。完成后,他们将单击“保存”按钮,将其保存到 .ini
我是一名优秀的程序员,十分优秀!