gpt4 book ai didi

java - 用 Java 创建 Web 浏览器

转载 作者:行者123 更新时间:2023-12-01 10:42:32 25 4
gpt4 key购买 nike

我想知道是否有人可以帮助我找出我的代码有什么问题。我正在尝试创建一个网络浏览器后退和前进按钮。我使用 Java“Deque”(类似于具有更多功能的堆栈)来跟踪当前和以前访问的页面。问题是经过多次调试后我不知道出了什么问题。由于某种原因,订单没有保留,我不明白为什么。任何帮助表示赞赏。到目前为止,该代码仅适用于超链接,但如果我能让它工作,那么当用户手动输入 URL 时,很容易让其他部分工作。谢谢:)

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;

/**
* This class includes Jbuttons, JTextFields, ScrollPane and View
* Created by Nash on 12/16/2015.
*/
public class Controller extends JPanel implements ActionListener, HyperlinkListener {

private JButton button;
private JButton back;
private JButton front;
private JTextField addressBar;
private JScrollPane scrollBar;
private View view;
private int padding = 5;
private String current = "https://www.kth.se";
private Deque<String> historyBack = new ArrayDeque<>();
private Deque<String> historyFront = new ArrayDeque<>();


/**
* Constructor name: Controller
* Includes: all the buttons
* actionPerformed method
* hyperlinkUpdate method
* @param v
*/
public Controller(View v){

this.view = v;

this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

/* Back button */
back = new JButton("<");
back.setName("back");
back.setEnabled(false);
back.addActionListener(this);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 0;
c.ipady = padding;
this.add(back, c);

/* Front button */
front = new JButton(">");
front.setName("front");
front.setEnabled(false);
front.addActionListener(this);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 1;
c.gridy = 0;
c.ipady = padding;
this.add(front, c);

/* URL address bar */
addressBar = new JTextField();
addressBar.addActionListener(this);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.weighty = 0.0;
c.gridx = 2;
c.gridy = 0;
this.add(addressBar, c);

/* Go button */
button = new JButton("Go!");
button.addActionListener(this);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 3;
c.gridy = 0;
c.ipady = padding;
this.add(button, c);

this.view.addHyperlinkListener(this);

scrollBar = new JScrollPane(v);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 4;
c.insets = new Insets(10,0,10,0);
this.add(scrollBar, c);

try{
view.setPage(current);
addressBar.setText(current);
}catch(IOException e){
System.out.println("Error: " + e.toString());
}

}

/**
* Method name: actionPerformed
* This method sets the page from the user given URL address
* @param e
*/

public void actionPerformed(ActionEvent e){
if(e.getSource() == back){
if(!historyBack.isEmpty()) {
goBack();
}else{
back.setEnabled(false);
}
} else if(e.getSource() == front){
if(!historyFront.isEmpty()) {
goFront();
}else{
front.setEnabled(false);
}
} else {
try {
// I can fix this later after the hyperlinks work! Skip this
current = addressBar.getText();
System.out.println("n: " + current); // Print URL
view.setPage(current);
addressBar.setText(current);
historyBack.addFirst(view.getPage().toString());

}catch(IOException arg0){
view.setText("404 error... PAGE NOT FOUND!");

System.out.println("error " + arg0.toString());
}
}
}


/**
* Method name: hyperlinkUpdate
* This method sets the page to the clicked hyperlink address
* @param e
*/
public void hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
try {
current = e.getURL().toExternalForm();
view.setPage(current);
addressBar.setText(current);
System.out.println("back saved & front cleared: " + view.getPage().toString());
historyBack.addFirst(view.getPage().toString());
historyFront.clear();
if(!historyBack.isEmpty()) {
back.setEnabled(true);
}else{
back.setEnabled(false);
}
if(!historyFront.isEmpty()) {
front.setEnabled(true);
}else{
front.setEnabled(false);
}
}catch(IOException arg0){
System.out.println("error " + arg0.toString());
}
}
}

public void goBack(){
try{
if(!historyBack.isEmpty()) {
String removed = historyBack.removeFirst();
view.setPage(removed);
addressBar.setText(removed);
historyFront.add(view.getPage().toString());
System.out.println("front saved: " + view.getPage().toString());
if(!historyBack.isEmpty()) {
back.setEnabled(true);
}else{
back.setEnabled(false);
}
if(!historyFront.isEmpty()) {
front.setEnabled(true);
}else{
front.setEnabled(false);
}
}
} catch(IOException arg0){
System.out.println("Error: " + arg0.toString());
}
}

public void goFront(){
try{
if(!historyFront.isEmpty()) {
String removed = historyFront.removeFirst();
view.setPage(removed);
addressBar.setText(removed);
historyBack.add(view.getPage().toString());
System.out.println("back saved: " + view.getPage().toString());
if(!historyFront.isEmpty()) {
front.setEnabled(true);
}else{
front.setEnabled(false);
}
if(!historyBack.isEmpty()) {
back.setEnabled(true);
}else{
back.setEnabled(false);
}

}
} catch(IOException arg0){
System.out.println("Error: " + arg0.toString());
}
}

}

最佳答案

historyBackhistoryFront 都应该像堆栈一样工作,如下所示:

  • 单击链接时,会将当前页面插入“后”堆栈,清除“前”堆栈,并显示新页面。

  • 单击后退按钮时,会将当前页面插入“前”堆栈,从“后”堆栈弹出到顶部值,然后显示该页面。

  • 单击前进按钮时,会将当前页面插入“后”堆栈,从“前”堆栈弹出到顶部值,然后显示该页面。

如您所见,这是纯粹的堆栈操作。

Deque具有堆栈方法 push()pop(),实现与 addFirst()removeFirst() 相同>。您可以直接调用它们,但使用 push()pop() 有助于阐明堆栈功能。

或者,您可以调用 addLast()removeLast(),其中 add() 同义addLast()

如果您想要堆栈功能,您不能做的就是混合它们,而您却做到了。

搜索代码以使用 historyBack 显示:

historyBack.isEmpty()
historyBack.addFirst()
historyBack.removeFirst()
historyBack.add() <==== WRONG

搜索代码以使用 historyFront 显示:

historyFront.isEmpty()
historyFront.clear()
historyFront.add() <==== WRONG
historyFront.removeFirst()

你正在混合它们!!!改为使用push()pop(),你就不会意外地混合它们了。

关于java - 用 Java 创建 Web 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34368991/

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