2012-02-06 53 views
2

我的家庭作业总结是创建一个显示器和一个GUI来与该显示器进行交互,允许用户添加形状并将它们与其他一些规格一起移动。我完成了这个任务,并添加了一些额外的功能。为什么添加URL按钮会删除我的keyPress能力?

其中一个功能是将热键分配给每个按钮。一旦我获得了这个功能,我就决定创建一个简短的视频教程来解释该程序的功能。我将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 */ } 
} 
} 
} 
+0

仅供参考,此处为显示链接。 http://i.imgur.com/HTODP.png – Justin 2012-02-06 10:56:49

回答

1

从你贴什么,唯一可能的问题,我看到的是,当你按下按钮URI也许也不会属于你的应用程序中的具有注册的KeyListener目前集中。

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

因此,如果您按下某个按键时viewPane和圆都没有焦点,则按键事件将不会触发。所以你可能需要确保他们重新获得焦点。这是我对这种行为的唯一解释。

+0

我仍然是一个新手,但你帮助我理解了这里的一个重点。我不明白以前需要使用circle keyListener,但我现在已经明白了。这是因为这是程序运行时默认的“突出显示”按钮。如果有其他按钮被突出显示,它将不会像我预期的那样工作。我只需要为每个按钮添加一个keyListener。非常感谢! – Justin 2012-02-06 11:17:09

相关问题