430 likes | 598 Views
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
E N D
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
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.
Imports needed for key handling: • import java.awt.event.KeyAdapter; • import java.awt.event.KeyEvent;
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.
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.
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); • }
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.
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 • { • } • } • }
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:
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.
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.
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.
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()); • }
The MouseHandler: • When an action is taken and the mouseClicked() method is triggered, the text describing the action is appended to the actionRecordArea.
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(); • }
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.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.
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.
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.
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.
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());
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); • }
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.
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.
private class ExitListener implements ActionListener • { • public void actionPerformed(ActionEvent event) • { • System.exit(0); • } • }
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.
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.
private class RestartListener implements ActionListener • { • public void actionPerformed(ActionEvent event) • { • myPanel.renewBoard(); • myPanel.renewTextArea(); • myPanel.setMoveCount(1); • } • }
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.
public void renewBoard() • { • myCupA = new SeedCup(4, 200, 200, 40, 40); • myCupB = new SeedCup(0, 250, 200, 40, 40); • whichCupIsActive = myCupA; • repaint(); • }
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, “”.
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().
public void renewTextArea() • { • actionRecordArea.setText(""); • repaint(); • }