Download Today Homework Reading

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture Notes
CPSC 224 (Spring 2012)
Today
• Quiz 6
• Start on Java Swing
Homework
• hw5 out
Reading
• Ch 7: 281-299
• Ch 8: 323-330
S. Bowers
1 of 10
Lecture Notes
CPSC 224 (Spring 2012)
GUI programming in Java
Early versions of Java used the “AWT”
• Abstract Window Toolkit
• an API over underlying graphics support of platform (Win, Mac, Linux, etc.)
“Swing” extends AWT
• richer API
• more consistent “look and feel”
• native “look and feels”
• more widgets
S. Bowers
2 of 10
Lecture Notes
CPSC 224 (Spring 2012)
JPanels
A JPanel is called either a “panel” or “pane”
• a general-purpose container for widgets
• widgets added using panel.add(widget) ... depending on layout manager
• panels are also widgets ... so can be added to panes
• basic operations:
– add(Comonent c)
– getComponent(int i)
– remove(int i)
– set the background color
– set the layout manager (more later)
– lots more!
NOTE: You are expected to look up the classes we discuss in the Java API ...
we can’t cover all the methods/uses in class
S. Bowers
3 of 10
Lecture Notes
CPSC 224 (Spring 2012)
JFrames
A JFrame is a top-level window
import javax.swing.*;
public class Lab {
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
• set/get the size
• set/get the window title
• add widgets to the “content” pane
• add/set the menu bar
• and so on
JFrame anatomy:
• root pane manages the interior of the pane (the other panes)
• layered pane for puting widgets on top of or behind other widgets
• menu bar (optional) is for menu items (File → Save, Edit → Copy, etc.)
• content pane is the visible widgets of the frame, panes added using frame.add(...)
• glass pane is used to intercept events before the content pane
S. Bowers
4 of 10
Lecture Notes
CPSC 224 (Spring 2012)
Example Widgets: JTextArea
Used for getting/displaying multiple lines of text
import javax.swing.*;
public class Lab {
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea(20, 40);
f.add(textArea);
f.pack();
f.setVisible(true);
}
}
Various operations for a JTextArea
• append a string
• set line wrapping
• get text
• get selected text
• set whether editable
• and so on
S. Bowers
5 of 10
Lecture Notes
CPSC 224 (Spring 2012)
Adding Scroll Bars to a Text Area
Here we can use a JScrollPane widget
import javax.swing.*;
public class Lab {
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea(20, 40);
JScrollPane scrollPane = new JScrollPane(textArea);
f.add(scrollPane);
f.pack();
f.setVisible(true);
}
}
Setting the horizontal and vertical scroll policy
• as needed, never, always ... by scroll bar
scrollPane.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL SCROLLBAR ALWAYS);
S. Bowers
6 of 10
Lecture Notes
CPSC 224 (Spring 2012)
Dialog Boxes
Displaying simple errors, warnings, messages, etc. using JOptionPane
JOptionPane.showMessageDialog(...)
– shows a message, waits for user to click OK
JOptionPane.showConfirmDialog(...)
– shows a message and gets confirmation (OK, cancel, etc.)
JOptionPane.showOptionDialog(...)
– shows a message and gets users choice from a set of options
JOptionPane.showInputDialog(...)
– shows a message and gets a line of user input
import javax.swing.*;
public class Lab {
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
JOptionPane.showMessageDialog(f, "Welcome to Swing");
JOptionPane.showMessageDialog(f, "Hi", "Title",
JOptionPane.PLAIN_MESSAGE);
}
}
• Types of messages: ERROR MESSAGE, INFORMATION MESSAGE, WARNING MESSAGE, QUESTION MESSAGE, or PLAIN MESSAGE
S. Bowers
7 of 10
Lecture Notes
CPSC 224 (Spring 2012)
Basic Class Organization
Create JFrame or JPanel subclasses for windows/panes
• main creates the main application frame
• each JFrame or JPanel class becomes a reusable, specialized widget
public class App {
public static void main(String[] args) {
...
JFrame frame = new MyJFrame(...);
...
}
}
public class MyJFrame extends JFrame {
public MyJFrame(...) {
initFrame();
...
}
private void initFrame() { ... }
}
public class MyPane extends JPanel {
public MyPane() {
initPane();
}
private void initPane() { ... }
}
S. Bowers
8 of 10
Lecture Notes
CPSC 224 (Spring 2012)
Buttons
Create buttons using the JButton class
import javax.swing.*;
import java.awt.event.*;
public class Lab {
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new PanicPane());
f.pack();
f.setVisible(true);
}
}
import javax.swing.*;
import java.awt.event.*;
public class PanicPane extends JPanel implements ActionListener {
private JButton button1 = new JButton("Panic");
private JButton button2 = new JButton("Don’t Panic");
public PanicPane() {
initPane();
}
private void initPane() {
button1.addActionListener(this);
button2.addActionListener(this);
add(button1);
S. Bowers
9 of 10
Lecture Notes
CPSC 224 (Spring 2012)
add(button2);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1)
System.out.println("Panic");
else
System.out.println("Don’t Panic");
}
}
Exercise
• Instead of printing ...
• Display a dialog box with a message depending on button clicked
S. Bowers
10 of 10