1. Trang chủ
  2. » Ôn tập Sinh học

LT Java CSDL - Bai 3 - Components - Part 1

36 11 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 36
Dung lượng 842,73 KB

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

Nội dung

JRadioButton( Icon icon, boolean selected) Creates a radio button with the specified image and selection state, but no text. JRadioButton( String text) Creates an unselected radio but[r]

Trang 4

GUI Components

// Create a button with text OK

JButton btOK = new JButton( "OK" );

// Create a label with text "Enter your name: "

JLabel lbName = new JLabel( "Enter your name: " );

// Create a text field with text "Type Name Here"

JTextField txtName = new JTextField( "Type Name Here" );

// Create a check box with text bold

JCheckBox chkBold = new JCheckBox( "Bold" );

// Create a radio button with text red

JRadioButton rbRed = new JRadioButton( "Red" );

// Create a combo box with choices red, green, blue

JComboBox cboColor = new JComboBox( new String[]{ "Red" ,

"Green" , "Blue" }); 4

Trang 5

Working with Components

 javax.swing.Jcomponent

 Several methods:

setEnabled(boolean): receive user input (an argument of true) or is inactive and cannot receive input (false)

Components are enabled by default

isEnabled() returns a boolean value

setVisible(boolean): for all components Use true to display

a component and false to hide it

isVisible()

setSize(int, int): width and height specified as arguments

setSize(Dimension) uses a Dimension

getSize(): returns a Dimension object with height and width

5

Trang 6

Working with Components

Trang 7

JLabel

 Display Text, not editable

Constructor:

Label() : An empty label

JLabel(String) : A label with the specified text

JLabel(String, int) : A label with the specified text and alignment LEFT, CENTER, and RIGHT

JLabel(String, Icon, int) : A label with the specified text, icon, and Alignment

7

Trang 9

JLabel

Example:

JLabel lbl=new JLabel("Họ và tên:");

JLabel lbl=new JLabel("Ngày sinh:");

9

Trang 10

JLabel

 The use of HTML is supported by most Swing components

 Example: use HTML markup to create multiline and multifont labels:

JLabel lbHoten = new JLabel("<html>Dòng 1<p style=\"color:red;font-size:20\">Dòng

2</p></html>");

10

Trang 11

JTextField

 Display data, Input data

Constructors

JTextField(): An empty text field

JTextField( int ): A text field with the specified width

JTextField(String): A text field with text

JTextField(String, int ): A text field with the specified text and width

Trang 12

JTextField

Example

setLayout(new FlowLayout());

JLabel lbHoten = new JLabel("Nhập họ và tên"); add(lbHoten);

JTextField txtHoten = new JTextField(20);

add(txtHoten);

Trang 13

JTextField

 Don’t allow input data

txtHoten.setEditable( false )

 To set Text in code behind for JTextField

txtHoten.setText( "Hello world" );

 To get Text from JTextField

String s = txtHoten.getText();

 We could convert data

int n = Integer.parseInt(s); //s is String

 To set focus:

txtHoten.requestFocus();

Trang 14

JPasswordField

 Hide the characters a user is typing into the field

 JPasswordField class, a subclass of JTextField

 JPasswordField constructor methods take the same arguments as those of its parent class

Methods:

JPasswordField( String text, int columns)

char [] getPassword(): returns the text contained in this password field

Trang 16

JTextArea

 Input is more than one line long

Constructors

JTextArea(): Create a default text area

JTextArea( int rows, int columns): Create a text area

with the specified number of rows and columns

JTextArea(String text)

JTextArea(String text, int rows, int columns)

JTextArea(Document doc): Create a text area that uses the

specified Document

JTextArea(Document doc, String text, int rows, int columns)

Trang 17

JTextArea

Common methods

void append(String str)

Append the given text to the end of the document

void insert(String str, int pos)

Insert the specified text at the given position To insert text

at the beginning of the document, use a position of 0

void replaceRange(String str, int start, int end)

Replace a section of the document

Trang 18

offset of the first character of the next line

public int getLineOfOffset(int offset) throws BadLocationException

Return the line number that contains the given character offset (from the beginning of the document)

Trang 19

JTextArea

JPanel contentPane = new JPanel();

JLabel lblNewLabel = new JLabel( "Nhập dữ liệu:" );

Trang 20

JTextArea

class MyJTextArea extends JFrame {

private JPanel contentPane ;

public MyJTextArea() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 257, 128);

contentPane = new JPanel();

contentPane setBorder( new EmptyBorder(5, 5, 5, 5));

setContentPane( contentPane );

contentPane setLayout( new FlowLayout(FlowLayout.CENTER, 5, 5));

JLabel lblNewLabel = new JLabel( "Nhập dữ liệu:" );

contentPane add(lblNewLabel);

JScrollPane scrollPane = new JScrollPane();

JTextArea textArea = new JTextArea(3,15);

Trang 21

JButton(Icon icon)

Creates a button with an icon

JButton(String text)

Creates a button with text

JButton(String text, Icon icon)

Creates a button with initial text and an icon

Trang 22

Gets the value of the defaultCapable property

protected String paramString()

Returns a string representation of this JButton

Trang 23

JButton

void removeNotify()

Overrides JComponent.removeNotify to check if this button is currently set as the default button on the RootPane, and if so, sets the RootPane's default button to null to ensure the

RootPane doesn't hold onto an invalid button reference

void setDefaultCapable(boolean defaultCapable)

Sets the defaultCapable property, which determines whether this button can be made the default button for its root pane

void updateUI()

Resets the UI property to a value from the current look and feel

Trang 24

JButton

24

It is very important, attach event to do something that you want

JButton bt=new JButton( "Watch" );

bt.setIcon(new ImageIcon( "mywatch.png" ));

bt.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

//do something here coding here }

}); Add event for this button

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

