comp 303 swing tutorial (the gui in...

22
COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University January 22, 2004 More info can be found on the java.sun website: http://java.sun.com/docs/books/tutorial/uiswing/index.html

Upload: vukiet

Post on 10-Apr-2018

243 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

COMP 303SWING Tutorial

(For a GUI in Java)

Sokhom PhengMcGill UniversityJanuary 22, 2004

More info can be found on the java.sun website:http://java.sun.com/docs/books/tutorial/uiswing/index.html

Page 2: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

Important Things to Know

• AWT vs SWING– Both are GUI tools– A lot of the SWING components are from AWT– How to differentiate them?

• All SWING components starts with ‘J’Eg. JButton, JLabel, etc.

– It’s preferable to use SWING whenever possible

• NEVER ever mix them together– Eg. JButton with Button, etc.

Page 3: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

JFrame vs JPanel

• Both are windows of an application.• JFrame have taskbar and window border

– Can only have one frame in a window• JPanel have invisible window border

– Can have many of them in a frame– They can be part of another panel

• Your application MUST have a frame

Page 4: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

JFrame

JPanels

Page 5: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

How to Create a JFrameimport javax.swing.*;

public class MyFrame extends JFrame {

public MyFrame() {

super("A frame"); // frame title

Jlabel label = new Jlabel(“Hello World!”);

getContentPane().add(label);

setSize(200,200);

setVisible(true);

}

public static void main(String[] args) {

MyFrame myFrame = new MyFrame();

}

}

Page 6: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

How to Create a JPanel

class MyPanel extends JPanel {

public MyPanel() {

JLabel label = new JLabel("Hello World!");

add(label);

}

}

Page 7: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

Add JPanel to JFrameimport javax.swing.*;

public class MyFrame extends JFrame {

public MyFrame() {

super("A frame"); // frame title

JPanel panel = new MyPanel();

getContentPane().add(panel);

setSize(200,200);

setVisible(true);

}

public static void main(String[] args) {

MyFrame myFrame = new MyFrame();

}

}

Page 8: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

Some Useful Components

• Basic Components– JButton, JLabel, JTextArea, JTextField, JTable

• Other Panels– JSplitPane, JScrollPane, JTabbedPane

Page 9: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

Layout Manager (AWT)

• BorderLayout (default in JFrame)• BoxLayout• CardLayout• FlowLayout (default in JPanel)• GridBagLayout• GridLayout

Page 10: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

BorderLayoutNORTH

SOUTH

EASTWEST CENTER

JLabel label = new JLabel(“Hello World!”);setLayout(new BorderLayout());add(label, BorderLayout.NORTH);

In FramegetContentPane.setLayout(new BorderLayout());getContentPane.add(label, BorderLayout.NORTH);

Page 11: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

BoxLayout

setLayout(new BoxLayout());

In FramegetContentPane().setLayout(

new BoxLayout());- Can be aligned along

an axis

Page 12: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

CardLayout

• Area that displays different components at different time– Eg. Tabbed windows

• Depending on the choice, the appropriate panel will be displayed

setLayout(new CardLayout());

In FramegetContentPane().setLayout(new CardLayout());

Page 13: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

FlowLayout

setLayout(new FlowLayout());

In FramegetContentPane().setLayout(new FlowLayout());

Page 14: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

GridBagLayout

setLayout(new GridBagLayout());

In FramegetContentPane().setLayout(new GridBagLayout());

Page 15: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

GridLayout

setLayout(new GridLayout());

In FramegetContentPane().setLayout(new GridLayout());

Page 16: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

Event Handling

• This is where the code to react to an action is done• Listener type

– ActionListener• Button presses, choosing menu item, etc.

– WindowListener• Closing, minimizing, resizing a window, etc.

– MouseListener• Pressing a mouse button

Page 17: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

How to Implementan Event Handler (1)

public class MyClass implements ActionListener {

...

//where initialization occurs:

button.addActionListener(this);

...

public void actionPerformed(ActionEvent e) {

...//Do something...

}

}

Page 18: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

Impl. Event Handler (2)public class MyClass {

...//where initialization occurs:button1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {...//Do something...

}});

button2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {

...//Do something...}

});}

Page 19: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

Impl. Event Handler (3)public class MyClass implements ActionListener {

...//where initialization occurs:button1.addActionListener(this);button2.addActionListener(new MyListener());...public void actionPerformed(ActionEvent e) {

...//Do something...}

}

class MyListener implements ActionListener {...public void actionPerformed(ActionEvent e) {

...//Do something...}

}

Page 20: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

Painting

• Override the paintComponent method of JPanel

• Use repaint() to refresh image

public void paintComponent(Graphics g) {

// use super method to paint background

super.paintComponent(g);

// now paint on top of what has already been painted...

}

Page 21: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

Animation

• Set a timer that calls repaint() in a fixed time interval

Timer timer;

public MyClass() {...timer = new Timer(10, new ActionListener() {

public void actionPerformed(ActionEvent e) {repaint(); //repaint every 10ms

}});timer.start(); //start the timer

}

Page 22: COMP 303 SWING Tutorial (The GUI in Java)cs.mcgill.ca/~cs303/winter2004/notes/tutorial1/swing_tutorial.pdf · COMP 303 SWING Tutorial (For a GUI in Java) Sokhom Pheng McGill University

Code Example