COMPSCI4039: Programming
Introduction to Swing 1
How the second part of this course works:
Copyright By PowCoder代写 加微信 powcoder
1. Introduction to Graphical User Interfaces (GUIs) and Java Swing
2. To explain some of the concepts for Swing (and our running example) we need to detour:
– Interfaces – Generics – ArrayLists
3. Back to Swing
4. Recursion
5. Testing
How GUI programs work
Up to now all our Java programs have been console-based
○ The programme tells the use what to do next (“Enter an integer …”)
○ The user can only do what the program says.
○ In most cases they do not have a choice of activities.
Graphical User Interfaces (GUIs) are graphical by nature.
The user can interact with several user interface objects (components):
○ The program displays the graphical user interface and waits.
○ The user has several different choices of what do to next.
○ The program reacts to the user activities.
Example GUI (web page)
JTextField
JRadioButton
JComboBox JButton
Working towards a Plotter program …
Program produces a window containing:
● list of x,y coordinates on right
● plotted points on left
click to create new plotted point on left
○ list will update
Add new x,y coords on right ○ points will be plotted on
Java is cross-platform … a point to bear in mind
GUIs look different on Windows and Mac.
Here’s an example that we will see today from these two different platforms …
Generated from Windows Generated on Mac
We will just let Swing decide on the appropriate interface style depending on the machine that you are running it on. (I will show Mac versions on the slides and I will demo on a Window’s machine so you can see the two interfaces.)
Creating a Swing GUI in Java
● The program creates a window object (JFrame), the basis for interaction – There can be more than one window
● The program creates several component objects (JComponents) – These components are given names and other properties
● Each component is added to the window
– It must be positioned in the right place
– Layout Managers are used to facilitate this
● Event Listener objects are added to each component – They process each event when it arrives
Class JFrame
● A JFrame object is an independent window
● A JFrame object looks like a standard ‘window’
– It has a border, a title bar, a ‘close’ box
● Each GUI program must have at least one JFrame object.
– A complicated program will have several windows; each is a different
JFrame object
● Our Plotter example has only one JFrame object
Class JFrame contd.
We must customise the Java JFrame class
– e.g. in a simplified version of our Plotter example:
Remember inheritance from week 3?
Bins and recycle bins! Also BankAccount, etc.
public class JustShowFrame extends JFrame{ … }
The Java keyword extends means that our new class will build on the older class, extending it with new methods, i.e. as we add our own application- specific code
Extending an existing class means that we can use all of the methods that the existing class has
Creating a frame with title, size, position
import javax.swing.*;
public class JustShowFrame extends JFrame
{ public JustShowFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300,300);
setLocation(100,100);
setTitle(“Just a JFrame”);
public static void main(String [] args) {JustShowFrame frame = new JustShowFrame();
frame.setVisible(true);
JFrame methods
● setTitle(String)
● setSize(int dimx, int dimy)
○ number of pixels in the x and y directions. ● setLocation(int x, int y)
○ distance of top left corner of frame from top left corner of monitor, in pixels (default (0,0)).
● setVisible(boolean)
○ You won’t see the window unless you setVisible(true)
○ You can make it invisible by setVisible(false)
● setDefaultCloseOperation
○ Ensure system exits the application when user closes the JFrame– call the method with the
(integer) parameter EXIT_ON_CLOSE(a field of JFrame)
Creating a frame with title, size, position (See Demo) import javax.swing.*;
public class JustShowFrame extends JFrame
{ public JustShowFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300,300);
setLocation(100,100);
setTitle(“Just a JFrame”);
public static void main(String [] args) {JustShowFrame frame = new JustShowFrame();
frame.setVisible(true);
First stage of our Plotter program (See Demo) import javax.swing.JFrame;
public class Plotter1 extends JFrame{
public Plotter1() {
this.setSize(1000,800);
this.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE); this.setVisible(true);
/* Main method — creates the Plotter object */
public static void main(String[] args) { new Plotter1();
Plotter1 output:
Appears in top left corner of screen (because we didn’t setLocation for this one).
We will create components to go onto the frame
More complex GUIs
● In general we’ll want to add components to our JFrame ○ Buttons, labels, textfields etc.
T extField
ButtonFrame – example to illustrate buttons etc.
public class ButtonFrame extends JFrame {
//add instance variables (buttons, etc.) TO DO
public ButtonFrame() { // constructor
// standard JFRAME information (size etc.)
// omit on later slides
this.setTitle(“JFrame with some components”); this.setSize(150, 350); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// to be continued
// Main method — creates the ButtonFrame object
public static void main(String[] args) { ButtonFrame gui = new ButtonFrame();
gui.setVisible(true);
GUI Components 1: JButton
● A JButton object is a button
○ It has a label, e.g. “Click”
○ It reacts to mouse clicks (see later)
● We declare two buttons in ButtonFrame:
public class ButtonFrame extends JFrame { private JButton button1, button2;
// other instance variables to come
public ButtonFrame() {//constructor
// include the standard JFRAME information (size etc.) button1 = new JButton(“Ok”);
button2 = new JButton(“Quit”);
GUI Components 2: JTextField
● A JTextField object displays a text input box
○ The user can type in a string
○ The user can signify that the input is complete by pressing
● Constructor can specify number of columns in input box and/or initial string
○ There are 3 constructors:
■ JTextField(int ncol);
■ JTextField(String text);
■ JTextField(String text, int ncols);
● Program can access/ set the text in a JTextField object with ○ getText/setText
ButtonFrame so far:
public class ButtonFrame extends JFrame { private JButton button1, button2;
private JTextField textField1;
// other instance variables to come
public ButtonFrame() {//constructor
// include the standard JFRAME information (size etc.)
button1 = new JButton(“Ok”);
button2 = new JButton(“Quit”);
textField1 = new JTextField(“initial Text”, 15);
GUI Components 3: JLabel
● This is very simple – it just displays text with no user input
● Useful next to a JTextField or a JButton
○ E.g. label1 = new JLabel(“Click a button”);
ButtonFrame so far:
public class ButtonFrame extends JFrame { private JButton button1, button2; private JTextField textField1; private JLabel label1;
public ButtonFrame() {//constructor
// include the standard JFRAME information (size etc.)
button1 = new JButton(“Ok”);
button2 = new JButton(“Quit”);
textField1 = new JTextField(“initial Text”, 15);
label1 = new JLabel(“Click a button”);
(See Demo)
(Aside) disabling components
● Components such as buttons can be disabled
○ They are greyed out and do not accept any interaction with the user
○ They can then be enabled again
○ Use button1.setEnabled(false) and button1.setEnabled(true)
○ This allows us to control which input is allowed (and when)
○ Components are enabled when they are created
● A JTextField is normally editable
○ The user can enter information and change existing information
○ Editing can be disabled by calling the method field.setEditable(false);
○ Using the parameter true makes it editable again
Our examples – what do we still need to know?
● ButtonFrame: How do we place the components on the JFrame? ○ Need layout managers and JPanels (coming up)
● Plotter:
○ How do we create our own components?
○ How do we make our components respond to events (e.g. button click)?
○ How do we create graphics (e.g. for plotting the red points on left hand side)?
○ How do we store the list of data points?
Time for some questions…
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com