Trang 25

JButton

Trang 26

JRadioButton(Icon icon) Creates an initially unselected radio

button with the specified image but no text

JRadioButton(Icon icon, boolean selected) Creates a radio button with the specified image and selection state, but no text

JRadioButton(String text) Creates an unselected radio button

with the specified text

JRadioButton(String text, boolean selected)

JRadioButton(String text, Icon icon) Creates a radio button that has the specified text and image, and that is initially unselected

JRadioButton(String text, Icon icon, boolean selected)

Trang 27

JRadioButton - Methods

Modifier and Type Method and Description

AccessibleContext getAccessibleContext()Gets the

AccessibleContext associated with this JRadioButton

String getUIClassID()Returns the name of the L&F

class that renders this component

protected String paramString()Returns a string

representation of this JRadioButton

void updateUI()Resets the UI property to a value

from the current look and feel

Trang 28

JRadioButton

28

Make single choice

Must add JRadioButton into the ButtonGroup

if(radio.isSelected())

{

}

Trang 29

JRadioButton

29

ButtonGroup & JRadioButton

JPanel panelGroup = new JPanel();

panelGroup setLayout( new BoxLayout( panelGroup , BoxLayout.Y_AXIS));

TitledBorder titlebor= new TitledBorder( "Ý kiến của bạn:" );

panelGroup setBorder(titlebor);

JRadioButton rad1= new JRadioButton( "Lập trình Java rất dễ" );

JRadioButton rad2= new JRadioButton( "Design GUI Java rất dễ" );

JRadioButton rad3= new JRadioButton( "Viết web trên Java rất dễ" ); JRadioButton rad4= new JRadioButton( "Tất cả đều khó" );

ButtonGroup group= new ButtonGroup ();

group.add(rad1);group.add(rad2);

group.add(rad3);group.add(rad4);

panelGroup add(rad1); panelGroup add(rad2);

panelGroup add(rad3); panelGroup add(rad4);

add( panelGroup );

Trang 30

JRadioButton & ButtonGroup

30

Create Border with title:

Border bor=BorderFactory.createLineBorder(Color RED );

TitledBorder titlebor= new TitledBorder( bor , "Ý kiến của bạn:" );

panelGroup setBorder(titlebor);

Define a buttongroup to add all radio:

ButtonGroup group= new ButtonGroup();

Trang 31

JCheckBox(String text, boolean selected)

JCheckBox(String text, Icon icon)

JCheckBox(String text, Icon icon, boolean selected)

Trang 32

Checkbox

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:

pnCheck.add(chk1);

Add pnCheck into the Window:

add(pnCheck);

BorderFactory.createEtchedBorder(Color BLUE , Color RED );

Create border with 2 color: Blue, Red:

if (chk1.isSelected())

{

//do something

}

Trang 33

Checkbox

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

setBounds(100, 100, 200, 150);

panelCheck = new JPanel();

panelCheck setLayout( new GridLayout(2, 2));

Border bor2=BorderFactory.createEtchedBorder(Color BLUE , Color RED );

TitledBorder titlebor2= new TitledBorder(bor2, "Môn học yêu thích:" );

panelCheck setBorder(titlebor2);

JCheckBox chk1= new JCheckBox( "C/C++" );

JCheckBox chk2= new JCheckBox( "C#" );

JCheckBox chk3= new JCheckBox( "PHP" );

JCheckBox chk4= new JCheckBox( "Java" );

panelCheck add(chk1); panelCheck add(chk2);

panelCheck add(chk3); panelCheck add(chk4);

add( panelCheck );

Trang 34

JComboBox

JComboBox() Creates a JComboBox with a default data model

JComboBox(ComboBoxModel<E> aModel) Creates a JComboBox

that takes its items from an existing ComboBoxModel

JComboBox(E[] items) Creates a JComboBox that contains the

elements in the specified array

JComboBox(Vector<E> items) Creates a JComboBox that contains

the elements in the specified Vector

Ngày đăng: 21/01/2021, 16:16

TỪ KHÓA LIÊN QUAN

w