0% found this document useful (0 votes)
40 views

User Interface Part 1

1. A frame may contain a menu bar with dropdown options or menu items. 2. When an option is clicked, a dropdown menu appears containing other menu items. 3. The sample code creates a menu bar with one menu titled "File" containing an "Exit" menu item.

Uploaded by

Lay Galicia
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

User Interface Part 1

1. A frame may contain a menu bar with dropdown options or menu items. 2. When an option is clicked, a dropdown menu appears containing other menu items. 3. The sample code creates a menu bar with one menu titled "File" containing an "Exit" menu item.

Uploaded by

Lay Galicia
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 14

USER INTERFACE PART 2

Menu, MenuBar and MenuItem


1*A frame may contain a menu bar with options (i.e. items)
2*When the mouse is clicked on an option a drop down menu appears
3*Each menu consists of one or more menu items

//SAMPLE7
import java.awt.*;
import java.awt.event.*;

class MNB extends Frame {

MNB()
{
super("Example: MenuBar");

final MenuBar mb = new MenuBar();

setMenuBar(mb);

final Menu m = new Menu("File");


MenuItem mi;

mi = new MenuItem("Exit");
mi.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae)


{
System.exit(0);
}
});
m.add(mi);
mb.add(m);
setSize(250,100);
}
public static void main(String[] args)
{
new MNB().setVisible(true);
}

SAMPLE7 OUTPUT:MENU
SAMPLE 8: Creating New Window Frame
// Dialog Box
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CreatNewFrame extends JFrame


{
JLabel client_title;
JButton create_button;

public CreatNewFrame() {

getContentPane().setLayout(new GridLayout(1,0));

create_button = new JButton("Create");


create_button.addActionListener(new ButtonListener());
getContentPane().add(create_button);
}

class ButtonListener implements ActionListener {


public void actionPerformed(ActionEvent e) {
NewFrame nf = new NewFrame();

nf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});

nf.setTitle("New Window Frame");


nf.setSize(200,150);
nf.setVisible(true);
}
}

public static void main (String args[]) {


CreatNewFrame f = new CreatNewFrame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});

f.setTitle("Create New Frame");


f.setSize(200,150);
f.setVisible(true);
}
} // end of CreatNewFrame
class NewFrame extends JFrame {
JLabel label;

public NewFrame() {
getContentPane().setLayout(new FlowLayout());

label = new JLabel("Another New Frame");


getContentPane().add(label);
} // NewFrame constructor

} // end of NewFrame class

SAMPLE8 OUTPUT:
SAMPLE 9: Dialogs
1*A dialog is a special window to convey a message or provides a special function
2*Every dialog is dependent on a frame – when that frame is destroyed, so are its dependent dialogs
3*A modal dialog blocks user input to all other windows in the program

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class DLG extends JFrame {

DLG()
{
super("Example: Swing Dialog");

final JFrame jf = this;


final JButton jb = new JButton("Show a message dialog!");

jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {


JOptionPane.showMessageDialog(jf,"This is a simple message dialog");
}
});
add(jb);
setSize(250,100);
}

public static void main(String[] args)


{
new DLG().setVisible(true);
}

SAMPLE 9 OUTPUT:
SAMPLE 10: The JPanel Class

EXERCISE:
Modify the User Class such that the details (name, gender, subject) will be displayed in a TextArea in
another frame when the Submit button is clicked.
The JPanel class is a simple container class. It provides space in which you can add other components
including other panels.

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class PanelSample{

private JPanel panel;


private JFrame frame;
private JLabel labelOne;
private JLabel labelTwo;

public PanelSample(){
initComponents();
}

public void initComponents(){


frame = new JFrame("My Frame");
labelOne = new JLabel("First Label");
labelTwo = new JLabel("Second Label");
panel = new JPanel();
panel.add(labelOne);
panel.add(labelTwo);

frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(100,150);
}

public static void main(String[] args){


PanelSample panelsample = new PanelSample();
}
}
SAMPLE 10 OUTPUT:
SAMPLE 11: The JTextField Class
The JTextField class is used to create a textfield control.
The getText() method is used to retrieve the text entered into a textfield control.
The setText() method is used to display text in a textfield control.

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class TextFieldSample{

private JPanel panel;


private JFrame frame;
private JLabel labelName;
private JLabel labelAddress;
private JTextField txtName;
private JTextField txtAddress;

public TextFieldSample(){
initComponents();
}

public void initComponents(){


frame = new JFrame("Employee Frame");
labelName = new JLabel("Employee Name:");
labelAddress = new JLabel("Employee Address:");

//20 and 20 specifies the length of the textfield control


txtName = new JTextField(20);
txtAddress = new JTextField(20);

panel = new JPanel();


panel.add(labelName);
panel.add(txtName);
panel.add(labelAddress);
panel.add(txtAddress);

frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(300,200);
}

public static void main(String[] args){


TextFieldSample textfieldsample = new TextFieldSample();
}
}
SAMPLE 11 OUTPUT:
SAMPLE 12: The JList Class
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JList;

