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

发布于 2025-01-02 20:00:58 字数 9797 浏览 0 评论 0原文

我的作业总结是创建一个显示器和一个与该显示器交互的 GUI,允许用户添加形状并移动它们以及一些其他规格。我完成了该任务,并添加了一些额外的功能。

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

问题是,一旦我使该按钮发挥作用,我就丢失了之前实现的热键实用程序。我不明白为什么我会失去热键实用程序。

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

以下是显示内容的链接:https://i.sstatic.net/3fPag.png< /a>

我很感激任何可能的建议。

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 */ }
}
}
}

A summary of my homework assignment was to create a display and a GUI to interact with that display allowing users to add shapes and move them around along with a few other specifications. I completed that assignment, and added some extra features.

One of the features was to assign hot keys to each of the buttons. Once I got that functional, I then decided to create a short video tutorial explaining the features of the program. I attached the URL to a button and placed it on the screen. Currently it's just pointing to Google.

The problem is, once I got that button functional, I lost the hot key utility that I previously implemented. I can't figure out why I would lose the hot key utility.

Below is the somewhat lengthy code for the controller class. If more (or less) information is needed, please let me know.

Here is a link to what the display looks like: https://i.sstatic.net/3fPag.png

I appreciate any advice possible.

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 */ }
}
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2025-01-09 20:00:58

从您发布的内容来看,我看到的唯一可能的问题是,当您按下 URI 按钮时,您的应用程序中可能没有注册了 keyListener 的部分当前处于焦点状态。

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

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

From what you posted, the only possible problem i see is that maybe no part of your application which has a keyListener registered is currently focussed when you pressed the URI Button.

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

So if neither viewPane nor the circle has focus when you push a key, the key Event won't fire. So you possibly got to make sure they have the focus again. That's the only explanation i would have for this behaviour.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文