Swing GUI Components Liang Introduction to Java Programming

  • Slides: 25
Download presentation
Swing GUI Components Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education,

Swing GUI Components Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 1

Component Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All

Component Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 2

Component F Component ����������� (Container) ������ Button, Label, List, Text. Area ��������� Lightweight F

Component F Component ����������� (Container) ������ Button, Label, List, Text. Area ��������� Lightweight F Container ����������� Component ���� JPanel, JFrame F ����� Method add(Component com); get. Content. Pane(); //������ //Create Obj. ������� 3 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6

Enabled Component F �������� component ���������� F ������� Obj. set. Enabled(boolean n); F ������

Enabled Component F �������� component ���������� F ������� Obj. set. Enabled(boolean n); F ������ true ������ component ������ Enable F ������ false ��� component ����� Disable Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 4

Button F ���������� JButton b = new JButton(“OK”, ”Cancel”); F �� constructor JButton() ������

Button F ���������� JButton b = new JButton(“OK”, ”Cancel”); F �� constructor JButton() ������ //���� JButton(String text) //�������� JButton(Icon icon) //�������� JButton(String text, Icon icon) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 5

Button F Method ������ set. Text(String text) // ����������� get. Text() // ����������� l

Button F Method ������ set. Text(String text) // ����������� get. Text() // ����������� l F ������� Obj. Button. Name. set. Text(“Text Name”); String n = Obj. Button. Name. get. Text(); F ���������� Create. Button 1. java Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 6

Tool Tip F ����������������� F ������� Tool Tip ������ Obj. Name. set. Tool. Tip.

Tool Tip F ����������������� F ������� Tool Tip ������ Obj. Name. set. Tool. Tip. Text(String text) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 7

JButton inherits Abstract. Button and provides several constructors to create buttons. Liang, Introduction to

JButton inherits Abstract. Button and provides several constructors to create buttons. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 8

JButton Constructors The following are JButton constructors: JButton() JButton(String text, Icon icon) JButton(Icon icon)

JButton Constructors The following are JButton constructors: JButton() JButton(String text, Icon icon) JButton(Icon icon) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 9

JButton Properties F text F icon F mnemonic F horizontal. Alignment F vertical. Alignment

JButton Properties F text F icon F mnemonic F horizontal. Alignment F vertical. Alignment F horizontal. Text. Position F vertical. Text. Position F icon. Text. Gap Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 10

Event Hangling Component Event JButton Interface Class ���� Action. Listener Override action. Performed(Action. Event

Event Hangling Component Event JButton Interface Class ���� Action. Listener Override action. Performed(Action. Event e) Fมเมธอด e. get. Source() ใชในการคนคาวาปมใดถกกด Fมเมธอด e. get. Action. Command(). equals("Copy") ใชในการคนคาวาปมใดถกกด Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 11

Label F ���������� icon ����� (������� ( F ���� Label ������� JLabel label =

Label F ���������� icon ����� (������� ( F ���� Label ������� JLabel label = new JLabel(); F �� constructor ������ JLabel() JLabel(Icon image, int horizontal. Alignment) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 12

Label F constructor ������ JLabel(String text) JLabel(String text, Icon icon, int horizontal) JLabel(String text,

Label F constructor ������ JLabel(String text) JLabel(String text, Icon icon, int horizontal) JLabel(String text, int horizontal) F �� method ������ Obj. set. Text(String text) String n = Obj. get. Text() Obj. set. Tool. Tip. Text(“String text”) Obj. set. Icon(icon) F ����������� Create. Label 1. java Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 13

JLabel A label is a display area for a short text, an image, or

JLabel A label is a display area for a short text, an image, or both. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 14

JLabel Constructors The constructors for labels are as follows: JLabel() JLabel(String text, int horizontal.

JLabel Constructors The constructors for labels are as follows: JLabel() JLabel(String text, int horizontal. Alignment) JLabel(String text) JLabel(Icon icon, int horizontal. Alignment) JLabel(String text, Icon icon, int horizontal. Alignment) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 15

Text. Field F ��������������������� F ���� Text. Field ������� JText. Field t = new

Text. Field F ��������������������� F ���� Text. Field ������� JText. Field t = new JText. Field(); F �� constructor JText. Field() JText. Field(int columns) JText. Field(String text, int columns) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All F ����������� Create. Text. Filed 1. java 16 rights reserved. 0 -13 -222158 -6

JText. Field A text field is an input area where the user can type

JText. Field A text field is an input area where the user can type in characters. Text fields are useful in that they enable the user to enter in variable data (such as a name or a description). Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 17

JText. Field Constructors F JText. Field(int columns) Creates an empty text field with the

JText. Field Constructors F JText. Field(int columns) Creates an empty text field with the specified number of columns. F JText. Field(String text) Creates a text field initialized with the specified text. F JText. Field(String text, int columns) Creates a text field initialized with the specified text and the column size. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 18

JText. Field Properties F text F horizontal. Alignment F editable F columns Liang, Introduction

JText. Field Properties F text F horizontal. Alignment F editable F columns Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 19

JText. Field Methods F get. Text() Returns the string from the text field. F

JText. Field Methods F get. Text() Returns the string from the text field. F set. Text(String text) Puts the given string in the text field. F set. Editable(boolean editable) Enables or disables the text field to be edited. By default, editable is true. F set. Columns(int) Sets the number of columns in this text field. The length of the text field is changeable. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 20

JPassword. Field ���� Jpassword. Field ������ JPassword. Field t = new JPassword. Field(); F

JPassword. Field ���� Jpassword. Field ������ JPassword. Field t = new JPassword. Field(); F �� constructor JPassword. Field() JPassword. Field(int columns) JPassword. Field(String text, int columns) F Method �������� set. Editable(boolean b) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 21

Text. Area F ����������� 1 ����������� F ���� Text. Field ������� JText. Area t

Text. Area F ����������� 1 ����������� F ���� Text. Field ������� JText. Area t = new JText. Area(); F �� constructor JText. Area() JText. Area(int row, int columns) JText. Field(String text, int row, int columns) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 22

Event Hangling Component���� E Interface Class vent JText. Field JPassword. Field Caret. Listener Override

Event Hangling Component���� E Interface Class vent JText. Field JPassword. Field Caret. Listener Override caret. Update(Care. Event e) JText. Area get. Source() iใชในการคนคาวาปมใดถกกด get. Text() iสำหรบดงขอความจาก set. Text() iสำหรบกำหนดขอความ get. Dot() iจะคนคาตำแหนงสดทายของขอความทเลอก get. Mark() iจะคนคาตำแหนงเรมตนของขอความทเลอก Text นนๆ Care. Event 1. java Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 23

JText. Area If you want to let the user enter multiple lines of text,

JText. Area If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JText. Area, which enables the user to enter multiple lines of text. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 24

แบบฝกหด Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All

แบบฝกหด Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 25