Introduction to Software Systems Honors Lecture #19: November 4, 2015 1/14
Third Exam The third, Checkpoint Exam, will be on: Wednesday, November 11, 2:30 to 3:45 pm You will have 3 questions, out of 9, listed at: http://www.cs.indiana.edu/classes/c212-dgerman/fall2015/lecturefourteen.html 1. Catching and Reporting Mouse Motion Events 2. Catching and Reporting Mouse Events 3. Catching and Reporting Action Events 4. Catching and Reporting Key(board) Events 5. Creating a JFrame, a JComponent and a Simple Graphics 6. Creating and Using Timers 7. Sorting an Array(List) of Objects 8. Throwing and Catching Exceptions 9. Creating and Using JButtons, JTextFields and JLabels 2/14
1. Catching and reporting mouse motion events We explained mouse events in Lecture 16, we mentioned two categories: mouse events mouse motion events Suppose we want to have a panel and to print the mouse position of we move the mouse. We need to implement: Frame Component We need a component object We have to add it to the frame Mouse listener We need to connect the components together. 3/14
Frame Remember: You cannot draw directly onto a frame! import javax.swing.*; public class myframe{ public static void main(string[] args) { JFrame a = new JFrame(); a.setvisible(true); a.setsize(400, 400); a.getcontentpane().add(new mycomponent ()); 4/14
JComponent JComponent class represents a blank component We have to modify the JComponent class and specify how the component should be painted. import javax.swing.*; import java.awt.*; public class mycomponent extends JComponent { Remember, paintcomponent is called: When the component is shown for the first time When the window is resized, When it is shown again after it was hidden, int x, y; public mycomponent() { Upon execution of repaint() this.addmousemotionlistener( new Helper(this) ); public void paintcomponent(graphics g) { g.drawstring("(" + this.x + ", " + this.y + ")", this.x, this.y); 5/14
Listener import java.awt.event.*; public class Helper implements MouseMotionListener { mycomponent myc; Helper(myComponent myc) { this.myc = myc; public void mousedragged(mouseevent e) { Remember: we have to implement all methods in the interface. public void mousemoved(mouseevent e) { this.myc.x = e.getx(); this.myc.y = e.gety(); this.myc.repaint(); Set x and y in mycomponent object to the current position of the mouse. Ask mycomponent object to the repaint itself. 6/14
Java puzzle public class Elementary { public static void main(string[] args) { System.out.println(12345 + 5432l); 7/14
We will implement a frame which will contain: button label 3. Catching and reporting action events Each time the button is pushed, the text on the label will be updated. Each container has its own layout manager, which determines how the components are laid out. By default, a JPanel uses a flow layout, which simply arranges its components from left to right and starts a new row when there is no more room in the current row. Another commonly used layout manager is the border layout, that groups components into five areas: center, north, west, south, and east. 8/14
import javax.swing.*; import java.awt.*; 3. Catching and reporting action events class One extends JFrame { private JButton b; private JLabel output; One() { createuserinterface(); Constructor public static void main(string[] args) { One one = new One(); one.setdefaultcloseoperation(jframe.exit_on_close); 9/14
private void createuserinterface() { Container c = getcontentpane(); c.setlayout(null); b = new JButton(); b.setbounds(20, 20, 120, 20); b.settext("push me!"); c.add(b); Add a button. output = new JLabel(); output.setbounds(50, 70, 220, 20); Add a label. output.settext("0 clicks so far. Push the button."); c.add(output); Two referee = new Two(output); b.addactionlistener(referee); When Action listener object is created, it is passed the reference to the label. Add listener for the button. setsize(400, 400); setvisible(true); Size of the frame. 10/14
Action listener import javax.swing.*; import java.awt.event.*; class Two implements ActionListener { int count = 0; JLabel label; Two(JLabel label) { this.label = label; public void actionperformed(actionevent e) { this.count += 1; System.out.println("Ouch: " + this.count); Constructor Action performed upon an event. this.label.settext(this.count + " click(s) so far and counting."); 11/14
import javax.swing.*; import java.awt.*; 4. Catching and reporting keyboard events class Three { public static void main(string[] args) { JFrame frame = new JFrame(); Container c = frame.getcontentpane(); c.setbackground(new Color(0.66f, 1.0f, 0.66f)); c.setlayout(null); JTextField t = new JTextField(); t.setbounds(10, 120, 70, 20); c.add(t); 12/14
4. Catching and reporting keyboard events KeyBroker k = new KeyBroker(); t.addkeylistener(k); JButton u = new JButton(); u.setbounds(100, 20, 90, 30); u.settext("erase"); c.add(u); ButtonBroker b = new ButtonBroker(t); u.addactionlistener(b); frame.setsize(300, 300); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); 13/14
4. Catching and reporting keyboard events import java.awt.event.*; class KeyBroker implements KeyListener { public void keypressed(keyevent e) { public void keyreleased(keyevent e) { public void keytyped(keyevent e) { System.out.println("Ouch!"); 14/14
import javax.swing.*; import java.awt.event.*; 4. Catching and reporting keyboard events class ButtonBroker implements ActionListener { JTextField t; ButtonBroker(JTextField t) { this.t = t; public void actionperformed(actionevent e) { System.out.println("Ha, ha!"); t.settext(""); 15/14