1 / 43

Object-Oriented Programming (Java), Unit 22

Object-Oriented Programming (Java), Unit 22. Kirk Scott. Keystrokes, Text Areas and Scroll Bars, and Menus. 22.1 Keystrokes 22.2 Text Areas and Scroll Bars 22.3 Menus. Kinkajou, South America. Infant stub-tailed macaque. 22.1 Keystrokes. ClickKey

chun
Download Presentation

Object-Oriented Programming (Java), Unit 22

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming (Java), Unit 22 Kirk Scott

  2. Keystrokes, Text Areas and Scroll Bars, and Menus • 22.1 Keystrokes • 22.2 Text Areas and Scroll Bars • 22.3 Menus

  3. Kinkajou, South America

  4. Infant stub-tailed macaque

  5. 22.1 Keystrokes • ClickKey • The idea of this example is that the seeds will jump between cups if the keys A or B are pressed, if the corresponding cup is active. • A screenshot of this application would look no different from that of the previous example. • The new part of the UML diagram is given on the next overhead, followed by the complete UML diagram.

  6. This is the only part of the UML diagram that is new.

  7. Imports needed for key handling: • import java.awt.event.KeyAdapter; • import java.awt.event.KeyEvent;

  8. Changes in the panel class: • The panel has an instance of the KeyHandler class added to it using the addKeyListener() method. • An important additional change is that it is necessary to make the panel focusable. • That means making it independently able to receive input.

  9. This is accomplished by a call to the setFocusable() method. • Focus is a broad and important topic which will come up again. • This is just the barest introduction to the topic in this unit.

  10. class ClickKeyPanel extends JPanel • { • private SeedCup myCupA; • private SeedCup myCupB; • private SeedCup whichCupIsActive; • public ClickKeyPanel() • { • myCupA = new SeedCup(4, 200, 200, 40, 40); • myCupB = new SeedCup(0, 250, 200, 40, 40); • whichCupIsActive = myCupA; • addMouseListener(new MouseHandler()); • addMouseMotionListener( • new MouseMotionHandler()); • addKeyListener(new KeyHandler()); • setFocusable(true); • }

  11. The code for the KeyHandler class: • Its basic logic is the same as that of the mouse handler. • The difference is that the event which it detects is a keystroke which is not entered into a text field. • Just as it’s possible to get the location of a mouse click, it is possible to get the keystroke that was entered when the panel has focus. • In the application, the cups are known as cup A and cup B. • Pressing either of the two keys A or B has the effect of selecting the corresponding cup if it’s active.

  12. private class KeyHandler extends KeyAdapter • { • public void keyTyped(KeyEvent event) • { • char keyChar = event.getKeyChar(); • if(keyChar == 'a' && whichCupIsActive == myCupA) • { • myCupA.moveCupValueTo(myCupB); • whichCupIsActive = myCupB; • repaint(); • } • else if(keyChar == 'b' && whichCupIsActive == myCupB) • { • myCupB.moveCupValueTo(myCupA); • whichCupIsActive = myCupA; • repaint(); • } • else • { • } • } • }

  13. 22.2 Text Areas and Scroll Bars

  14. ClickTextScroll: • This example uses a JTextArea and a JScrollPane to include a textual record of the sequence of actions in the application. • A screenshot is given on the following overhead of the application’s appearance after four mouse clicks on the alternately active cups, A and B:

  15. The ClickTextScrollPanel: • This application has changes in the ClickTextScrollPanel class and its constructor. • It also has changes in the mouse handler—what has to be done when the mouse is clicked.

  16. The panel has an instance variable, actionRecordArea, constructed in place. • In the panel constructor an instance of JScrollPane is constructed and the actionRecordArea is passed to it as a parameter. • The result of this construction, actionScrollPane, contains the actionRecordArea and has scroll bars.

  17. The changes from the previous version of the code are sufficiently small that a new UML diagram isn’t provided. • The code for the panel class is given on the following overhead.

  18. class ClickTextScrollPanel extends JPanel • { • private JTextArea actionRecordArea = new JTextArea(6, 24); • private JScrollPane actionScrollPane; • private SeedCup myCupA; • private SeedCup myCupB; • private SeedCup whichCupIsActive; • private int moveCount = 1; • public ClickTextScrollPanel() • { • myCupA = new SeedCup(4, 200, 200, 40, 40); • myCupB = new SeedCup(0, 250, 200, 40, 40); • whichCupIsActive = myCupA; • actionRecordArea.setEditable(false); • actionScrollPane = new JScrollPane(actionRecordArea, • JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, • JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); • add(actionScrollPane, "North"); • addMouseListener(new MouseHandler()); • addMouseMotionListener(new MouseMotionHandler()); • }

  19. The MouseHandler: • When an action is taken and the mouseClicked() method is triggered, the text describing the action is appended to the actionRecordArea.

  20. private class MouseHandler extends MouseAdapter • { • public void mouseClicked(MouseEvent event) • { • if(myCupA.getRectangle().contains(event.getPoint()) • && whichCupIsActive == myCupA) • { • myCupA.moveCupValueTo(myCupB); • whichCupIsActive = myCupB; • actionRecordArea.append("Move: " + moveCount + • ". Cup: A.\n"); • moveCount++; • repaint(); • }

  21. else if(myCupB.getRectangle().contains • (event.getPoint()) && whichCupIsActive == myCupB) • { • myCupB.moveCupValueTo(myCupA); • whichCupIsActive = myCupA; • actionRecordArea.append("Move: " + moveCount + • ". Cup: B.\n"); • moveCount++; • repaint(); • } • else • { • } • setCursor(Cursor.getDefaultCursor()); • } • }

  22. 22.3 Menus • ClickMenu • This program includes a simple menu with two options. • One causes the program to exit. • The other causes the application to restart, or reset itself to its initial state.

  23. ClickMenu Screenshot:

  24. ClickMenu Listeners: • The code for the application includes RestartListener and ExitListener classes. • These two classes implement the ActionListener interface, which has just one method in it, actionPerformed(). • Unlike the handlers associated with the mouse, which belong to the panel, the menu and the listeners for its two options belong to the frame.

  25. A menu bar is created and added to the frame with a call to setJMenuBar(). • A menu is then added to the menu bar, various menu items are added to it, and listeners are added to the items. • The following UML diagram shows the structure of menus, menu items, and the listeners that go with them.

  26. The code for the frame class is given on the following overheads. • First, the needed imports are shown. • Then you see the sequence of object constructions and calls to add() needed to assemble a menu. • After that code, the code for the menu item listeners will be covered.

  27. import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • class ClickMenuFrame extends JFrame • { • private ClickMenuPanelmyPanel; • private final int FRAMEW = 500; • private final int FRAMEH = 500; • public ClickMenuFrame() • { • setTitle("ClickMenu Frame"); • setSize(FRAMEW, FRAMEH); • myPanel = new ClickMenuPanel(); • Container contentPane = getContentPane(); • contentPane.add(myPanel, "Center"); • addWindowListener(new WindowCloser());

  28. JMenuBar menuBar = new JMenuBar(); • setJMenuBar(menuBar); • JMenu fileMenu = new JMenu("File"); • menuBar.add(fileMenu); • JMenuItem restartItem = new JMenuItem("Restart"); • fileMenu.add(restartItem); • RestartListener myRestartListener = new RestartListener(); • restartItem.addActionListener(myRestartListener); • JMenuItem exitItem = new JMenuItem("Exit"); • fileMenu.add(exitItem); • ExitListener myExitListener = new ExitListener(); • exitItem.addActionListener(myExitListener); • }

  29. Applications with multiple parts • As you do the assignments, your application will grow to have multiple parts. • Getting them all to work consistently becomes a challenge. • The exit listener is relatively simple. • The restart listener for the menu has to renew the text area as well as the cups.

  30. The listener for the Exit menu option is given on the following overhead. • It simply duplicates the functionality of the WindowCloser class. • Taking the Exit option ends the application. • Remember that the listener is an inner class of the frame.

  31. private class ExitListener implements ActionListener • { • public void actionPerformed(ActionEvent event) • { • System.exit(0); • } • }

  32. The code for the Restart menu option (item) listener is given on the overhead following the next one. • It calls methods to renew the cups in the panel, renew the text area, and reset the move count.

  33. These renew methods are specific to the application and belong to the panel class. • Remember that the listener is an inner class of the frame. • The listener has direct access to the panel belonging to the frame.

  34. private class RestartListener implements ActionListener • { • public void actionPerformed(ActionEvent event) • { • myPanel.renewBoard(); • myPanel.renewTextArea(); • myPanel.setMoveCount(1); • } • }

  35. The code for the renewBoard() method is given on the following overhead. • This is a method of the panel class. • It duplicates part of the functionality of the constructor for that class, replacing the cups and then repainting.

  36. public void renewBoard() • { • myCupA = new SeedCup(4, 200, 200, 40, 40); • myCupB = new SeedCup(0, 250, 200, 40, 40); • whichCupIsActive = myCupA; • repaint(); • }

  37. Updating the text area: • When working with the contents of the text area, the setText() and getText() methods can be used. • These methods are inherited by the JTextArea class from the JTextComponent class. • setText() takes a String as a parameter. • To clear a text area, all you have to do is pass in the empty String, “”.

  38. The code for the renewTextArea() method is given on the following overhead. • This is a method of the panel class. • It simply resets the text in the action record area to the empty string and then calls repaint().

  39. public void renewTextArea() • { • actionRecordArea.setText(""); • repaint(); • }

  40. The End

More Related