14
By .Muhammad Shebl GETTING STARTED WITH GUI PROGRAMMING 2

Getting started with GUI programming in Java_2

Embed Size (px)

Citation preview

Page 1: Getting started with GUI programming in Java_2

By .Muhammad Shebl

GETTING STARTED WITH GUI PROGRAMMING 2

Page 2: Getting started with GUI programming in Java_2

OBJECTIVES

1. Labels & Images 2. Buttons 3. Text Fields & Text Area 4. Combo Boxes 5. Check box 6. Set bounds layout 7. Pack() & setResizable() 8. Menus

Page 3: Getting started with GUI programming in Java_2

Labels :

JLabel lab=new JLabel(“MUFIX”);

ImageIcon icon = new ImageIcon("sigin.png");

JLabel lab=new JLabel(icon );

Page 4: Getting started with GUI programming in Java_2

BUTTONS :

Adding Event :

JButton jbt = new JButton("OK");

jbt.addActionListener(this );

Add icon image :

ImageIcon icon = new

ImageIcon("sigin.png");

JButton jbt = new JButton(icon);

Page 5: Getting started with GUI programming in Java_2

BUTTONS :

Roll over icon :

ImageIcon icon = new

ImageIcon("sigin.png");

JButton jbt = new JButton(icon);

Jbt.setRolloverIcon(icon);

Page 6: Getting started with GUI programming in Java_2

BUTTONS:

Roll Pressedicon : ImageIcon icon = new

ImageIcon("sigin.png");

JButton jbt = new JButton(icon);

jbt.setPressedIcon(icon);

Page 7: Getting started with GUI programming in Java_2

TEXT AREA

If you want to let the user enter multiple lines of text, you have to create several instances of JTextField. A better alternative is to use JTextArea, which enables the user to enter multiple lines of text.

JTextArea jtaNote = new JTextArea(5, 20);

Page 8: Getting started with GUI programming in Java_2

TEXT FIELDS KEY LISTNERS :

// Create a text field with text "Type Name Here" JTextField jtfName = new JTextField("Type Name Here");

jtfName .addKeyListener(this);

Page 9: Getting started with GUI programming in Java_2

Array of Text Fields

You can create Array of Text fields by:

JTextField[][] txt = new JTextField[x][y];

for (int i = 0; i < x; i++) {

for (int j = 0; j < y; j++) {

txt[i][j] = new JTextField(10);

panel.add(txt[i][j]);

txt[i][j].setHorizontalAlignment(JTextField.CENTER);

if (i == j) {

txt[i][j].setText("1");

txt[i][j].setEditable(false);

} txt[i][j].addKeyListener(this);

}}

Page 10: Getting started with GUI programming in Java_2

Combo box

To Create Combo box :

JComboBox operations_txt= new JComboBox(new String[]{"<=", "=", ">="});

Operation_txt. getSelectedItem();

Page 11: Getting started with GUI programming in Java_2

Check box :

// Create a check box with text bold JCheckBox jchkBold = new JCheckBox("Bold");

jchkBold.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { if (jchkBold.isSelected()) { // your Action

}

} });

Page 12: Getting started with GUI programming in Java_2

QUESTIONS ?!!

Page 13: Getting started with GUI programming in Java_2

TASK : FULL CALCULATOR

Page 14: Getting started with GUI programming in Java_2

Thank You