New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Week 13 Notes

by: Taylor Kahl

Week 13 Notes CSC 2310

Taylor Kahl
GPA 4.21

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Notes on the beginning of chapter 6 - GUI
Princliples of Computer Programming
Kebina Manandhar
Class Notes
25 ?




Popular in Princliples of Computer Programming

Popular in ComputerScienence

This 5 page Class Notes was uploaded by Taylor Kahl on Sunday April 17, 2016. The Class Notes belongs to CSC 2310 at Georgia State University taught by Kebina Manandhar in Winter 2016. Since its upload, it has received 9 views. For similar materials see Princliples of Computer Programming in ComputerScienence at Georgia State University.

Similar to CSC 2310 at GSU


Reviews for Week 13 Notes


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 04/17/16
Chapter 6: Graphical User Interfaces  GUI – graphical user interface. Allows the user to interact with a program using graphic elements like windows, buttons, menus and text fields  Objects needed to create a GUI in Java: o components – defines a screen element that is used to display information or interact with the user  Example: push buttons; text fields; scroll bars  container – a special component that holds/organizes other components o events – represent an occurrence, often corresponding to user actions  Example: an event indicating that the user has pressed a button  event-driven programs are oriented around a GUI and respond to events from the user o listener – waits for an event to occur & responds when it does  Requirements for a Java program that uses a GUI: o set up necessary components o implement listener classes; define what happens when events occur o relate listeners to the components that generate the events they listen for  Java components and GUI-related classes are defined in 2 packages: o java.awt o javax.swing – newer; provides more versatile components Frames & Panels:  Frame – a container that displays GUI-based Java applications o displayed as a separate window w/ its own title bar o can be moved/resized w/ the mouse o buttons for minimizing, maximizing, & closing the frame o defined by the JFrame class  Panel – a container that can’t be displayed on its own o must be added to another container to be displayed o organizes other components o defined by the JPanel class  Containers are either heavyweight or lightweight o heavyweight – managed by the operating system  more complex than lightweight  Example: frame o lightweight – managed by the Java program itself  Example: panel  Frames have multiple panes which are responsible for characteristics of the frame window o Example: a frame’s content pane displays all visible elements of a Java interface  GUI-based Java applications usually create a frame that contains a panel, which can be further organized into more panels Labels, Buttons & Action Events:  Label – a component that displays a line of text or an image o not interactive w/ the user o from the JLabel class  Push button – component that allows user to initiate an action by pressing it w/ the mouse o from the JButton class o calling the JButton constructor takes a String parameter for the text on the button  Action event – generated by pushing a JButton o the event will cause the program to respond o to respond to the event, a listener must be created to “listen” for that event o the listener is represented by writing a new class for it Code for a GUI-based program:  The code below is for a program which creates a frame named “Push Counter”  the frame contains a panel o the panel contains:  a button reading “Push Me!”  a label reading “Pushes : “ + count  the listener ButtonListener is associated with the button push o when the button is pressed, the listener responds by updating count and the label  ButtonListener is represented in its own class o this class is written as an inner class within the PushCounterPanel class  an inner class can access the members of the class that contains it  ButtonListener implements the ActionListener class, which contains 1 method: o actionPerformed(ActionEvent event) import javax.swing.JFrame; public class PushCounter { //this class will create & display the main frame public static void main(String[] args) { JFrame frame = new JFrame(“Push Counter”);//constructs a frame with “Push //Counter” in the title bar frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //sets the close button to //end the program PushCounterPanel panel = new PushCounterPanel(); frame.getContentPane().add(panel); //adds panel to the frame’s content pane frame.pack(); //pack() method sizes the frame based on its contents frame.setVisible(true); //setVisible() method allows the frame to be displayed } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PushCounterPanel extends JPanel { //a class for constructing new panels private int count; private JButton push; private JLabel label; public PushCounterPanel() { //a constructor method for initializing panels count = 0; push = new JButton(“Push Me!”); push.addActionListener(new ButtonListener()); //associates push w/ the ButtonListener label = new JLabel(“Pushes: “ + count); add(push); //adds push to the panel add(label); //adds label to the panel //the order in which components are added matters – push will appear to the left of label setBackground(Color.cyan); setPrefferedSize(new Dimension(300, 40); } private class ButtonListener implements ActionListener { //this class represents an action event listener, which “listens” for the push JButton being pressed public void actionPerformed(ActionEvent event) { count++; label.setText(“Pushes: “ + count); } } }  Listeners can “listen” for more than 1 event  In this example, the frame contains 2 buttons, one that says “left” and one that says “right” o the label will display “left” or “right” depending on which button was just pressed o the listener must respond to the left & right buttons and determine which button was pressed private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { if (event.getSource()==left) //getSource() returns the component that generated the event label.setText("Left"); else label.setText("Right"); } } }  Note that another option would be to write 2 ButtonListener classes, 1 which listens for the right button and 1 which listens for the left button


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Kyle Maynard Purdue

"When you're taking detailed notes and trying to help everyone else out in the class, it really helps you learn and understand the I made $280 on my first study guide!"

Steve Martinelli UC Los Angeles

"There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.