为什么添加URL按钮会删除我的keyPress功能?

时间:2012-02-06 10:34:58

标签: java button uri keypress

我的家庭作业总结是创建一个显示器和一个GUI来与该显示器进行交互,允许用户添加形状并将其与其他一些规格一起移动。我完成了这项任务,并添加了一些额外的功能。

其中一项功能是为每个按钮分配热键。一旦我获得了这个功能,我就决定创建一个简短的视频教程来解释该程序的功能。我将URL附加到按钮并将其放在屏幕上。目前它只是指向谷歌。

问题是,一旦我按下该按钮,我就丢失了之前实现的热键实用程序。我无法弄清楚为什么我会丢失热键实用程序。

以下是控制器类的冗长代码。如果需要更多(或更少)的信息,请告诉我。

以下是显示内容的链接:http://i.imgur.com/HTODP.png

我感谢任何可能的建议。

public class DrawingBoardControl extends JPanel {

private DrawingBoardView viewPane;
private DrawingBoardTextView textPane;
private DrawingBoard board;
private BoardButtonListener buttonListener;
private BoardMouseListener mouseListener;
private BoardKeyListener keyListener;
private int desiredShape;
private JLabel count = new JLabel();
private JLabel selectedShape = new JLabel();
private final URI uri;



/** Construct a viewer/controller for the given DrawingBoard
* @param board The DrawingBoard object to be controlled and viewed.
*/
public DrawingBoardControl(DrawingBoard b) throws URISyntaxException{ 

// Initializes panel with no shape selected    
desiredShape = 3;

// URL for Instruction Video
uri = new URI("http://google.com");

// model
board = b;

// layout
setLayout(new BorderLayout());

// create panel
viewPane = new DrawingBoardView(board);
viewPane.setPreferredSize(new Dimension(board.getWidth(),board.getHeight()));
viewPane.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
viewPane.setBackground(Color.darkGray);
add(viewPane, BorderLayout.CENTER);

textPane = new DrawingBoardTextView(board);

// register view with model
board.addViewer(viewPane);
board.addViewer(textPane);

// create bottom panel
JButton circle = new JButton("(C)ircle");
JButton delta = new JButton("(D)elta");
JButton tee = new JButton("(T)ee");
JButton edit = new JButton("(E)dit Mode");
JButton reset = new JButton("(R)eset");
count.setText("Total shapes: " + board.getShapeCount());
count.setForeground(Color.white);
selectedShape.setText("Currently there is no selected Shape.");
selectedShape.setForeground(Color.white);
JPanel buttons = new JPanel();
buttons.add(selectedShape);
buttons.add(circle);
buttons.add(delta);
buttons.add(tee);
buttons.add(edit);
buttons.add(reset);
buttons.add(count);    
circle.setBackground(Color.cyan);
delta.setBackground(Color.red);
tee.setBackground(Color.green);
edit.setBackground(Color.yellow);
reset.setBackground(Color.white);
buttons.setBackground(Color.black);
this.add(buttons, BorderLayout.SOUTH);

// create top panel
JButton tutorial = new JButton();
tutorial.setText("Click here to view a web tutorial explaining the "
                                 + "options available in this program.");
tutorial.setToolTipText(uri.toString());
tutorial.addActionListener(new OpenUrlAction());
tutorial.setBackground(Color.black);
tutorial.setForeground(Color.white);
add(tutorial, BorderLayout.NORTH);

// setup listeners
buttonListener = new BoardButtonListener(board);
circle.addActionListener(buttonListener);
delta.addActionListener(buttonListener);
tee.addActionListener(buttonListener);
edit.addActionListener(buttonListener);
reset.addActionListener(buttonListener);

mouseListener = new BoardMouseListener(board);
viewPane.addMouseListener(mouseListener);

// I don't fully understand why, but all three of these are needed.
keyListener = new BoardKeyListener(board);
viewPane.addKeyListener(keyListener);
circle.addKeyListener(keyListener);
}

/**
* Handle button clicks for the DrawingBoardControl window.
* 
* @author Justin Ashburn
* @version 2/4/2012
*/  
class BoardButtonListener implements ActionListener {

private DrawingBoard board;

/**Constructor for objects of class BoardButton.
  * @param board The model object of the DrawingBoard.
  */

public BoardButtonListener(DrawingBoard board) {
  this.board = board;
}

/** Process button clicks by choosing the shape to be created.
  * @param The button click event.
  */
public void actionPerformed(ActionEvent e) {
  count.setText("Total shapes: " + board.getShapeCount());
  if(e.getActionCommand().equals("(C)ircle")) {
    desiredShape = 0;
  }
  else if(e.getActionCommand().equals("(D)elta")) {
    desiredShape = 1;
  }
  else if (e.getActionCommand().equals("(T)ee")){
    desiredShape = 2;
  }
  else if (e.getActionCommand().equals("(E)dit Mode")){
    desiredShape = 3;
    if (board.getHasSelected() == true){
      board.setHasSelected(false);
    }
  }
  else if (e.getActionCommand().equals("(R)eset")){
    desiredShape =4;
    for (int k = board.getShapeCount()-1; k >= 0; k--) {
      board.setHasSelected(true);
      board.removeShape();
      count.setText("Total shapes: " + board.getShapeCount());
    }
  }

  if (board.getHasSelected()) {
    selectedShape.setText(board.getSelectedShape().toString());
  }
  else
    selectedShape.setText("Currently there is no selected Shape.");
 }
 }


