You are on page 1of 24

SWING

Trn Hong t thdat@fetel.hcmus.edu.vn

Nguyn Hng Thng


nhthang@fetel.hcmus.edu.vn
Lp trnh Java

K I, nm hc 2013 - 2014

NI DUNG

Buttons, Events, and Other


Containers and Layout Managers Menus and Buttons

Text Fields and Text Areas


2
Lp trnh Java K I, nm hc 2013 - 2014

BUTTONS, EVENTS, AND OTHER (1)


A Simple Window
JFrame

Pixel.

Constructor 1. new JFrame(); 2. new JFrame("fetel");


3
Lp trnh Java K I, nm hc 2013 - 2014

BUTTONS, EVENTS, AND OTHER (2)


Some Methods in the Class JFrame

1. public void setSize(int width, int height) 2. public void setTitle(String title) 3. public void add(Component componentAdded) 4. public void setLayout(LayoutManager manager) 5. public void setJMenuBar(JMenuBar menubar) 6. public void setVisible(true)
4
Lp trnh Java K I, nm hc 2013 - 2014

BUTTONS, EVENTS, AND OTHER (3)


Buttons

Jbutton

JButton endButton =new JButton("Click to end program.");

add

firstWindow.add(endButton);

5
Lp trnh Java K I, nm hc 2013 - 2014

BUTTONS, EVENTS, AND OTHER (4)


Action Listeners and Action Events
Click ActionEvent Listener class Invoke ActionEvent Method (actionPerformed) ActionListener

EndingListener buttonEar =new EndingListener(); endButton.addActionListener(buttonEar);


6
Lp trnh Java K I, nm hc 2013 - 2014

BUTTONS, EVENTS, AND OTHER (5)


Demo First Window

7
Lp trnh Java K I, nm hc 2013 - 2014

BUTTONS, EVENTS, AND OTHER (6)


Labels

Jlable
JLabel greeting = new JLabel("Hello");

add
firstWindow.add(greeting);

8
Lp trnh Java K I, nm hc 2013 - 2014

BUTTONS, EVENTS, AND OTHER (7)


Color
java.awt package

this.getContentPane().setBackground(Color.GRAY); Color.BLACK Color.BLUE Color.CYAN Color.DARK_GRAY Color.GRAY Color.GREEN Color.LIGHT_GRAY


Lp trnh Java

Color.MAGENTA Color.ORANGE Color.PINK Color.RED Color.WHITE Color.YELLOW


9
K I, nm hc 2013 - 2014

CONTAINERS AND LAYOUT MANAGER (1)


Layout Manager
add(Component componentAdded_1)
add(Component componentAdded_2) add(Component componentAdded_3) JFrame

componentAdded_2 componentAdded_1 componentAdded_3

layout manager
10
Lp trnh Java K I, nm hc 2013 - 2014

CONTAINERS AND LAYOUT MANAGER (2)


Border Layout Managers

setLayout( new BorderLayout( )); JLabel label1 =new JLabel("First label"); add(label1, BorderLayout.NORTH);

11
Lp trnh Java K I, nm hc 2013 - 2014

CONTAINERS AND LAYOUT MANAGER (3)


Flow Layout Managers

setLayout( new FlowLayout( )); JLabel label1 =new JLabel("First label................"); add(label1); JLabel label2 =new JLabel("Second label..............."); add(label2);
Lp trnh Java K I, nm hc 2013 - 2014

12

CONTAINERS AND LAYOUT MANAGER (4)


Grid Layout Managers

S thay i cho ph hp vi s lng Component setLayout( new GridLayout(2, 3)); JButton btn1 =new JButton("First Button................"); add(btn1); JButton btn2 =new JButton("Second Button..............."); add(btn2); JButton btn3 = new JButton("Third Button..............."); add(btn3);
Lp trnh Java K I, nm hc 2013 - 2014

13

CONTAINERS AND LAYOUT MANAGER (5)


Panels
BorderLayout
JPanel GridLayout JPanel JPanel

setLayout( new BorderLayout()); JPanel biggerPanel = new JPanel(); biggerPanel.setLayout(new GridLayout(1, 3));
Lp trnh Java K I, nm hc 2013 - 2014

14

MODEL-VIEW-CONTROLLER PATTERN (1)


Model-View-Controller Model l class cha data v phng thc cn s dng data. View l class hin th giao din. Controller l class phi hp gia Model v View

15
Lp trnh Java K I, nm hc 2013 - 2014

MODEL-VIEW-CONTROLLER PATTERN (2)


Example (1)

16
Lp trnh Java K I, nm hc 2013 - 2014

MODEL-VIEW-CONTROLLER PATTERN (3)


Example (2)

17
Lp trnh Java K I, nm hc 2013 - 2014

MENUS AND BUTTONS (1)


Menus
JMenu colorMenu =new JMenu("Add Colors");

JMenuItem blueChoice =new JMenuItem("Blue"); blueChoice.addActionListener(this); colorMenu.add(blueChoice);

JMenuBar bar =new JMenuBar( ); bar.add(colorMenu); setJMenuBar(bar);


18
Lp trnh Java K I, nm hc 2013 - 2014

MENUS AND BUTTONS (2)


Dimension Class
aButton.setPreferredSize(new Dimension(30, 50));

aButton aButton.setPreferredSize(new Dimension(60, 50));

aButton

19
Lp trnh Java K I, nm hc 2013 - 2014

MENUS AND BUTTONS (3)


setActionCommand Method
grayButton.setActionCommand("ABC"); grayButton.addActionListener( new ActionListenerClass());

View

public void actionPerformed(ActionEvent e) { String buttonString = e.getActionCommand(); if(buttonString.equals(ABC")) do A; } ActionListenerClass


Lp trnh Java K I, nm hc 2013 - 2014

20

MENUS AND BUTTONS (4)


Listeners as Inner Classes
public class PanelDemo extends Jframe { private JPanel bluePanel; private class bueListener implements ActionListener { public void actionPerformed(ActionEvent e)

{
bluePanel.setBackground(Color.BLUE); } }//End of greenListener inner class }
21
Lp trnh Java K I, nm hc 2013 - 2014

TEXT FIELDS AND TEXT AREAS (1)


Text Field
Text Field: enter a single line of text

JTextField name =new JTextField(NUMBER_OF_CHAR);

getText
String inputString = name.getText();

setText
name.setText("");
22

Lp trnh Java

K I, nm hc 2013 - 2014

TEXT FIELDS AND TEXT AREAS (2)


Text Area

Create Object
JTextArea theText =new JTextArea(int minimum_number_lines, int minimum_number_characters_per_line);

setLineWrap
name.setLineWrap(true);

setEditable
name.setEditable(true);

23
Lp trnh Java K I, nm hc 2013 - 2014

BI TP

Thit k phn mm tnh cc php ton trn h s nh phn v thp lc phn 8 bit.
1. 2.

Php AND, OR, XOR, NOT. Php +, -, *, /

19/11 np bi
24
Lp trnh Java K I, nm hc 2013 - 2014

You might also like