我的家庭作業總結是創建一個顯示器和一個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 */ }
}
}
}
僅供參考,此處爲顯示鏈接。 http://i.imgur.com/HTODP.png – Justin 2012-02-06 10:56:49