330 likes | 440 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. 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. • Here is a screenshot 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. • 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.
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()); • } • }
Updating the text area: • As the application grows, it will be necessary to do more things with text areas. • 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, “”. • To set the text area with text retrieved with the load operation, call setText() with that text as a String. • The getText() method returns a String. • It can be called in order to obtain the text that needs to be saved when taking the Save option in the menu.
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.
Imports needed to do menus: • import java.awt.*; • import java.awt.event.*; • import javax.swing.*;
Here is the code for the frame class, showing how the menu elements shown in the UML diagram are constructed and assembled: • class ClickMenuFrame extends JFrame • { • private ClickMenuPanel myPanel; • 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. • For example, the restart listener has to renew the game board.
After adding a text area to the application, the restart listener should also renew it. • A future version of the game will also have menu options to save and restore the state of the game. • These options will also require working with the text area as well as the state of the board.
Here is the listener for the Restart menu option. It calls a methods to renew the cups in the panel, renew the text area, and reset the move count. • private class RestartListener implements ActionListener • { • public void actionPerformed(ActionEvent event) • { • myPanel.renewBoard(); • myPanel.renewTextArea(); • myPanel.setMoveCount(1); • } • }
Here is the listener for the Exit menu option. It duplicates the functionality of the WindowCloser class. Taking the Exit option ends the application. • private class ExitListener implements ActionListener • { • public void actionPerformed(ActionEvent event) • { • System.exit(0); • } • } • }
Here is the renewCups() method in the panel class. It duplicates part of the functionality of the constructor for that class, replacing the cups and then repainting. • public void renewCups() • { • myCupA = new SeedCup(4, 200, 200, 40, 40); • myCupB = new SeedCup(0, 250, 200, 40, 40); • whichCupIsActive = myCupA; • repaint(); • }