 /**
 * Handle mouse clicks for the DrawingBoardControl window.
 * 
 * @author Justin Ashburn
 * @version 2/4/2012
 */
 class BoardMouseListener implements MouseListener, MouseMotionListener {

 private DrawingBoard board;

 /**
 * Constructor for objects of class BoardMouseListener.
 * @param board The model object containing the state of the DrawingBoard.
 */
 public BoardMouseListener(DrawingBoard board) {
  this.board = board;    
 }

 /**
 * Process mouse press by adding a designated Shape 
 * to the DrawingBoard at the location of the click.
 * @param e The mouse press event.
 */    
 public void mousePressed (MouseEvent e) {
  if (e.getModifiers()==16) {
    if (desiredShape == 0) {
      Circle c = new Circle(e.getX(), e.getY(), 100);
      c.setColor(Color.cyan);          
      board.addShape(c);
    }
    else if (desiredShape == 1) {
      Delta d = new Delta(e.getX(), e.getY(), 100);
      d.setColor(Color.red);
      board.addShape(d);
    }
    else if (desiredShape == 2) {
      Tee t = new Tee(e.getX(), e.getY(), 100);
      t.setColor(Color.green);
      board.addShape(t);
    }
    else if (desiredShape == 3) {
      if (board.getShapeCount() > 0)
        board.selectShape(e.getX(),e.getY());
    }
  }
  else if(e.getModifiers()==4) {
    if (desiredShape == 3) {
      if (board.getShapeCount() > 0) 
        board.selectShape(e.getX(),e.getY());
    }
  }
  count.setText("Total shapes: " + board.getShapeCount());
  if (board.getHasSelected()) {
    selectedShape.setText(board.getSelectedShape().toString());
  }
  else
    selectedShape.setText("Currently there is no selected Shape.");
}

/**
* Process mouse release by adding a designated Shape 
* to the DrawingBoard at the location of the click.
* @param e The mouse release event.
*/
public void mouseReleased(MouseEvent e) {
  if (board.getHasSelected()) {
    if (e.getModifiers()==16) {
      if (desiredShape == 3) {
        board.moveShape(e.getX() - board.getSelectedShape().getX(), 
                        e.getY() - board.getSelectedShape().getY());
        board.setHasSelected(false);
      }
    }        
    else if (e.getModifiers()==4) {
      if (desiredShape >= 0 && desiredShape <= 2) {
        board.removeShape();
      }
      if (desiredShape == 3) {
        board.removeShape();
        if (board.getHasSelected()) {
          board.setHasSelected(false);
        }
      }
    }
  }
  count.setText("Total shapes: " + board.getShapeCount());
  if (board.getHasSelected()) {
    selectedShape.setText(board.getSelectedShape().toString());
  }
  else
    selectedShape.setText("Currently there is no selected Shape.");
}

// dummy implementations for other events in mouselistener
public void mouseDragged (MouseEvent e) { }
public void mouseClicked (MouseEvent e) { }
public void mouseEntered (MouseEvent e) { }
public void mouseExited  (MouseEvent e) { }
public void mouseMoved   (MouseEvent e) { }
}

/**
* Handle keyboard entries for the DrawingBoardControl window.
* 
* @author Justin Ashburn
* @version 2/4/2012
*/

class BoardKeyListener implements KeyListener {

private DrawingBoard board;

/**Constructor for objects of class BoardKey.
  * @param board The model object of the DrawingBoard.
  */    
public BoardKeyListener(DrawingBoard board) {
  this.board = board;
}

/** Process key presses by choosing the shape to be created.
  * @param The key press event.
  */
public void keyPressed(KeyEvent e) {
  switch(e.getKeyCode()) {
    case KeyEvent.VK_C:
      desiredShape = 0;
      break;
    case KeyEvent.VK_D:
      desiredShape = 1;
      break;
    case KeyEvent.VK_T:
      desiredShape = 2;
      break;
    case KeyEvent.VK_E:
      desiredShape = 3;
      if (board.getHasSelected() == true){
        board.setHasSelected(false);          
        break;
      }
    case KeyEvent.VK_R:
      desiredShape = 4;
      for (int k = board.getShapeCount()-1; k >= 0; k--) {
        board.setHasSelected(true);
        board.removeShape();
        count.setText("Total shapes: " + board.getShapeCount());
      }
      if (board.getHasSelected()) {
        selectedShape.setText(board.getSelectedShape().toString());
      }
      else
        selectedShape.setText("Currently there is no selected Shape.");
  }
}
//dummy implementations for keyboard listener
public void keyReleased(KeyEvent e) { }
public void keyTyped  (KeyEvent e) { }
}

/**
* Handle URL actions for the DrawingBoardControl window.
* 
* @author Justin Ashburn
* @version 2/4/2012
*/
class OpenUrlAction implements ActionListener {

/** Process URL events.
  * @param The URL event.
  */
public void actionPerformed(ActionEvent e) {
  open(uri);
}
public void open(URI uri) {
  if (Desktop.isDesktopSupported()) {
    try {
      Desktop.getDesktop().browse(uri);
    } catch (IOException e) { /* TODO: error handling */ }
  } else { /* TODO: error handling */ }
}
}
}

1 个答案:

答案 0 :(得分:1)

根据您发布的内容,我看到的唯一可能的问题是,当您按下URI按钮时,可能没有注册了keyListener的应用程序的任何部分。

viewPane.addKeyListener(keyListener);
circle.addKeyListener(keyListener);

因此,如果在按下某个键时viewPane和圆圈都没有焦点,则不会触发键事件。所以你可能要确保他们再次关注焦点。这是我对这种行为的唯一解释。