0% found this document useful (0 votes)
71 views3 pages

Tutorial 9

This document discusses creating graphical user interfaces in Java. It provides code examples for creating checkboxes, radio buttons, combo boxes, and lists. It also explains how checkbox and combo box events are triggered. Steps are outlined for creating multiple windows in an application, including defining main and subwindows and implementing an action listener. Code examples are given for creating a JMenuBar and adding menus and menu items.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views3 pages

Tutorial 9

This document discusses creating graphical user interfaces in Java. It provides code examples for creating checkboxes, radio buttons, combo boxes, and lists. It also explains how checkbox and combo box events are triggered. Steps are outlined for creating multiple windows in an application, including defining main and subwindows and implementing an action listener. Code examples are given for creating a JMenuBar and adding menus and menu items.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

AMIT3094 GUI and Web Application Programming

Tutorial 9
1. Create a simple graphical user interfaces with checkboxes, radio buttons, combo boxes,
and lists as shown in Figure 1.

Figure 1
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.*;

public class Q1 extends JFrame{


private JCheckBox jCheckBox1 = new JCheckBox("Bold");
private JCheckBox jCheckBox2 = new JCheckBox("Italic");
private JRadioButton jrbRed = new JRadioButton("Red");
private JRadioButton jrbGreen = new JRadioButton("Green");
private JRadioButton jrbBlue = new JRadioButton("Blue");
private JComboBox jcboCanada = new JComboBox(new
Object[]{"Canada", "China", "Denmark", "Malaysia"});
private JList jlCountry = new JList(new String[]{"Canada",
"China", "Denmark", "Malaysia"});
private JPanel p1 = new JPanel();
private JPanel radioButtonList = new JPanel(new
GridLayout(3,1,2,1));
private JPanel checkBoxList = new JPanel(new
GridLayout(2,1,4,1));

public Q1(){
radioButtonList.add(jrbRed);
radioButtonList.add(jrbGreen);
radioButtonList.add(jrbBlue);
checkBoxList.add(jCheckBox1);
checkBoxList.add(jCheckBox2);

p1.setLayout(new BorderLayout());
p1.add(jcboCanada, BorderLayout.NORTH);
p1.add(radioButtonList, BorderLayout.WEST);
p1.add(checkBoxList, BorderLayout.EAST);

setLayout(new BorderLayout());
add(p1, BorderLayout.NORTH);
add(jlCountry, BorderLayout.CENTER);
setTitle("GUI Component II Demo");
setSize(400,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Q1();
}
}

2. Explain how the following types of events are triggered:

a) Checkbox Events

When a JCheckBox is click for checked and unchecked action, it fires an


ItemEvent and then an ActionEvent

b) JComboBox Events

When an item is selected ,an ActionEvent ids fired and no ItemEvent is fired if the
current item is reselected.

3. Specify the steps to Create multiple windows in an application.

1. Create the main window

- Define a main class for the frame. There must be a button that will invoke the subwindow and add
it to the frame.

2. Create the subwindow

- Define a subclass of JPanel.

3. Implement the actionPerformed handler in the main window.

- Create an instance of the subwindow.

- Add the subwindow object to the main window and make it visible.

4. Write code to create and add a JMenuBar to a frame.


JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
setJMenuBar(menuBar);

5. Write code creates two menus, File and Help, and adds them to the JMenuBar mb.

JMenuBar mb = new JMenuBar();


JMenu file = new JMenu("File");
JMenu help = new JMenu("Help");
mb.add(file);
mb.add(help);
setJMenuBar(mb);

6. Write code adds menu items and item separators in menu fileMenu.

fileMenu.add(new JMenuItem(“new”));
fileMenu.add(new JMenuItem(“open”));
fileMenuSeperator();
fileMenu.add(new JMenuItem(“print”));
fileMenu.add(new JMenuItem(“exit”));
fileMenuSeperator();

You might also like