1. Trang chủ
  2. » Công Nghệ Thông Tin

LESSON 12 GUI Lập trình Java

59 294 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 59
Dung lượng 2,12 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

 FlowLayoutFlowLayout cho phép add các control trên cùng một dòng, khi nào hết chỗ chứa nó sẽ tự động xuống dòng, ta cũng có thể điều chỉnh hướng xuất hiện của control..  FlowLayout JP

Trang 1

1

Trang 2

Full Name: Trần Duy Thanh

Blog : http ://duythanhcse.wordpress.com

Email: tranduythanh@dntu.edu.vn

Phone: 0987773061

Contact

Trang 4

1 How to display a Window?

 Extends from Frame or JFrame?

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

Trang 5

1 How to display a Window?

Trang 6

1 How to display a Window?

Use to set title for this window

setDefaultCloseOperation(EXIT_ON_CLOSE )

;

Allow click ‘x’ Top right corner

to close the window

Trang 8

 FlowLayout

FlowLayout cho phép add các control trên cùng một dòng, khi nào hết chỗ chứa nó sẽ tự động xuống dòng, ta cũng có thể điều chỉnh hướng xuất hiện của control Mặc định khi một JPanel được khởi tạo thì bản thân lớp chứa này sẽ có kiểu Layout là FlowLayout

Resize the Width

Trang 9

 FlowLayout

JPanel pnFlow=new JPanel();

pnFlow.setLayout(new FlowLayout());

pnFlow.setBackground(Color.PINK );

JButton btn1=new JButton( "FlowLayout" );

