Docsity

Lecture Slides on Graphical User Interfaces | CS 112, Study notes of Computer Science

Toggle sidebar
bg1
1
Graphical User Interfaces
Containers and Components
• Frame - OS-managed container used to
display GUI-based java apps
• Panel - Container used to organized other
GUI components (buttons, labels, etc)
• Label - Component used to display text
//Similar to Authority.java from Lewis/Loftus
import java.awt.*;
import javax.swing.*;
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame("First GUI"); //takes title for window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel( );
panel.setBackground(Color.red);
panel.setPreferredSize(new Dimension(250, 75));
JLabel label = new JLabel("My first GUI");
panel.add(label); //add label to panel
frame.getContentPane().add(panel); //add panel to content pane of frame
frame.pack(); //size to the preferred size
frame.setVisible(true); //make v isible
}
}
Exercises
1. Experiment with class GUI
1. Change t he color of the panel
2. Change t he size of the panel
3. See what happens if you do not “ pack”
4. See wha t happens if you do not make visible
5. Add a se cond label of a different color
More Components
• Buttons - Component that can be “pushed”
– must invoke addActionListener in order to
enable the program to respond to a button
push
– program waits for an event and the p erforms
the appropriate action
public class GUIWButton {
public GUIWButton() {
JFrame frame = new JFrame("First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.red);
panel.setPreferredSize(new Dimension(250, 75));
JLabel label = new JLabel("Count is 0");
JButton button = new JButton("Push Me!");
button.addActionListener(new ButtonListener(label));
panel.add(label);
panel.add(button);
frame.getContentPane().add(panel);
frame.pack(); //size to the preferred size
frame.setVisible(true);
}
pf2

Partial preview of the text

Download Lecture Slides on Graphical User Interfaces | CS 112 and more Study notes Computer Science in PDF only on Docsity!

Graphical User Interfaces

Containers and Components

• Frame - OS-managed container used to

display GUI-based java apps

• Panel - Container used to organized other

GUI components (buttons, labels, etc)

• Label - Component used to display text

//Similar to Authority.java from Lewis/Loftus import import java.javaxawt.swing.;.; public class GUI { public static void main(String[] args) { JFrame frame = new JFrame("First GUI"); //takes title for window frame. JPanelsetDefaultCloseOperation panel = new JPanel();(JFrame.EXIT_ON_CLOSE); panel. panel.setBackgroundsetPreferredSize(Color.red);(new Dimension(250, 75)); JLabel label = new JLabel("My first GUI"); panel.add(label); frame.getContentPane //add().add(panel); label to panel //add panel to content pane of frame frame.pack(); frame.setVisible //size(true); to the//make preferred visible size } }

Exercises

1. Experiment with class GUI

1. Change the color of the panel

2. Change the size of the panel

3. See what happens if you do not “pack”

4. See what happens if you do not make visible

5. Add a second label of a different color

More Components

• Buttons - Component that can be “pushed”

– must invoke addActionListener in order to

enable the program to respond to a button

push

– program waits for an event and the performs

the appropriate action

public class GUIWButton {

public GUIWButton() {

JFrame frame = new JFrame("First GUI");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

panel.setBackground(Color.red);

panel.setPreferredSize(new Dimension(250, 75));

JLabel label = new JLabel("Count is 0");

JButton button = new JButton("Push Me!");

button.addActionListener(new ButtonListener(label));

panel.add(label);

panel.add(button);

frame.getContentPane().add(panel);

frame.pack(); //size to the preferred size

frame.setVisible(true);

private class ButtonListener implements ActionListener {

JLabel label;

int count;

public ButtonListener(JLabel label) {

this.label = label;

this.count = 0;

public void actionPerformed(ActionEvent event) {

count++;

label.setText("Count is " + count);

public static void main(String[] args) {

GUIWButton mygui = new GUIWButton();

System.out.println("running...");

Exercises

1. Compile and run GUIWButton.

2. Create a GUI with a label and text field. Allow

the user to enter text in the text field. When

the user presses enter, update the text of the

label to be the text contained in the text field.

3. Add a button to the program you wrote for

exercise 2. When the user presses the button

OR presses enter, update the text of the label

to be the text contained in the text field.

4. Modify your program from exercise 3 to keep

a count of the number of times you have

changed the label.