User Interface Part 1
User Interface Part 1
//SAMPLE7
import java.awt.*;
import java.awt.event.*;
MNB()
{
super("Example: MenuBar");
setMenuBar(mb);
mi = new MenuItem("Exit");
mi.addActionListener(new ActionListener() {
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 CreatNewFrame() {
getContentPane().setLayout(new GridLayout(1,0));
nf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
public NewFrame() {
getContentPane().setLayout(new FlowLayout());
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.*;
DLG()
{
super("Example: Swing Dialog");
jb.addActionListener(new ActionListener() {
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 PanelSample(){
initComponents();
}
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(100,150);
}
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
public TextFieldSample(){
initComponents();
}
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(300,200);
}
public ListSample(){
initComponents();
}
panel.add(list);
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(300,200);
}
SAMPLE 12 OUTPUT:
SAMPLE 13: The JComboBox Class
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
public ComboSample(){
initComponents();
}
panel.add(combo);
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(500,500);
}
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public TextAreaSample(){
initComponents();
}
panel.add(textAddress);
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(300,150);
}
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.
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;
JOptionPane.showMessageDialog(null, "HI " +sName+ "\n THE SUM IS " +sum, "RESULTS",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
Sample 16 Output: