我想知道是否有人可以帮我弄清楚我的代码有什么问题。我正在尝试创建一个Web浏览器后退和前进按钮。我使用Javas" 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());
}
}
}
答案 0 :(得分:1)
historyBack
和historyFront
都应该像堆栈一样工作,如下所示:
点击链接时,将当前页面推到“后面”堆栈,清除“前”堆栈,然后显示新页面。
单击后退按钮时,将当前页面推到“前”堆栈,从“后”堆栈弹出最高值,然后显示该页面。
单击前进按钮时,按“后”堆栈上的当前页面,从“前”堆栈弹出最高值,并显示该页面。
如您所见,它是纯粹的堆栈操作。
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()
,您不会意外混合它们。