COMP16121 Sample Code Lecture 1

Size: px
Start display at page:

Download "COMP16121 Sample Code Lecture 1"

Transcription

1 COMP16121 Sample Code Lecture 1 Sean Bechhofer, University of Manchester, Manchester, UK sean.bechhofer@manchester.ac.uk 1 SimpleFrame 1 import javax.swing.jframe; 2 3 public class SimpleFrame { 4 5 /* Main method to drive the program */ 6 public static void main( String [] args ) { 7 JFrame theframe = new JFrame(); 8 theframe.settitle( "A Frame" ); 9 theframe.setsize( 300, 200 ); 10 theframe.setvisible( true ); 11 } // main } // SimpleFrame 1

2 2 HelloWorld 1 import java.awt.container; 2 import javax.swing.jlabel; 3 import javax.swing.jframe; 4 5 public class HelloWorld extends JFrame { 6 7 /* Constructor */ 8 public HelloWorld() { 9 settitle( "Hello World" ); 10 /* Get the contents of the frame and add a label to it */ 11 Container contents = getcontentpane(); 12 contents.add( new JLabel( "Hello World!" ) ); /* Specify appropriate behaviour when the window is closed */ 15 setdefaultcloseoperation( EXIT_ON_CLOSE ); /* pack the frame */ 18 pack(); 19 } // HelloWorld /* Main method to drive the program */ 22 public static void main( String [] args ) { 23 HelloWorld thehelloworld = new HelloWorld(); 24 thehelloworld.setvisible( true ); 25 } // main } // HelloWorld 2

3 3 HelloSolarSystem 1 import java.awt.container; 2 import java.awt.flowlayout; 3 import javax.swing.jlabel; 4 import javax.swing.jframe; 5 6 public class HelloSolarSystem extends JFrame { 7 8 /* Constructor */ 9 public HelloSolarSystem() { 10 settitle( "Hello SolarSystem" ); 11 /* Get the contents of the frame. */ 12 Container contents = getcontentpane(); 13 /* Set the Layout Manager */ 14 contents.setlayout( new FlowLayout( FlowLayout.LEFT, 10, 10 ) ); 15 /* Add labels */ 16 contents.add( new JLabel( "Hello Mercury!" ) ); 17 contents.add( new JLabel( "Hello Venus!" ) ); 18 contents.add( new JLabel( "Hello Earth!" ) ); 19 contents.add( new JLabel( "Hello Mars!" ) ); 20 contents.add( new JLabel( "Hello Jupiter!" ) ); 21 contents.add( new JLabel( "Hello Saturn!" ) ); 22 contents.add( new JLabel( "Hello Uranus!" ) ); 23 contents.add( new JLabel( "Hello Neptune!" ) ); 24 contents.add( new JLabel( "Hello Pluto!" ) ); /* Specify appropriate behaviour when the window is closed */ 27 setdefaultcloseoperation( EXIT_ON_CLOSE ); /* pack the frame */ 30 pack(); 31 } // HelloSolarSystem /* Main method to drive the program */ 34 public static void main( String [] args ) { 35 HelloSolarSystem thehellosolarsystem = 36 new HelloSolarSystem(); 37 thehellosolarsystem.setvisible( true ); 38 } // main } // HelloSolarSystem 3

4 4 HelloSolarSystemGrid 1 import java.awt.container; 2 import java.awt.gridlayout; 3 import javax.swing.jlabel; 4 import javax.swing.jframe; 5 6 public class HelloSolarSystemGrid extends JFrame { 7 8 /* Constructor */ 9 public HelloSolarSystemGrid() { 10 settitle( "Hello SolarSystem" ); 11 /* Get the contents of the frame. */ 12 Container contents = getcontentpane(); 13 /* Set the Layout Manager */ 14 contents.setlayout( new GridLayout( 0, 3, 10, 10 ) ); 15 /* Add labels */ 16 contents.add( new JLabel( "Hello Mercury!" ) ); 17 contents.add( new JLabel( "Hello Venus!" ) ); 18 contents.add( new JLabel( "Hello Earth!" ) ); 19 contents.add( new JLabel( "Hello Mars!" ) ); 20 contents.add( new JLabel( "Hello Jupiter!" ) ); 21 contents.add( new JLabel( "Hello Saturn!" ) ); 22 contents.add( new JLabel( "Hello Uranus!" ) ); 23 contents.add( new JLabel( "Hello Neptune!" ) ); 24 contents.add( new JLabel( "Hello Pluto!" ) ); /* Specify appropriate behaviour when the window is closed */ 27 setdefaultcloseoperation( EXIT_ON_CLOSE ); /* pack the frame */ 30 pack(); 31 } // HelloSolarSystemGrid /* Main method to drive the program */ 34 public static void main( String [] args ) { 35 HelloSolarSystemGrid thehellosolarsystemgrid = 36 new HelloSolarSystemGrid(); 37 thehellosolarsystemgrid.setvisible( true ); 38 } // main } // HelloSolarSystemGrid 4

5 5 HelloSmiley 1 import java.awt.container; 2 3 import javax.swing.imageicon; 4 import javax.swing.jframe; 5 import javax.swing.jlabel; 6 7 public class HelloSmiley extends JFrame { 8 9 /* Constructor */ 10 public HelloSmiley() { 11 settitle( "Hello Smiley" ); 12 /* Get the contents of the frame and add a label to it */ 13 Container contents = getcontentpane(); 14 contents.add( new JLabel( new ImageIcon( "smiley.jpg" ) ) ); /* Specify appropriate behaviour when the window is closed */ 17 setdefaultcloseoperation( EXIT_ON_CLOSE ); /* pack the frame */ 20 pack(); 21 } // HelloWorld /* Main method to drive the program */ 24 public static void main( String [] args ) { 25 HelloSmiley thehellosmiley = new HelloSmiley(); 26 thehellosmiley.setvisible( true ); 27 } // main } // HelloSmiley 5

6 COMP16121 Sample Code Lecture 2 Sean Bechhofer, University of Manchester, Manchester, UK sean.bechhofer@manchester.ac.uk 1 StopClock 1 import javax.swing.jlabel; 2 import java.util.date; 3 import javax.swing.jframe; 4 import java.awt.event.actionlistener; 5 import java.awt.event.actionevent; 6 import javax.swing.jbutton; 7 import java.awt.container; 8 import java.awt.gridlayout; 9 10 /** 11 * Stop Clock example. Button stops and starts the clock, which 12 * records start time, stop time and elapsed time. 13 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 14 */ 15 public class StopClock extends JFrame implements ActionListener { /* Local instance variables */ 18 private boolean isrunning = false; 19 private Date startdate; 20 private Date stopdate; 21 private JLabel starttimejlabel = new JLabel("Not started"); 22 private JLabel stoptimejlabel = new JLabel("Not started"); 23 private JLabel elapsedtimejlabel = new JLabel("Not started"); /* Constructor */ 26 public StopClock() { 27 settitle( "Stop Clock" ); Container contents = getcontentpane(); 30 /* Grid layout with one column */ 31 contents.setlayout( new GridLayout( 0, 1 ) ); /* Add some labels */ 34 contents.add(new JLabel("Started at:")); 35 contents.add(starttimejlabel); 36 contents.add(new JLabel("Stopped at:")); 37 contents.add(stoptimejlabel); 38 contents.add(new JLabel("Elapsed time (seconds):")); 39 contents.add(elapsedtimejlabel); 1

7 40 41 /* Create a button */ 42 JButton startstopjbutton = new JButton("Start / Stop"); 43 /* Register me with the button as a listener */ 44 startstopjbutton.addactionlistener(this); 45 contents.add(startstopjbutton); /* Specify appropriate behaviour when the window is closed */ 48 setdefaultcloseoperation(exit_on_close); /* pack the frame */ 51 pack(); 52 } // StopClock public void actionperformed( ActionEvent evt ) { 55 if (! isrunning) { 56 /* Start the clock */ 57 startdate = new Date(); 58 starttimejlabel.settext("" + startdate); 59 stoptimejlabel.settext("running..."); 60 elapsedtimejlabel.settext("running..."); 61 isrunning = true; 62 // Need to pack again because label size may have changed 63 pack(); 64 } else { 65 /* Stop the clock and show the updated times */ 66 stopdate = new Date(); 67 stoptimejlabel.settext("" + stopdate); 68 long elapsedmilliseconds = 69 (stopdate.gettime() - startdate.gettime()); 70 elapsedtimejlabel.settext("" + elapsedmilliseconds / ); 71 isrunning = false; 72 // Need to pack again because label size may have changed 73 pack(); 74 } 75 } // actionperformed public static void main(string [] args) { 78 StopClock thestopclock = new StopClock(); 79 thestopclock.setvisible( true ); 80 } // main 81 } 2

8 2 Button 1 import java.awt.container; 2 import javax.swing.jframe; 3 import javax.swing.jbutton; 4 import java.awt.event.actionlistener; 5 import java.awt.event.actionevent; 6 7 /** 8 * Simple frame with a single button. 9 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 10 */ 11 public class Button extends JFrame implements ActionListener { private JButton thebutton = new JButton( "Press Me!" ); /* Constructor */ 16 public Button() { 17 settitle("one Button"); 18 /* Get the contents of the frame and add a label to it */ 19 Container contents = getcontentpane(); 20 contents.add( thebutton ); 21 thebutton.addactionlistener(this); /* Specify appropriate behaviour when the window is closed */ 24 setdefaultcloseoperation(exit_on_close); /* pack the frame */ 27 pack(); 28 } // OneButton public void actionperformed( ActionEvent evt ) { 31 System.out.println("Pressed!"); 32 } /* Main method to drive the program */ 35 public static void main(string [] args) { 36 Button thebutton = new Button(); 37 thebutton.setvisible( true ); 38 } // main } // OneButton 3

9 COMP16121 Sample Code Lecture 3 Sean Bechhofer, University of Manchester, Manchester, UK sean.bechhofer@manchester.ac.uk 1 GCD 1 import javax.swing.jtextfield; 2 import javax.swing.jframe; 3 import java.awt.event.actionlistener; 4 import javax.swing.jbutton; 5 import java.awt.event.actionevent; 6 import java.awt.gridlayout; 7 import javax.swing.jlabel; 8 import java.awt.container; 9 10 /** 11 * Calculates GCD of two integers. Simple GUI. 12 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 13 */ 14 public class GCD extends JFrame implements ActionListener { private JTextField number1jtextfield = new JTextField(20); 17 private JTextField number2jtextfield = new JTextField(20); 18 private JTextField resultjtextfield = new JTextField(20); public GCD() { 21 settitle("gcd"); 22 Container contents = getcontentpane(); 23 contents.setlayout(new GridLayout(0, 1)); 24 contents.add(new JLabel("Number 1")); 25 contents.add(number1jtextfield); 26 contents.add(new JLabel("Number 2")); 27 contents.add(number2jtextfield); 28 JButton computejbutton = new JButton("Compute"); 29 contents.add(computejbutton); 30 computejbutton.addactionlistener(this); 31 contents.add(new JLabel("GCD of Number 1 and Number 2")); 32 contents.add(resultjtextfield); 33 /* Specify appropriate behaviour when the window is closed */ 34 setdefaultcloseoperation(exit_on_close); pack(); 37 } // GCD public void actionperformed(actionevent e) { 1

10 40 int number1 = Integer.parseInt(number1JTextField.getText()); 41 int number2 = Integer.parseInt(number2JTextField.getText()); 42 int thegcd = greatestcommondivisor(number1, number2); 43 resultjtextfield.settext("" + thegcd); 44 } // actionperformed private static int greatestcommondivisor(int multiple1ofgcd, 47 int multiple2ofgcd) { 48 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 49 while (multiple1ofgcd!= multiple2ofgcd) 50 if (multiple1ofgcd > multiple2ofgcd) 51 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 52 else 53 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 54 return multiple1ofgcd; 55 } // greatestcommondivisor public static void main(string [] args) { 58 GCD thegcd = new GCD(); 59 thegcd.setvisible( true ); 60 } // main 61 } // class GCD 2

11 2 GCDDisabled 1 import javax.swing.jtextfield; 2 import javax.swing.jframe; 3 import java.awt.event.actionlistener; 4 import javax.swing.jbutton; 5 import java.awt.event.actionevent; 6 import java.awt.gridlayout; 7 import javax.swing.jlabel; 8 import java.awt.container; 9 10 /** 11 * Calculates GCD of two integers. Simple GUI. 12 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 13 */ 14 public class GCDDisabled extends JFrame implements ActionListener { private JTextField number1jtextfield = new JTextField(20); 17 private JTextField number2jtextfield = new JTextField(20); 18 private JTextField resultjtextfield = new JTextField(20); public GCDDisabled() { 21 settitle("gcd"); 22 Container contents = getcontentpane(); 23 contents.setlayout(new GridLayout(0, 1)); 24 contents.add(new JLabel("Number 1")); 25 contents.add(number1jtextfield); 26 contents.add(new JLabel("Number 2")); 27 contents.add(number2jtextfield); 28 JButton computejbutton = new JButton("Compute"); 29 contents.add(computejbutton); 30 computejbutton.addactionlistener(this); 31 contents.add(new JLabel("GCD of Number 1 and Number 2")); 32 contents.add(resultjtextfield); 33 resultjtextfield.setenabled( false ); 34 /* Specify appropriate behaviour when the window is closed */ 35 setdefaultcloseoperation(exit_on_close); pack(); 38 } // GCDDisabled public void actionperformed(actionevent e) { 41 int number1 = Integer.parseInt(number1JTextField.getText()); 42 int number2 = Integer.parseInt(number2JTextField.getText()); 43 int thegcd = greatestcommondivisor(number1, number2); 44 resultjtextfield.settext("" + thegcd); 45 } // actionperformed private static int greatestcommondivisor(int multiple1ofgcd, 48 int multiple2ofgcd) { 49 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 3

12 50 while (multiple1ofgcd!= multiple2ofgcd) 51 if (multiple1ofgcd > multiple2ofgcd) 52 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 53 else 54 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 55 return multiple1ofgcd; 56 } // greatestcommondivisor public static void main(string [] args) { 59 GCDDisabled thegcd = new GCDDisabled(); 60 thegcd.setvisible( true ); 61 } // main 62 } // class GCDDisabled 4

13 3 TimesTable 1 import javax.swing.jframe; 2 import java.awt.event.actionlistener; 3 import javax.swing.jtextfield; 4 import javax.swing.jtextarea; 5 import java.awt.container; 6 import javax.swing.jbutton; 7 import java.awt.borderlayout; 8 import java.awt.event.actionevent; 9 10 /** 11 * Displays a times table using a {@link JTextArea JTextArea}. 12 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 13 */ 14 public class TimesTable extends JFrame implements ActionListener { private JTextField multiplierjtextfield = new JTextField(5); 17 private JTextArea displayjtextarea = new JTextArea(15, 20); public TimesTable() { 20 settitle("times Table"); 21 Container contents = getcontentpane(); 22 contents.setlayout(new BorderLayout()); 23 contents.add(multiplierjtextfield, BorderLayout.NORTH); 24 contents.add(displayjtextarea, BorderLayout.CENTER); 25 JButton displayjbutton = new JButton("Display"); 26 contents.add(displayjbutton, BorderLayout.SOUTH); 27 displayjbutton.addactionlistener(this); setdefaultcloseoperation(exit_on_close); 30 pack(); 31 } // TimesTable public void actionperformed(actionevent e) { 34 displayjtextarea.settext(""); 35 int multiplier = Integer.parseInt(multiplierJTextField.getText()); 36 displayjtextarea.append(" \n"); 37 displayjtextarea.append(" Times table for " + multiplier + "\n"); 38 displayjtextarea.append(" \n"); 39 for (int thisnumber = 1; 40 thisnumber <= 10; 41 thisnumber = thisnumber + 1) 42 displayjtextarea.append(" " + thisnumber + " x " + 43 multiplier + " = " 44 + thisnumber * multiplier + "\n"); 45 displayjtextarea.append(" \n"); 46 } // actionperformed public static void main(string [] args) { 49 TimesTable thetimestable = new TimesTable(); 5

14 50 thetimestable.setvisible( true ); 51 } // main } // class TimesTable

15 4 GCDWithPanels 1 import javax.swing.jtextfield; 2 import javax.swing.jframe; 3 import java.awt.event.actionlistener; 4 import javax.swing.jbutton; 5 import java.awt.event.actionevent; 6 import java.awt.gridlayout; 7 import javax.swing.jlabel; 8 import java.awt.container; 9 import javax.swing.jpanel; /** 12 * Calculates GCD of two integers. GUI using nested panels for layout. 13 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 14 */ 15 public class GCDWithPanels extends JFrame implements ActionListener { private JTextField number1jtextfield = new JTextField(20); 18 private JTextField number2jtextfield = new JTextField(20); 19 private JTextField resultjtextfield = new JTextField(20); public GCDWithPanels() { 22 settitle("gcd"); 23 Container contents = getcontentpane(); 24 contents.setlayout(new GridLayout(0, 1)); JPanel numberfieldspanel = new JPanel(); 27 numberfieldspanel.setlayout( new GridLayout( 0, 2 ) ); 28 contents.add( numberfieldspanel ); numberfieldspanel.add(new JLabel("Number 1")); 31 numberfieldspanel.add(new JLabel("Number 2")); 32 numberfieldspanel.add(number1jtextfield); 33 numberfieldspanel.add(number2jtextfield); JPanel buttonandresultpanel = new JPanel(); 36 buttonandresultpanel.setlayout( new GridLayout( 0, 2 ) ); 37 contents.add( buttonandresultpanel ); JButton computejbutton = new JButton("Compute"); 40 buttonandresultpanel.add(computejbutton); 41 computejbutton.addactionlistener(this); JPanel resultpanel = new JPanel(); 44 resultpanel.setlayout( new GridLayout( 0, 1 ) ); 45 buttonandresultpanel.add( resultpanel ); resultpanel.add(new JLabel("GCD of Number 1 and Number 2")); 48 resultpanel.add(resultjtextfield); 49 /* Specify appropriate behaviour when the window is closed */ 7

16 50 setdefaultcloseoperation(exit_on_close); pack(); 53 } // GCDWithPanels public void actionperformed(actionevent e) { 56 int number1 = Integer.parseInt(number1JTextField.getText()); 57 int number2 = Integer.parseInt(number2JTextField.getText()); 58 int thegcd = greatestcommondivisor(number1, number2); 59 resultjtextfield.settext("" + thegcd); 60 } // actionperformed private static int greatestcommondivisor(int multiple1ofgcd, 63 int multiple2ofgcd) { 64 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 65 while (multiple1ofgcd!= multiple2ofgcd) 66 if (multiple1ofgcd > multiple2ofgcd) 67 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 68 else 69 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 70 return multiple1ofgcd; 71 } // greatestcommondivisor public static void main(string [] args) { 74 GCDWithPanels thegcd = new GCDWithPanels(); 75 thegcd.setvisible( true ); 76 } // main 77 } // class GCDWithPanels 8

17 5 TimesTable2 1 import javax.swing.jframe; 2 import java.awt.event.actionlistener; 3 import javax.swing.jtextfield; 4 import javax.swing.jtextarea; 5 import java.awt.container; 6 import javax.swing.jbutton; 7 import java.awt.borderlayout; 8 import java.awt.event.actionevent; 9 import javax.swing.jpanel; 10 import java.awt.gridlayout; 11 import javax.swing.jlabel; /** 14 * Displays a times table using a {@link JTextArea JTextArea}. 15 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class TimesTable2 extends JFrame implements ActionListener { private JTextField multiplierjtextfield = new JTextField(5); 20 private JTextField tablesizejtextfield = new JTextField(5); 21 private JTextArea displayjtextarea = new JTextArea(15, 20); public TimesTable2() { 24 settitle("times Table"); 25 Container contents = getcontentpane(); 26 contents.setlayout(new BorderLayout()); 27 JPanel numberspanel = new JPanel( new GridLayout( 2, 2 ) ); 28 contents.add( numberspanel, BorderLayout.NORTH ); 29 numberspanel.add( new JLabel( "Multiplier:" ) ); 30 numberspanel.add( multiplierjtextfield ); numberspanel.add( new JLabel( "Table size:" ) ); 33 numberspanel.add( tablesizejtextfield ); 34 contents.add(displayjtextarea, BorderLayout.CENTER); 35 JButton displayjbutton = new JButton("Display"); 36 contents.add(displayjbutton, BorderLayout.SOUTH); 37 displayjbutton.addactionlistener(this); setdefaultcloseoperation(exit_on_close); 40 pack(); 41 } // TimesTable public void actionperformed(actionevent e) { 44 displayjtextarea.settext(""); 45 int multiplier = Integer.parseInt(multiplierJTextField.getText()); 46 int tablesize = Integer.parseInt(tableSizeJTextField.getText()); 47 displayjtextarea.append(" \n"); 48 displayjtextarea.append(" Times table for " + multiplier + "\n"); 49 displayjtextarea.append(" \n"); 9

18 50 for (int thisnumber = 1; 51 thisnumber <= tablesize; 52 thisnumber = thisnumber + 1) 53 displayjtextarea.append(" " + thisnumber + " x " + 54 multiplier + " = " 55 + thisnumber * multiplier + "\n"); 56 displayjtextarea.append(" \n"); 57 } // actionperformed public static void main(string [] args) { 60 TimesTable2 thetimestable = new TimesTable2(); 61 thetimestable.setvisible( true ); 62 } // main } // class TimesTable

19 6 TimesTableScroll 1 import javax.swing.jframe; 2 import java.awt.event.actionlistener; 3 import javax.swing.jtextfield; 4 import javax.swing.jtextarea; 5 import java.awt.container; 6 import javax.swing.jbutton; 7 import java.awt.borderlayout; 8 import java.awt.event.actionevent; 9 import javax.swing.jpanel; 10 import java.awt.gridlayout; 11 import javax.swing.jlabel; 12 import javax.swing.jscrollpane; /** 15 * Displays a times table using a {@link JTextArea JTextArea}. 16 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 17 */ 18 public class TimesTableScroll extends JFrame implements ActionListener { private JTextField multiplierjtextfield = new JTextField(5); 21 private JTextField tablesizejtextfield = new JTextField(5); 22 private JTextArea displayjtextarea = new JTextArea(15, 20); public TimesTableScroll() { 25 settitle("times Table"); 26 Container contents = getcontentpane(); 27 contents.setlayout(new BorderLayout()); 28 JPanel numberspanel = new JPanel( new GridLayout( 2, 2 ) ); 29 contents.add( numberspanel, BorderLayout.NORTH ); 30 numberspanel.add( new JLabel( "Multiplier:" ) ); 31 numberspanel.add( multiplierjtextfield ); numberspanel.add( new JLabel( "Table size:" ) ); 34 numberspanel.add( tablesizejtextfield ); 35 contents.add( new JScrollPane ( displayjtextarea), 36 BorderLayout.CENTER ); 37 JButton displayjbutton = new JButton("Display"); 38 contents.add(displayjbutton, BorderLayout.SOUTH); 39 displayjbutton.addactionlistener(this); setdefaultcloseoperation(exit_on_close); 42 pack(); 43 } // TimesTableScroll public void actionperformed(actionevent e) { 46 displayjtextarea.settext(""); 47 int multiplier = Integer.parseInt(multiplierJTextField.getText()); 48 int tablesize = Integer.parseInt(tableSizeJTextField.getText()); 49 displayjtextarea.append(" \n"); 11

20 50 displayjtextarea.append(" Times table for " + multiplier + "\n"); 51 displayjtextarea.append(" \n"); 52 for (int thisnumber = 1; 53 thisnumber <= tablesize; 54 thisnumber = thisnumber + 1) 55 displayjtextarea.append(" " + thisnumber + " x " + 56 multiplier + " = " 57 + thisnumber * multiplier + "\n"); 58 displayjtextarea.append(" \n"); 59 } // actionperformed public static void main(string [] args) { 62 TimesTableScroll thetimestable = new TimesTableScroll(); 63 thetimestable.setvisible( true ); 64 } // main } // class TimesTableScroll

21 COMP16121 Sample Code Lecture 4 Sean Bechhofer, University of Manchester, Manchester, UK sean.bechhofer@manchester.ac.uk 1 GCDWithNew 1 import java.awt.container; 2 import java.awt.flowlayout; 3 import java.awt.gridlayout; 4 import java.awt.event.actionevent; 5 import java.awt.event.actionlistener; 6 7 import javax.swing.jbutton; 8 import javax.swing.jframe; 9 import javax.swing.jlabel; 10 import javax.swing.jpanel; 11 import javax.swing.jtextfield; /** 14 * Calculates GCD of two integers. GUI allows spawning of a new window. 15 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class GCDWithNew extends JFrame implements ActionListener { private JTextField number1jtextfield = new JTextField(20); 20 private JTextField number2jtextfield = new JTextField(20); 21 private JTextField resultjtextfield = new JTextField(20); private JButton computejbutton = new JButton("Compute"); 24 private JButton newjbutton = new JButton("New"); public GCDWithNew() { 27 settitle("gcd"); 28 Container contents = getcontentpane(); 29 contents.setlayout(new GridLayout(0, 1)); JPanel numberfieldspanel = new JPanel(); 32 numberfieldspanel.setlayout( new GridLayout( 0, 2 ) ); 33 contents.add( numberfieldspanel ); numberfieldspanel.add(new JLabel("Number 1")); 36 numberfieldspanel.add(new JLabel("Number 2")); 37 numberfieldspanel.add(number1jtextfield); 38 numberfieldspanel.add(number2jtextfield); 39 1

22 40 JPanel buttonandresultpanel = new JPanel(); 41 buttonandresultpanel.setlayout( new GridLayout( 0, 2 ) ); 42 contents.add( buttonandresultpanel ); JPanel buttonpanel = new JPanel(); 45 buttonpanel.setlayout( new FlowLayout() ); 46 buttonandresultpanel.add( buttonpanel ); buttonpanel.add(computejbutton); 49 computejbutton.addactionlistener(this); buttonpanel.add(newjbutton); 52 newjbutton.addactionlistener(this); JPanel resultpanel = new JPanel(); 55 resultpanel.setlayout( new GridLayout( 0, 1 ) ); 56 buttonandresultpanel.add( resultpanel ); resultpanel.add(new JLabel("GCD of Number 1 and Number 2")); 59 resultpanel.add(resultjtextfield); 60 /* Specify appropriate behaviour when the window is closed */ 61 setdefaultcloseoperation(exit_on_close); pack(); 64 } // GCDWithNew public void actionperformed(actionevent e) { 67 if ( e.getsource() == computejbutton ) { 68 int number1 = Integer.parseInt(number1JTextField.getText()); 69 int number2 = Integer.parseInt(number2JTextField.getText()); 70 int thegcd = greatestcommondivisor(number1, number2); 71 resultjtextfield.settext("" + thegcd); 72 } else if ( e.getsource() == newjbutton ) { 73 GCDWithNew newone = new GCDWithNew(); 74 newone.setvisible( true ); 75 } 76 } // actionperformed private static int greatestcommondivisor(int multiple1ofgcd, 79 int multiple2ofgcd) { 80 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 81 while (multiple1ofgcd!= multiple2ofgcd) 82 if (multiple1ofgcd > multiple2ofgcd) 83 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 84 else 85 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 86 return multiple1ofgcd; 87 } // greatestcommondivisor public static void main(string [] args) { 2

23 90 GCDWithNew thegcd = new GCDWithNew(); 91 thegcd.setvisible( true ); 92 } // main 93 } // class GCDWithNew 3

24 2 GCDWithNew2 1 import javax.swing.jtextfield; 2 import javax.swing.jframe; 3 import java.awt.event.actionlistener; 4 import javax.swing.jbutton; 5 import java.awt.event.actionevent; 6 import java.awt.gridlayout; 7 import javax.swing.jlabel; 8 import java.awt.container; 9 import javax.swing.jpanel; 10 import java.awt.flowlayout; 11 import java.awt.point; /** 14 * Calculates GCD of two integers. GUI allows spawning of a new window. 15 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class GCDWithNew2 extends JFrame implements ActionListener { private JTextField number1jtextfield = new JTextField(20); 20 private JTextField number2jtextfield = new JTextField(20); 21 private JTextField resultjtextfield = new JTextField(20); private JButton computejbutton = new JButton("Compute"); 24 private JButton newjbutton = new JButton("New"); public GCDWithNew2() { 27 settitle("gcd"); 28 Container contents = getcontentpane(); 29 contents.setlayout(new GridLayout(0, 1)); JPanel numberfieldspanel = new JPanel(); 32 numberfieldspanel.setlayout( new GridLayout( 0, 2 ) ); 33 contents.add( numberfieldspanel ); numberfieldspanel.add(new JLabel("Number 1")); 36 numberfieldspanel.add(new JLabel("Number 2")); 37 numberfieldspanel.add(number1jtextfield); 38 numberfieldspanel.add(number2jtextfield); JPanel buttonandresultpanel = new JPanel(); 41 buttonandresultpanel.setlayout( new GridLayout( 0, 2 ) ); 42 contents.add( buttonandresultpanel ); JPanel buttonpanel = new JPanel(); 45 buttonpanel.setlayout( new FlowLayout() ); 46 buttonandresultpanel.add( buttonpanel ); buttonpanel.add(computejbutton); 49 computejbutton.addactionlistener(this); 4

25 50 51 buttonpanel.add(newjbutton); 52 newjbutton.addactionlistener(this); JPanel resultpanel = new JPanel(); 55 resultpanel.setlayout( new GridLayout( 0, 1 ) ); 56 buttonandresultpanel.add( resultpanel ); resultpanel.add(new JLabel("GCD of Number 1 and Number 2")); 59 resultpanel.add(resultjtextfield); 60 /* Specify appropriate behaviour when the window is closed */ 61 setdefaultcloseoperation(exit_on_close); pack(); 64 } // GCDWithNew public void actionperformed(actionevent e) { 67 if ( e.getsource() == computejbutton ) { 68 int number1 = Integer.parseInt(number1JTextField.getText()); 69 int number2 = Integer.parseInt(number2JTextField.getText()); 70 int thegcd = greatestcommondivisor(number1, number2); 71 resultjtextfield.settext("" + thegcd); 72 } else if ( e.getsource() == newjbutton ) { 73 GCDWithNew2 newone = new GCDWithNew2(); 74 newone.setlocation( new Point( this.getlocation().x + 10, 75 this.getlocation().y + 10 ) ); 76 newone.setvisible( true ); 77 } 78 } // actionperformed private static int greatestcommondivisor(int multiple1ofgcd, 81 int multiple2ofgcd) { 82 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 83 while (multiple1ofgcd!= multiple2ofgcd) 84 if (multiple1ofgcd > multiple2ofgcd) 85 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 86 else 87 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 88 return multiple1ofgcd; 89 } // greatestcommondivisor public static void main(string [] args) { 92 GCDWithNew2 thegcd = new GCDWithNew2(); 93 thegcd.setvisible( true ); 94 } // main 95 } // class GCDWithNew2 5

26 3 GCDWithNew3 1 import javax.swing.jtextfield; 2 import javax.swing.jframe; 3 import java.awt.event.actionlistener; 4 import javax.swing.jbutton; 5 import java.awt.event.actionevent; 6 import java.awt.gridlayout; 7 import javax.swing.jlabel; 8 import java.awt.container; 9 import javax.swing.jpanel; 10 import java.awt.flowlayout; 11 import java.awt.point; /** 14 * Calculates GCD of two integers. GUI allows spawning of a new window. 15 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class GCDWithNew3 extends JFrame implements ActionListener { private JTextField number1jtextfield = new JTextField(20); 20 private JTextField number2jtextfield = new JTextField(20); 21 private JTextField resultjtextfield = new JTextField(20); private JButton computejbutton = new JButton("Compute"); 24 private JButton newjbutton = new JButton("New"); public GCDWithNew3() { 27 settitle("gcd"); 28 Container contents = getcontentpane(); 29 contents.setlayout(new GridLayout(0, 1)); JPanel numberfieldspanel = new JPanel(); 32 numberfieldspanel.setlayout( new GridLayout( 0, 2 ) ); 33 contents.add( numberfieldspanel ); numberfieldspanel.add(new JLabel("Number 1")); 36 numberfieldspanel.add(new JLabel("Number 2")); 37 numberfieldspanel.add(number1jtextfield); 38 numberfieldspanel.add(number2jtextfield); JPanel buttonandresultpanel = new JPanel(); 41 buttonandresultpanel.setlayout( new GridLayout( 0, 2 ) ); 42 contents.add( buttonandresultpanel ); JPanel buttonpanel = new JPanel(); 45 buttonpanel.setlayout( new FlowLayout() ); 46 buttonandresultpanel.add( buttonpanel ); buttonpanel.add(computejbutton); 49 computejbutton.addactionlistener(this); 6

27 50 51 buttonpanel.add(newjbutton); 52 newjbutton.addactionlistener(this); JPanel resultpanel = new JPanel(); 55 resultpanel.setlayout( new GridLayout( 0, 1 ) ); 56 buttonandresultpanel.add( resultpanel ); resultpanel.add(new JLabel("GCD of Number 1 and Number 2")); 59 resultpanel.add(resultjtextfield); 60 /* Specify appropriate behaviour when the window is closed */ 61 setdefaultcloseoperation(dispose_on_close); pack(); 64 } // GCDWithNew public void actionperformed(actionevent e) { 67 if ( e.getsource() == computejbutton ) { 68 int number1 = Integer.parseInt(number1JTextField.getText()); 69 int number2 = Integer.parseInt(number2JTextField.getText()); 70 int thegcd = greatestcommondivisor(number1, number2); 71 resultjtextfield.settext("" + thegcd); 72 } else if ( e.getsource() == newjbutton ) { 73 GCDWithNew3 newone = new GCDWithNew3(); 74 newone.setlocation( new Point( this.getlocation().x + 10, 75 this.getlocation().y + 10 ) ); 76 newone.setvisible( true ); 77 } 78 } // actionperformed private static int greatestcommondivisor(int multiple1ofgcd, 81 int multiple2ofgcd) { 82 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 83 while (multiple1ofgcd!= multiple2ofgcd) 84 if (multiple1ofgcd > multiple2ofgcd) 85 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 86 else 87 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 88 return multiple1ofgcd; 89 } // greatestcommondivisor public static void main(string [] args) { 92 GCDWithNew3 thegcd = new GCDWithNew3(); 93 thegcd.setvisible( true ); 94 } // main 95 } // class GCDWithNew3 7

28 4 LogGUI 1 import javax.swing.jframe; 2 import java.awt.event.actionlistener; 3 import javax.swing.jtextfield; 4 import javax.swing.jtextarea; 5 import java.awt.container; 6 import javax.swing.jbutton; 7 import java.awt.borderlayout; 8 import java.awt.event.actionevent; 9 import java.awt.gridlayout; 10 import javax.swing.jpanel; 11 import javax.swing.jlabel; /** 14 * Simple GUI that allows users to log messages. 15 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class LogGUI extends JFrame implements ActionListener { /* GUI Components */ 20 private JTextField labelfield = new JTextField("label"); 21 private JTextArea messagetextarea = new JTextArea(15, 20); 22 private JButton logbutton = new JButton("Log"); 23 private JButton newbutton = new JButton("New"); /* The LogBook that we will log messages to */ 26 private LogBook logbook; /** 29 * Creates a new <code>loggui</code> instance. 30 * 31 alogbook The <code>logbook</code> that the GUI is 32 * connected to. 33 */ 34 public LogGUI( LogBook alogbook ) { 35 logbook = alogbook; 36 settitle("log"); 37 Container contents = getcontentpane(); 38 contents.setlayout(new BorderLayout()); JPanel labelpanel = new JPanel( new GridLayout( 0, 2 ) ); 41 labelpanel.add( new JLabel( "Label:" ) ); 42 labelpanel.add( labelfield ); contents.add( labelpanel, BorderLayout.NORTH ); 46 contents.add( messagetextarea, BorderLayout.CENTER ); JPanel buttonpanel = new JPanel( new GridLayout( 0, 2 ) ); 49 buttonpanel.add( logbutton ); 8

29 50 logbutton.addactionlistener( this ); buttonpanel.add( newbutton ); 53 newbutton.addactionlistener( this ); contents.add( buttonpanel, BorderLayout.SOUTH ); setdefaultcloseoperation( DISPOSE_ON_CLOSE ); 58 pack(); 59 } // LogGUI public void actionperformed( ActionEvent e ) { 62 if ( e.getsource() == logbutton ) { 63 /* Log the message */ 64 logbook.logmessage( labelfield.gettext(), 65 messagetextarea.gettext() ); 66 /* Now clear the text */ 67 messagetextarea.settext(""); 68 } else if ( e.getsource() == newbutton ) { 69 /* Create a new GUI with the same logbook */ 70 LogGUI newloggui = new LogGUI( logbook ); 71 newloggui.setvisible( true ); 72 } 73 } // actionperformed public static void main(string [] args) { 76 LogBook logbook = new LogBook( "My LogBook" ); 77 LogGUI theloggui = new LogGUI( logbook ); 78 theloggui.setvisible( true ); 79 } // main } // class LogGUI

30 5 LogBook 1 import java.text.simpledateformat; 2 import java.util.date; 3 4 /** 5 * A class that records information. 6 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 7 */ 8 public class LogBook { 9 10 /* private String logname; */ 11 private String log; 12 private SimpleDateFormat dateformat; /** 15 * Create a new <code>logbook</code> instance. 16 * 17 alogname name of the room. 18 */ 19 public LogBook( String alogname ) { 20 /* logname = alogname; */ 21 dateformat = new SimpleDateFormat( "hh:mm:ss " ); 22 log = ""; 23 } /** 26 * Log a message. 27 * 28 user a <code>string</code> value 29 message a <code>string</code> value 30 */ 31 public void logmessage( String label, 32 String message ) { 33 Date now = new Date(); String logmessage = "Time: " + dateformat.format( now ) + "\n" + 36 "Label: " + label + "\n"+ 37 "Note: " + message; /* Add the message to the log */ 40 log = log + logmessage + "\n"; /* Display the message */ 43 System.out.println( " " ); 44 System.out.println( logmessage ); 45 System.out.println( " " ); 46 } 47 } 10

31 6 LogGUI2 1 import javax.swing.jframe; 2 import java.awt.event.actionlistener; 3 import javax.swing.jtextfield; 4 import javax.swing.jtextarea; 5 import java.awt.container; 6 import javax.swing.jbutton; 7 import java.awt.borderlayout; 8 import java.awt.event.actionevent; 9 import java.awt.gridlayout; 10 import javax.swing.jpanel; 11 import javax.swing.jlabel; /** 14 * Simple GUI2 that allows labels to log messages. 15 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class LogGUI2 extends JFrame implements ActionListener { /* GUI Components */ 20 private JTextField labelfield = new JTextField("label"); 21 private JTextArea messagetextarea = new JTextArea(15, 20); 22 private JButton logbutton = new JButton("Log"); 23 private JButton newbutton = new JButton("New"); 24 private JButton transcriptbutton = new JButton("Transcript"); /* The LogBook that we will log messages to */ 27 private LogBook2 logbook; /** 30 * Creates a new <code>loggui2</code> instance. 31 * 32 alogbook The <code>logbook</code> that the GUI is 33 * connected to. 34 */ 35 public LogGUI2( LogBook2 alogbook ) { 36 logbook = alogbook; 37 settitle("log"); 38 Container contents = getcontentpane(); 39 contents.setlayout(new BorderLayout()); JPanel labelpanel = new JPanel( new GridLayout( 0, 2 ) ); 42 labelpanel.add( new JLabel( "Label:" ) ); 43 labelpanel.add( labelfield ); contents.add( labelpanel, BorderLayout.NORTH ); 47 contents.add( messagetextarea, BorderLayout.CENTER ); JPanel buttonpanel = new JPanel( new GridLayout( 0, 3 ) ); 11

32 50 buttonpanel.add( logbutton ); 51 logbutton.addactionlistener( this ); buttonpanel.add( newbutton ); 54 newbutton.addactionlistener( this ); buttonpanel.add( transcriptbutton ); 57 transcriptbutton.addactionlistener( alogbook ); contents.add( buttonpanel, BorderLayout.SOUTH ); setdefaultcloseoperation( DISPOSE_ON_CLOSE ); 62 pack(); 63 } // LogGUI public void actionperformed( ActionEvent e ) { 66 if ( e.getsource() == logbutton ) { 67 /* Log the message */ 68 logbook.logmessage( labelfield.gettext(), 69 messagetextarea.gettext() ); 70 /* Now clear the text */ 71 messagetextarea.settext(""); 72 } else if ( e.getsource() == newbutton ) { 73 /* Create a new GUI with the same logbook */ 74 LogGUI2 newloggui = new LogGUI2( logbook ); 75 newloggui.setvisible( true ); 76 } 77 } // actionperformed public static void main(string [] args) { 80 LogBook2 logbook = new LogBook2( "LogBook" ); 81 LogGUI2 theloggui = new LogGUI2( logbook ); 82 theloggui.setvisible( true ); 83 } // main } // class LogGUI

33 7 LogBook2 1 import java.text.simpledateformat; 2 import java.util.date; 3 import java.awt.event.actionlistener; 4 import java.awt.event.actionevent; 5 6 /** 7 * A class that records information. 8 <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 9 */ 10 public class LogBook2 implements ActionListener { /* private String logname; */ 13 private String log; 14 private SimpleDateFormat dateformat; /** 17 * Create a new <code>logbook</code> instance. 18 * 19 alogname name of the room. 20 */ 21 public LogBook2( String alogname ) { 22 /* logname = alogname; */ 23 dateformat = new SimpleDateFormat( "hh:mm:ss " ); 24 log = ""; 25 } /** 28 * Log a message. 29 * 30 user a <code>string</code> value 31 message a <code>string</code> value 32 */ 33 public void logmessage( String label, 34 String message ) { 35 Date now = new Date(); String logmessage = "Time: " + dateformat.format( now ) + "\n" + 38 "Label: " + label + "\n"+ 39 "Note: " + message; /* Add the message to the log */ 42 log = log + logmessage + "\n"; 43 } public void actionperformed( ActionEvent e ) { 46 System.out.println("========================"); 47 System.out.println( log ); 48 System.out.println("========================"); 49 } 13

34 50 } 14

Graphical User Interfaces in Java

Graphical User Interfaces in Java COMP16121 Graphical User Interfaces in Java COMP16121 Sean Bechhofer sean.bechhofer@manchester.ac.uk Why? With the dominance of windows-based systems, Graphical User Interfaces (GUIs) are everywhere. They

More information

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau List of Slides 1 Title 2 Chapter 13: Graphical user interfaces 3 Chapter aims 4 Section 2: Example:Hello world with a GUI 5 Aim 6 Hello world with a GUI 7 Hello world with a GUI 8 Package: java.awt and

More information

SampleApp.java. Page 1

SampleApp.java. Page 1 SampleApp.java 1 package msoe.se2030.sequence; 2 3 /** 4 * This app creates a UI and processes data 5 * @author hornick 6 */ 7 public class SampleApp { 8 private UserInterface ui; // the UI for this program

More information

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver Building a GUI in Java with Swing CITS1001 extension notes Rachel Cardell-Oliver Lecture Outline 1. Swing components 2. Building a GUI 3. Animating the GUI 2 Swing A collection of classes of GUI components

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. SOLUTION HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2006 FINAL EXAMINATION 7pm to 10pm, 19 DECEMBER 2006, Jeffrey Hall 1 st Floor Instructor:

More information

PIC 20A GUI with swing

PIC 20A GUI with swing PIC 20A GUI with swing Ernest Ryu UCLA Mathematics Last edited: November 22, 2017 Hello swing Let s create a JFrame. import javax. swing.*; public class Test { public static void main ( String [] args

More information

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University Java Swing Recitation 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University Announcements Project 8 is out Milestone due on Dec 3rd, 10:00 pm Final due on Dec 10th, 10:00 pm No classes,

More information

Example: Building a Java GUI

Example: Building a Java GUI Steven Zeil October 25, 2013 Contents 1 Develop the Model 2 2 Develop the layout of those elements 3 3 Add listeners to the elements 9 4 Implement custom drawing 12 1 The StringArt Program To illustrate

More information

Example: Building a Java GUI

Example: Building a Java GUI Steven Zeil October 25, 2013 Contents 1 Develop the Model 3 2 Develop the layout of those elements 4 3 Add listeners to the elements 12 4 Implement custom drawing 15 1 The StringArt Program To illustrate

More information

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. Page 1 of 16 SOLUTION HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2005 FINAL EXAMINATION 9am to 12noon, 19 DECEMBER 2005 Instructor: Alan McLeod

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2007 FINAL EXAMINATION 7pm to 10pm, 10 DECEMBER 2007, Jeffery Hall Instructor: Alan McLeod If the

More information

DAFTAR LAMPIRAN. Source Code Java Aplikasi Keyword to Image Renamer Split

DAFTAR LAMPIRAN. Source Code Java Aplikasi Keyword to Image Renamer Split DAFTAR LAMPIRAN Source Code Java Aplikasi Keyword to Image Renamer Split Source Code Menu Utama package spin_text; import java.awt.color; import java.awt.event.actionevent; import java.awt.event.actionlistener;

More information

Agenda. Container and Component

Agenda. Container and Component Agenda Types of GUI classes/objects Step-by-step guide to create a graphic user interface Step-by-step guide to event-handling PS5 Problem 1 PS5 Problem 2 Container and Component There are two types of

More information

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords Goals By the end of this lesson, you should: Lecture 7 More GUI programming 1. Be able to write Java s with JTextField, JList, JCheckBox and JRadioButton components 2. Be able to implement a ButtonGroup

More information

Graphical User Interfaces in Java - SWING

Graphical User Interfaces in Java - SWING Graphical User Interfaces in Java - SWING Graphical User Interfaces (GUI) Each graphical component that the user can see on the screen corresponds to an object of a class Component: Window Button Menu...

More information

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1 Datenbank-Praktikum Universität zu Lübeck Sommersemester 2006 Lecture: Swing Ho Ngoc Duc 1 Learning objectives GUI applications Font, Color, Image Running Applets as applications Swing Components q q Text

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2006 FINAL EXAMINATION 7pm to 10pm, 19 DECEMBER 2006, Jeffrey Hall 1 st Floor Instructor: Alan

More information

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid Systems Programming Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid Leganés, 21st of March, 2014. Duration: 75 min. Full

More information

Building Java Programs Bonus Slides

Building Java Programs Bonus Slides Building Java Programs Bonus Slides Graphical User Interfaces Copyright (c) Pearson 2013. All rights reserved. Graphical input and output with JOptionPane JOptionPane An option pane is a simple dialog

More information

69 Section 7: Example:Single times table with exception catching 70 Aim 74 Single times table with exception catching

69 Section 7: Example:Single times table with exception catching 70 Aim 74 Single times table with exception catching List of Slides 1 Title 2 Chapter 15: Exceptions 3 Chapter aims 4 Section 2: Example:Age next year revisited 5 Aim 6 Age next year revisited 7 Age next year revisited 8 Exception 9 Trying it 10 Trying it

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC124, WINTER TERM, 2009 FINAL EXAMINATION 7pm to 10pm, 18 APRIL 2009, Dunning Hall Instructor: Alan McLeod If the

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. Solution HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2007 FINAL EXAMINATION 7pm to 10pm, 10 DECEMBER 2007, Jeffery Hall Instructor: Alan McLeod

More information

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions. Parts of a Contract Syntax - Method signature Method name Parameter list Return type Semantics - Comments Preconditions: requirements placed on the caller Postconditions: what the method modifies and/or

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. QUEEN'S UNIVERSITY SCHOOL OF COMPUTING HAND IN Answers Are Recorded on Question Paper CMPE212, FALL TERM, 2012 FINAL EXAMINATION 18 December 2012, 2pm Instructor: Alan McLeod If the instructor is unavailable

More information

COMP16121 Notes on Mock Exam Questions

COMP16121 Notes on Mock Exam Questions COMP16121 Notes on Mock Exam Questions December 2016 Mock Exam Attached you will find a Mock multiple choice question (MCQ) exam for COMP16121, to assist you in preparing for the actual COMP16121 MCQ Exam

More information

Window Interfaces Using Swing. Chapter 12

Window Interfaces Using Swing. Chapter 12 Window Interfaces Using Swing 1 Reminders Project 7 due Nov 17 @ 10:30 pm Project 6 grades released: regrades due by next Friday (11-18-2005) at midnight 2 GUIs - Graphical User Interfaces Windowing systems

More information

CSCI 201L Midterm Written Summer % of course grade

CSCI 201L Midterm Written Summer % of course grade CSCI 201L Summer 2016 10% of course grade 1. Abstract Classes and Interfaces Give two differences between an interface and an abstract class in which all of the methods are abstract. (0.5% + 0.5%) 2. Serialization

More information

AP CS Unit 11: Graphics and Events

AP CS Unit 11: Graphics and Events AP CS Unit 11: Graphics and Events This packet shows how to create programs with a graphical interface in a way that is consistent with the approach used in the Elevens program. Copy the following two

More information

Part I: Learn Common Graphics Components

Part I: Learn Common Graphics Components OOP GUI Components and Event Handling Page 1 Objectives 1. Practice creating and using graphical components. 2. Practice adding Event Listeners to handle the events and do something. 3. Learn how to connect

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. SOLUTION HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC124, WINTER TERM, 2009 FINAL EXAMINATION 7pm to 10pm, 18 APRIL 2009, Dunning Hall Instructor: Alan McLeod

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. SOLUTION HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2008 FINAL EXAMINATION 7pm to 10pm, 17 DECEMBER 2008, Grant Hall Instructor: Alan McLeod

More information

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling Handout 12 CS603 Object-Oriented Programming Fall 15 Page 1 of 12 Handout 14 Graphical User Interface (GUI) with Swing, Event Handling The Swing library (javax.swing.*) Contains classes that implement

More information

Introduction. Introduction

Introduction. Introduction Introduction Many Java application use a graphical user interface or GUI (pronounced gooey ). A GUI is a graphical window or windows that provide interaction with the user. GUI s accept input from: the

More information

MIT AITI Swing Event Model Lecture 17

MIT AITI Swing Event Model Lecture 17 MIT AITI 2004 Swing Event Model Lecture 17 The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the user. But how do GUIs interact with users? How do applications

More information

CS 251 Intermediate Programming GUIs: Event Listeners

CS 251 Intermediate Programming GUIs: Event Listeners CS 251 Intermediate Programming GUIs: Event Listeners Brooke Chenoweth University of New Mexico Fall 2017 What is an Event Listener? A small class that implements a particular listener interface. Listener

More information

CSIS 10A Assignment 7 SOLUTIONS

CSIS 10A Assignment 7 SOLUTIONS CSIS 10A Assignment 7 SOLUTIONS Read: Chapter 7 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment,

More information

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. Page 1 of 16 HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2005 FINAL EXAMINATION 9am to 12noon, 19 DECEMBER 2005 Instructor: Alan McLeod If

More information

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing Rui Moreira Some useful links: http://java.sun.com/docs/books/tutorial/uiswing/toc.html http://www.unix.org.ua/orelly/java-ent/jfc/

More information

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 12 A First Look at GUI Applications Chapter Topics 12.1 Introduction 12.2 Creating Windows 12.3 Equipping GUI Classes

More information

Dr. Hikmat A. M. AbdelJaber

Dr. Hikmat A. M. AbdelJaber Dr. Hikmat A. M. AbdelJaber JWindow: is a window without a title bar or move controls. The program can move and resize it, but the user cannot. It has no border at all. It optionally has a parent JFrame.

More information

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1. GUI Applications The C implementation of Python comes with Tkinter for writing Graphical User Interfaces (GUIs). The GUI toolkit that you get automatically with Jython is Swing, which is included with

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2010 FINAL EXAMINATION 11 DECEMBER 2010, 9am SOLUTION HAND IN Answers Are Recorded on Question Paper Instructor: Alan McLeod If the instructor

More information

Graphical User Interface

Graphical User Interface Lecture 10 Graphical User Interface An introduction Sahand Sadjadee sahand.sadjadee@liu.se Programming Fundamentals 725G61 http://www.ida.liu.se/~725g61/ Department of Computer and Information Science

More information

CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2

CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2 Question 1 30 30 60 60 90 20 20 40 40 60 Question 2 a. b. public Song(String title, String artist, int length, String composer) { this.title = title; this.artist = artist; this.length = length; this.composer

More information

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS Glossary of Terms 2-4 Step by Step Instructions 4-7 HWApp 8 HWFrame 9 Never trust a computer

More information

CSCI 201L Midterm Written SOLUTION Summer % of course grade

CSCI 201L Midterm Written SOLUTION Summer % of course grade CSCI 201L SOLUTION Summer 2016 10% of course grade 1. Abstract Classes and Interfaces Give two differences between an interface and an abstract class in which all of the methods are abstract. (0.5% + 0.5%)

More information

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphic User Interfaces. - GUI concepts - Swing - AWT Graphic User Interfaces - GUI concepts - Swing - AWT 1 What is GUI Graphic User Interfaces are used in programs to communicate more efficiently with computer users MacOS MS Windows X Windows etc 2 Considerations

More information

JAVA NOTES GRAPHICAL USER INTERFACES

JAVA NOTES GRAPHICAL USER INTERFACES 1 JAVA NOTES GRAPHICAL USER INTERFACES Terry Marris 24 June 2001 5 TEXT AREAS 5.1 LEARNING OUTCOMES By the end of this lesson the student should be able to understand how to get multi-line input from the

More information

AppBisect > PrBisect > class Functie. AppBisect > PrBisect > class Punct. public class Functie { double x(double t) { return t;

AppBisect > PrBisect > class Functie. AppBisect > PrBisect > class Punct. public class Functie { double x(double t) { return t; 1 AppBisect > PrBisect > class Punct public class Punct { double x,y; public Punct(double x, double y) { this.x = x; this.y = y; public void setx(double x) { this.x = x; public double getx() { return x;

More information

More about GUIs GEEN163

More about GUIs GEEN163 More about GUIs GEEN163 The best programmers are not marginally better than merely good ones. They are an order-ofmagnitude better, measured by whatever standard: conceptual creativity, speed, ingenuity

More information

// autor igre Ivan Programerska sekcija package mine;

// autor igre Ivan Programerska sekcija package mine; // autor igre Ivan Bauk @ Programerska sekcija package mine; import java.awt.color; import java.awt.flowlayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener;

More information

User interfaces and Swing

User interfaces and Swing User interfaces and Swing Overview, applets, drawing, action listening, layout managers. APIs: java.awt.*, javax.swing.*, classes names start with a J. Java Lectures 1 2 Applets public class Simple extends

More information

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners GUI (Graphic User Interface) Programming Part 2 (Chapter 8) Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons, text fields, and the

More information

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); } A Calculator Project This will be our first exposure to building a Graphical User Interface (GUI) in Java The functions of the calculator are self-evident The Calculator class creates a UserInterface Class

More information

Systems Programming Graphical User Interfaces

Systems Programming Graphical User Interfaces Systems Programming Graphical User Interfaces Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: José Jesús García Rueda Systems Programming GUIs based on Java

More information

Answer on question #61311, Programming & Computer Science / Java

Answer on question #61311, Programming & Computer Science / Java Answer on question #61311, Programming & Computer Science / Java JSP JSF for completion Once the user starts the thread by clicking a button, the program must choose a random image out of an image array,

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2009 FINAL EXAMINATION 14 DECEMBER 2009 Instructor: Alan McLeod If the instructor is unavailable

More information

CS 251 Intermediate Programming GUIs: Components and Layout

CS 251 Intermediate Programming GUIs: Components and Layout CS 251 Intermediate Programming GUIs: Components and Layout Brooke Chenoweth University of New Mexico Fall 2017 import javax. swing.*; Hello GUI public class HelloGUI extends JFrame { public HelloGUI ()

More information

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Graphics programming COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Overview Aims To provide an overview of Swing and the AWT To show how to build

More information

CMP 326 Midterm Fall 2015

CMP 326 Midterm Fall 2015 CMP 326 Midterm Fall 2015 Name: 1) (30 points; 5 points each) Write the output of each piece of code. If the code gives an error, write any output that would happen before the error, and then write ERROR.

More information

Chapter 9 Designing Graphical User Interfaces (GUIs)

Chapter 9 Designing Graphical User Interfaces (GUIs) Chapter 9 Designing Graphical User Interfaces (GUIs) Overview The basics of GUIs in Java A tour of Java GUI libraries Containers and components Swing: the full picture Layout managers Understanding events

More information

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1 Java Swing based on slides by: Walter Milner Java Swing Walter Milner 2005: Slide 1 What is Swing? A group of 14 packages to do with the UI 451 classes as at 1.4 (!) Part of JFC Java Foundation Classes

More information

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus Chapter 13 Lab Advanced GUI Applications Lab Objectives Be able to add a menu to the menu bar Be able to use nested menus Be able to add scroll bars, giving the user the option of when they will be seen.

More information

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions University of Cape Town Department of Computer Science Computer Science CSC117F Solutions Class Test 4 Wednesday 14 May 2003 Marks: 40 Approximate marks per question are shown in brackets Time: 40 minutes

More information

GUI Forms and Events, Part II

GUI Forms and Events, Part II GUI Forms and Events, Part II Quick Start Compile step once always mkdir labs javac PropertyTax6.java cd labs Execute step mkdir 6 java PropertyTax6 cd 6 cp../5/propertytax5.java PropertyTax6.java Submit

More information

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Announcements A3 is up, due Friday, Oct 10 Prelim 1 scheduled for Oct 16 if you have a conflict, let us know now 2 Interactive

More information

Class 16: The Swing Event Model

Class 16: The Swing Event Model Introduction to Computation and Problem Solving Class 16: The Swing Event Model Prof. Steven R. Lerman and Dr. V. Judson Harward 1 The Java Event Model Up until now, we have focused on GUI's to present

More information

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted. York University AS/AK/ITEC 2610 3.0 All Sections OBJECT-ORIENTED PROGRAMMING Midterm Test Duration: 90 Minutes This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. QUEEN'S UNIVERSITY SCHOOL OF COMPUTING HAND IN Answers Are Recorded on Question Paper CISC124, WINTER TERM, 2012 FINAL EXAMINATION 9am to 12pm, 26 APRIL 2012 Instructor: Alan McLeod If the instructor is

More information

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

APPENDIX. public void cekroot() { System.out.println(nilai root : +root.data); } APPENDIX CLASS NODE AS TREE OBJECT public class Node public int data; public Node left; public Node right; public Node parent; public Node(int i) data=i; PROCEDURE BUILDING TREE public class Tree public

More information

Java. GUI building with the AWT

Java. GUI building with the AWT Java GUI building with the AWT AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses the controls defined by your OS therefore

More information

AnimatedImage.java. Page 1

AnimatedImage.java. Page 1 1 import javax.swing.japplet; 2 import javax.swing.jbutton; 3 import javax.swing.jpanel; 4 import javax.swing.jcombobox; 5 import javax.swing.jlabel; 6 import javax.swing.imageicon; 7 import javax.swing.swingutilities;

More information

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages. 1 CS257 Computer Science I Kevin Sahr, PhD Lecture 14: Graphical User Interfaces Command-Line Applications 2 The programs we've explored thus far have been text-based applications A Java application is

More information

Object-Oriented Software Engineering Re-exam, 2012 (Also Object-Oriented Analysis, Design and Programming, Re-exam, 2012)

Object-Oriented Software Engineering Re-exam, 2012 (Also Object-Oriented Analysis, Design and Programming, Re-exam, 2012) Object-Oriented Software Engineering Re-exam, 2012 (Also Object-Oriented Analysis, Design and Programming, Re-exam, 2012) Medialogy, 4 th Semester, Aalborg Thursday 23 August 2012, 09.00 12.00 Instructions

More information

Based on slides by Prof. Burton Ma

Based on slides by Prof. Burton Ma Based on slides by Prof. Burton Ma 1 TV - on : boolean - channel : int - volume : int + power(boolean) : void + channel(int) : void + volume(int) : void Model View Controller RemoteControl + togglepower()

More information

Section Basic graphics

Section Basic graphics Chapter 16 - GUI Section 16.1 - Basic graphics Java supports a set of objects for developing graphical applications. A graphical application is a program that displays drawings and other graphical objects.

More information

Java Just in Time: Collected concepts after chapter 18

Java Just in Time: Collected concepts after chapter 18 Java Just in Time: Collected concepts after chapter 18 John Latham, School of Computer Science, Manchester University, UK. April 15, 2011 Contents 1 Computer basics 18000 1.1 Computer basics: hardware

More information

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Graphical User Interfaces. Swing. Jose Jesus García Rueda Graphical User Interfaces. Swing Jose Jesus García Rueda Introduction What are the GUIs? Well known examples Basic concepts Graphical application. Containers. Actions. Events. Graphical elements: Menu

More information

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921 CHAPTER 17 Swing I 17.1 EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921 17.2 BUTTONS, EVENTS, AND OTHER SWING BASICS 923 Example: A Simple Window 923 Buttons 930 Action Listeners and Action Events

More information

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 143 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946 17.1 Event-Driven Programming 925 Events and Listeners 925 17.2 Buttons, Events, and Other Swing Basics 926 Example: A Simple Window 927 Buttons 933 Action Listeners and Action Events 934 Example: A Better

More information

PART 22. Java GUI Library SWT GUI Library SWING GUI Library Swing First Program

PART 22. Java GUI Library SWT GUI Library SWING GUI Library Swing First Program PART 22 Java GUI Library 22.1 SWT GUI Library There is also another GUI library for the Java programming language. It is called SWT (The Standard widget toolkit). The SWT library was initially developed

More information

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { } CBOP3203 What Is an Event? Events Objects that describe what happened Event Sources The generator of an event Event Handlers A method that receives an event object, deciphers it, and processes the user

More information

Java Never Ends MULTITHREADING 958 Example: A Nonresponsive GUI 959

Java Never Ends MULTITHREADING 958 Example: A Nonresponsive GUI 959 CHAPTER 19 Java Never Ends 19.1 MULTITHREADING 958 Example: A Nonresponsive GUI 959 Thread.sleep 959 The getgraphics Method 963 Fixing a Nonresponsive Program Using Threads 964 Example: A Multithreaded

More information

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts SE1021 Exam 2 Name: You may use a note-sheet for this exam. But all answers should be your own, not from slides or text. Review all questions before you get started. The exam is printed single-sided. Write

More information

Points Missed on Page page 1 of 8

Points Missed on Page page 1 of 8 Midterm II - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total. Name: ID: Problem #1 (8 points) Rewrite the following code segment using a for loop instead of a while loop (that is

More information

Graphical User Interfaces. Comp 152

Graphical User Interfaces. Comp 152 Graphical User Interfaces Comp 152 Procedural programming Execute line of code at a time Allowing for selection and repetition Call one function and then another. Can trace program execution on paper from

More information

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Frames, GUI and events Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Introduction to Swing The Java AWT (Abstract Window Toolkit)

More information

Chapter 13 Lab Advanced GUI Applications

Chapter 13 Lab Advanced GUI Applications Gaddis_516907_Java 4/10/07 2:10 PM Page 113 Chapter 13 Lab Advanced GUI Applications Objectives Be able to add a menu to the menu bar Be able to use nested menus Be able to add scroll bars, giving the

More information

Final Exam CS 251, Intermediate Programming December 13, 2017

Final Exam CS 251, Intermediate Programming December 13, 2017 Final Exam CS 251, Intermediate Programming December 13, 2017 Name: NetID: Answer all questions in the space provided. Write clearly and legibly, you will not get credit for illegible or incomprehensible

More information

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp DM550 / DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ GRAPHICAL USER INTERFACES 2 HelloWorld Reloaded

More information

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

An array is a type of variable that is able to hold more than one piece of information under a single variable name. Arrays An array is a type of variable that is able to hold more than one piece of information under a single variable name. Basically you are sub-dividing a memory box into many numbered slots that can

More information

Chapter 7: A First Look at GUI Applications

Chapter 7: A First Look at GUI Applications Chapter 7: A First Look at GUI Applications Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All

More information

CS 134 Programming Exercise 7:

CS 134 Programming Exercise 7: CS 134 Programming Exercise 7: Scribbler Objective: To gain more experience using recursion and recursive data structures. This week, you will be implementing a program we call Scribbler. You have seen

More information

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers Course Name: Advanced Java Lecture 19 Topics to be covered Java Swing Lists Trees Tables Styled Text Components Progress Indicators Component Organizers AWT to Swing AWT: Abstract Windowing Toolkit import

More information

Part 3: Graphical User Interface (GUI) & Java Applets

Part 3: Graphical User Interface (GUI) & Java Applets 1,QWURGXFWLRQWR-DYD3URJUDPPLQJ (( Part 3: Graphical User Interface (GUI) & Java Applets EE905-GUI 7RSLFV Creating a Window Panels Event Handling Swing GUI Components ƒ Layout Management ƒ Text Field ƒ

More information

Class 14: Introduction to the Swing Toolkit

Class 14: Introduction to the Swing Toolkit Introduction to Computation and Problem Solving Class 14: Introduction to the Swing Toolkit Prof. Steven R. Lerman and Dr. V. Judson Harward 1 Class Preview Over the next 5 lectures, we will introduce

More information

1.00 Lecture 14. Lecture Preview

1.00 Lecture 14. Lecture Preview 1.00 Lecture 14 Introduction to the Swing Toolkit Lecture Preview Over the next 5 lectures, we will introduce you to the techniques necessary to build graphic user interfaces for your applications. Lecture

More information