public class ListSample{

private JPanel panel;


private JFrame frame;
private JList list;
private String[] country = {"China", "USA", "India", "Philippines"};

public ListSample(){
initComponents();
}

public void initComponents(){


frame = new JFrame("Employee Frame");
panel = new JPanel();
list = new JList(country);

panel.add(list);

frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(300,200);
}

public static void main(String[] args){


ListSample listsample = new ListSample();
}
}

SAMPLE 12 OUTPUT:
SAMPLE 13: The JComboBox Class
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;

public class ComboSample{

private JPanel panel;


private JFrame frame;
private JComboBox combo;
private String[] country = {"China", "USA", "India", "Philippines"};

public ComboSample(){
initComponents();
}

public void initComponents(){


frame = new JFrame("Employee Frame");
panel = new JPanel();
combo = new JComboBox(country);

panel.add(combo);

frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(500,500);
}

public static void main(String[] args){


ComboSample combosample = new ComboSample();
}
}
SAMPLE 13 OUTPUT:
SAMPLE 14: The JTextArea Class

Similar to JTextField except that JTextArea can have multiple lines.

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class TextAreaSample{

private JPanel panel;


private JFrame frame;
private JTextArea textAddress;

public TextAreaSample(){
initComponents();
}

public void initComponents(){


frame = new JFrame("Employee Frame");
panel = new JPanel();
textAddress = new JTextArea(3,15);

panel.add(textAddress);

frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(300,150);
}

public static void main(String[] args){


TextAreaSample textareasample = new TextAreaSample();
}
}
SAMPLE14 OUTPUT:
Displaying Text in a Dialog Box
•Display
–Most Java applications use windows or a dialog box
•We have used command window
–Class JOptionPane allows us to use dialog boxes
•Packages
–Set of predefined classes for us to use
–Groups of related classes called packages
•Group of all packages known as Java class library or Java applications programming interface (Java
API)
–JOptionPane is in the javax.swing package
•Package has classes for using Graphical User Interfaces (GUIs)

Swing - is one of the main improvements in the versions of JDK starting from 1.2 Swing was the
result of collaborative effort by Sun Microsystems, IBM, Netscape, and other companies.

SYNTAX: JOptionPane.showMessageDialog(null, “message”, “title”,icon_type);


Note! Without the final statement System.exit(0); a program that ends with a message dialog
box will still be active after the dialog box has been closed.

//Sample 15 program using swing


import javax.swing.*;

public class M3
{
public static void main(String args[])
{
JOptionPane.showMessageDialog(null,"WELCOME LADY CROFT!","WELCOME",
JOptionPane.WARNING_MESSAGE);
System.exit(0); // terminate application with window
}
}
Sample 15 Output:
If a JOptionPane has configured to all input setWantsInput the bound property
JOptionPane.INPUT_VALUE_PROPERTY can also be listened to, to determine when the user has input or
selected a value.
When one of the showXxxDialog methods returns an integer, the possible values are:
YES_OPTION

NO_OPTION

CANCEL_OPTION

OK_OPTION

CLOSED_OPTION
Examples:
Show an error dialog that displays the message, 'alert':
JOptionPane.showMessageDialog(null, "alert", "alert",
JOptionPane.ERROR_MESSAGE);
Show an internal information dialog with the message, 'information':
JOptionPane.showInternalMessageDialog(frame, "information",
"information", JOptionPane.INFORMATION_MESSAGE);
Show an information panel with the options yes/no and message 'choose one':
JOptionPane.showConfirmDialog(null,
"choose one", "choose one", JOptionPane.YES_NO_OPTION);
Show an internal information dialog with the options yes/no/cancel and message 'please choose one' and
title information:
JOptionPane.showInternalConfirmDialog(frame,
"please choose one", "information",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
Show a warning dialog with the options OK, CANCEL, title 'Warning', and message 'Click OK to
continue':
Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
Show a dialog asking the user to type in a String:
String inputValue = JOptionPane.showInputDialog("Please input a value");
Show a dialog asking the user to select a String:
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
Problem: Create a program using swing that will let the user to enter
a name and 2 numbers then display the name and the sum of 2
numbers.
//SAMPLE 16

import javax.swing.*;

public class M4
{
public static void main(String args[])
{ String sName;
int num1, num2, sum=0;

sName = JOptionPane.showInputDialog("ENTER Name");


num1 = Integer.parseInt(JOptionPane.showInputDialog("ENTER FIRST NUMBER"));
num2 = Integer.parseInt(JOptionPane.showInputDialog("ENTER SECOND NUMBER"));

sum = num1 + num2;

JOptionPane.showMessageDialog(null, "HI " +sName+ "\n THE SUM IS " +sum, "RESULTS",
JOptionPane.PLAIN_MESSAGE);

System.exit(0);
}
}

Sample 16 Output:

You might also like