JButton btn2=new JButton( "Add các

control" );

JButton btn3=new JButton( "Trên 1 dòng" );

JButton btn4=new JButton( "Hê: t chỗ<

Trang 10

 FlowLayout

pnFlow.setLayout(new FlowLayout());

Setup FlowLayout for pnFlow

Trang 11

 BoxLayout

BoxLayout cho phép add các control theo dòng hoặc cột, tại mỗi vị trí add nó chỉ chấp nhận 1 control, do đó muốn xuất hiện nhiều control tại một vị trí thì bạn nên add vị trí

đó là 1 JPanel rồi sau đó add các control khác vào JPanel

này

từ trái qua phải

từ trên xuống dưới

BoxLayout sẽ không tự động xuống dòng khi hết chỗ

chứa, tức là các control sẽ bị che khuất nếu như thiếu

không gian chứa nó

Trang 12

 BoxLayout

No wrap row when resize dimension

Trang 13

 BoxLayout

JPanel pnBox=new JPanel();

pnBox.setLayout(new BoxLayout(pnBox,

Trang 14

Setup TextColor for button btn1

Font font=new Font("Arial",Font.BOLD |

Trang 15

 BorderLayout

BorderLayout giúp chúng ta hiển thị các control theo 5

vùng: North, South, West, East, Center

Nếu như không có 4 vùng : North, West, South, East Thì vùng Center sẽ tràn đầy cửa sổ, thông thường khi đưa các control JTable, JTree, ListView, JScrollpane… ta thường đưa vào vùng Center để nó có thể tự co giãn theo kích thước cửa sổ giúp giao diện đẹp hơn

Trang 16

hocui\src\MyBorderLayou

Trang 17

 BorderLayout

pnBorder.setLayout(new BorderLayout());

Setup BorderLayout for pnBorder

Trang 19

 CardLayout

final JPanel pnCenter= new JPanel();

pnCenter.setLayout(new CardLayout());

final JPanel pnCard1= new JPanel();

hocui\src\MyCardLayout.

Trang 20

 CardLayout

pnCenter.setLayout(new CardLayout());

Setup CardLayout for pnCenter

Show component with mycard1 name,

that we define from above

Trang 21

 CardLayout

Ngoài ra ta có thể dùng JTabbedPane để thay thế cho

CardLayout, JTabbedPane có giao diện đẹp mắt và thân thiện với người sử dụng:

Theo kinh nghiệm thì các bạn chỉ cần hiểu 4 loại Layout

trên là có thể thiết kế được giao diện đẹp mắt

Trang 22

pnTab1.add(new JButton( "Tabbed 1" ));

JPanel pnTab2=new JPanel();

Trang 25

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent

arg0) {

//do something here coding here

}

});

Add event for this button:

btn.setMnemonic('W'); Alt+W to call btn command

Trang 26

Display Text, not editable

Could add Icon for JLabel

ImageIcon icon=new

ImageIcon( "watch2.png" );

lbl.setIcon(icon);

Trang 27

JLabel lblTen=new JLabel( "Nhập tên:" );

JTextField txtTen=new JTextField(15);

add(lblTen);add(txtTen);

txtTen.setEditable( false);

If you don’t allow input data, please call setEditable

method and take false value

Trang 28

3 Common Control

 JTextField

To set Text in code behind for JTextField:

txtTen.setText( "Hello Tèo" );

To get Text from JTextField:

Trang 29

3 Common Control

29

 JTextArea Input data multi line

JLabel lblDes= new JLabel("Mỗ

We should use JScrollPane to create

Scroll for JTextArea when user

input data over limit row or column

Trang 30

3 Common Control

 ButtonGroup & JRadioButton

Make single choice

Must add JRadioButton into the ButtonGroup

if(rad1.isSelected()){

}

Trang 31

 ButtonGroup & JRadioButton

JPanel pnGroup=new JPanel();

JRadioButton rad1=new JRadioButton("Rấ: t hài lòng");

JRadioButton rad2=new JRadioButton("Hài lòng");

JRadioButton rad3=new JRadioButton("Tạm chấ: p nhận");

JRadioButton rad4=new JRadioButton("Khỗng chấ: p

Trang 32

 ButtonGroup & JRadioButton

Create Border with title:

Border bor=BorderFactory.createLineBorder(Color RED );

TitledBorder titlebor=new TitledBorder

(bor, "Chọn nè:");

pnGroup.setBorder(titlebor);

Define a buttongroup to add all radio:

ButtonGroup group=new ButtonGroup();

Trang 33

3 Common Control

33

 JCheckBox Make multi choice

JPanel pnCheck=new JPanel();

pnCheck.setLayout(new GridLayout(2, 2));

JCheckBox chk1=new JCheckBox("Java");

JCheckBox chk2=new JCheckBox("F Sharp");

JCheckBox chk3=new JCheckBox("C Sharp");

JCheckBox chk4=new JCheckBox("Ruby");

pnCheck.add(chk1);pnCheck.add(chk2);

pnCheck.add(chk3);pnCheck.add(chk4);

add(pnCheck);

Trang 34

3 Common Control

 JCheckBox Make multi choice

Set grid layout 2 rows and 2 column

pnCheck.setLayout(new GridLayout(2, 2));

Create JCheckBox:

JCheckBox chk1=new JCheckBox( "Java" );

Add chk1 into the pnCheck:

Trang 37

private String Name ;

public Person(String id,String name){

Trang 38

3 Common Control

 JComboBox How to add Array Person to

JComboBox?

Person []list={

new Person( "1" , "TrấI n Thành Cỗng" ),

new Person( "2" , "Nguyê< n Đại Thắ: ng" ),

new Person( "3" , "Hoàng Thành Đạt" )};

JComboBox cbo2=new JComboBox(list);

add(cbo2);

We get Person from selected:

Person

p=(Person)cbo2.getSelectedItem();

Trang 39

3 Common Control

39

 JListPerson []list={

new Person("1", "Đỗ< Cỗng Thành"),

new Person("2", "Nguyê< n Vắn Hùng"),

new Person("3", "TrấI n Duy Thanh"),

new Person("4", "Đoàn Ái Nương"),

Trang 40

Object o=jl.getSelectedValue();

Object arr[]=jl.getSelectedValues();

Trang 42

4 Event Listener

 Inline anonymous listener

JButton btn1=new JButton("Say Hello!");

btn1.addActionListener(new ActionListener()

Trang 43

4 Event Listener

43

 Inline anonymous listener

btn1.addMouseListener(new MouseListener() {

public void mouseReleased(MouseEvent arg0) {

Trang 44

4 Event Listener

 Listener in variable

Hold a reference to the Listener in a variable

 could share event ActionListener btnClick=new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

Trang 45

btn1.addActionListener(new MyClick());

Could use class event anywhere

Trang 48

 JScrollpane

Provides a scrollable view of a lightweight component

A JScrollPane manages a viewport, optional vertical and horizontal scroll bars, and optional row and column

heading viewports

ImageIcon img=new ImageIcon("baby.jpg");

JLabel lblImg=new JLabel(img);

JScrollPane scimg=new JScrollPane(lblImg,

Trang 49

 JTable

The JTable is used to display and edit regular

two-dimensional tables of cells

Trang 50

 JTable

DefaultTableModel dm =new DefaultTableModel();

dm.addColumn( "Mã" );

dm.addColumn( "Tên" );

dm.addColumn( "TuổE i" );

final JTable tbl= new JTable( dm );

dm.addRow(new String[]{"112","Ngỗ vắn Bắ: p","21"});

dm.addRow(new String[]{"113","Nguyê< n Thị

Trang 52

 JTable

How to handle event : Mouse, Key from JTable?

tbl.addKeyListener(new KeyListener() {

Trang 53

2 Ways to add new Row:

dm.getRowCount();Get total row from JTable

Trang 55

 JTree

Handle event

tree.addMouseListener(new MouseListener() {

public void mouseReleased(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {

Trang 56

 JMenuBar

JMenuBar menubar=new JMenuBar();

setJMenuBar(menubar);

JMenu mnuFile=new JMenu("File");

JMenu mnuEdit=new JMenu("Edit");

Trang 58

6 Advanced Control

 JToolBar

JToolBar toolBar=new JToolBar("MyBar");

JButton btn1=new JButton("New");

JCheckBox chk1=new JCheckBox("Checkme");

Trang 59

59

Ngày đăng: 30/05/2016, 00:16

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN