Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Java Swings
• A graphical user interface (GUI) presents a user-
friendly mechanism for interacting with an
application. These are sometimes called controls
or widgets—short for window gadgets. A GUI
component is an object with which the user
interacts via the mouse, the keyboard or another
form of input, such as voice recognition.
• Java’s so-called Swing GUI components from the
javax.swing package. We cover other
Complete java swing
Complete java swing
Complete java swing
Simple GUI-Based Input/Output with
JOptionPane
• Most applications you use on a daily basis use windows or
dialog boxes (also called dialogs) to interact with the user.
For example, an e-mail program allows you to type and
read messages in a window the program provides. Dialog
boxes are windows in which programs display important
messages to the user or obtain information from the user.
Java’s JOptionPane class (package javax.swing) provides
prebuilt dialog boxes for both input and output. These are
displayed by invoking static JOptionPane methods. Program
presents a simple addition application that uses two input
dialogs to obtain integers from the user and a message
dialog to display the sum of the integers the user enters.
• // Addition.java
• // Addition program that uses JOptionPane for input and output.
• import javax.swing.JOptionPane; // program uses JOptionPane
• public class Addition
• {
• public static void main( String[] args )
• {
• // obtain user input from JOptionPane input dialogs
• String firstNumber =
• JOptionPane.showInputDialog( "Enter first integer" );
• String secondNumber =
• JOptionPane.showInputDialog( "Enter second integer" );
Complete java swing
Complete java swing
• private void
jButton1ActionPerformed(java.awt.event.Acti
onEvent evt) {
• JOptionPane.showMessageDialog(null, "Thank
you");// TODO add your handling code here:
• }
Overview of Swing Components
Complete java swing
Complete java swing
Complete java swing
Complete java swing
Non-AWT Upgraded Components
• In addition to offering replacements for all the
basic AWT components, the Swing component
• set includes twice as many new components.
Complete java swing
Complete java swing
Complete java swing
Complete java swing
Complete java swing
• package hello;
• import javax.swing.*;
• class jpswrd
• {
• public void Testing()
• {
• JPasswordField pwd = new JPasswordField(10);
• int action = JOptionPane.showConfirmDialog(null, pwd,"Enter
Password",JOptionPane.OK_CANCEL_OPTION);
• if(action < 0)JOptionPane.showMessageDialog(null,"Cancel, X or escape key selected");
• else JOptionPane.showMessageDialog(null,"Your password is "+new
String(pwd.getPassword()));
• System.exit(0);
• }
• public static void main(String args[])
• {jpswrd p= new jpswrd();
• p.Testing();}
• }
output
• setEchoChar('+');
• Set character for password field.
JFrame
• A Frame is a top-level window with a title and
a border.
• Frame that adds support for the Swing
component architecture.
JFrame Features
 It’s a window with title, border, (optional)
menu bar and user-specified components.
.It can be moved, resized, iconified.
.It is not a subclass of JComponent.
.Delegates responsibility of managing user-
specified components to a content pane, an
instance of JPanel.
Centering JFrame’s
• By default, a Jframe is displayed in the upper-
left corner of the screen. To display a frame
at a specified location, you can use the
setLocation(x, y) method in the JFrame class.
This method places the upper-left corner of a
frame at location (x, y).
Class constructors
• JFrame()
Constructs a new frame that is initially
invisible.
• JFrame(String title)
Creates a new, initially invisible Frame with
the specified title.
JButton
• A button is a component the user clicks to
trigger a specific action. A Java application
can use several types of buttons, including
command buttons, checkboxes, toggle
buttons and radio buttons.
• all the button types are subclasses of
AbstractButton (package javax.swing), which
declares the common features of Swing
buttons.
Complete java swing
Buttons That Maintain State
• The Swing GUI components contain three
types of state buttons—JToggleButton,
• JCheckBox and JRadioButton—that have
on/off or true/false values. Classes
JCheckBox and JRadioButton are subclasses
of JToggleButton
JLabel
• A JLabel displays read-only text, an image, or
both text and an image.
JFileChooser
Allows the the user to choose a file
• import javax.swing.JFileChooser;
• public class swing_examples {
• public static void main(String args[])
• {
• JFileChooser fc = new JFileChooser();
• int returnVal = fc.showOpenDialog(null);
• if(returnVal == JFileChooser.APPROVE_OPTION)
• System.out.println("File: " + fc.getSelectedFile());
• }
• }
JCheckBox Class
• The class JCheckBox is an implementation of a
check box - an item that can be selected or
deselected, and which displays its state to the
user.
Class constructors
of JcheckBox
Complete java swing
Complete java swing
output
/*code for checkbox that display bold text*/
• font=new Font("",Font.BOLD,14);
• jTextField1.setFont(font);
• /*code for checkbox that display italic text*/
• font=new Font("",Font.ITALIC,14);
• jTextField1.setFont(font);
Complete java swing
Complete java swing
JRadio Button
• The class JRadioButton is an implementation
of a radio button - an item that can be
selected or deselected, and which displays its
state to the user.
Complete java swing
Mathematical Operations using Radio
Buttons
• private void
jButton1ActionPerformed(java.awt.event.Acti
onEvent evt) {
• jTextField1.setText("");
• jTextField2.setText("");
• jTextField3.setText("");
Addition RadioButton
• private void
jRadioButton1ActionPerformed(java.awt.event.A
ctionEvent evt) {
• int num1,num2,result;
• num1=Integer.parseInt(jTextField1.getText());
• num2=Integer.parseInt(jTextField2.getText());
• result=num1+num2;
• jTextField3.setText(String.valueOf(result));
Multiplication RadioButton
• private void
jRadioButton2ActionPerformed(java.awt.event.A
ctionEvent evt) {
• int num1,num2,result;
• num1=Integer.parseInt(jTextField1.getText());
• num2=Integer.parseInt(jTextField2.getText());
• result=num1*num2;
• jTextField3.setText(String.valueOf(result)); //
TODO add your handling code here:
• }
Subtraction RadioButton
• private void
jRadioButton3ActionPerformed(java.awt.event.A
ctionEvent evt) {
• int num1,num2,result;
• num1=Integer.parseInt(jTextField1.getText());
• num2=Integer.parseInt(jTextField2.getText());
• result=num1-num2;
• jTextField3.setText(String.valueOf(result)); //
TODO add your handling code here:
• }
Complete java swing
Complete java swing
Complete java swing

More Related Content

Complete java swing

  • 2. • A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application. These are sometimes called controls or widgets—short for window gadgets. A GUI component is an object with which the user interacts via the mouse, the keyboard or another form of input, such as voice recognition. • Java’s so-called Swing GUI components from the javax.swing package. We cover other
  • 6. Simple GUI-Based Input/Output with JOptionPane • Most applications you use on a daily basis use windows or dialog boxes (also called dialogs) to interact with the user. For example, an e-mail program allows you to type and read messages in a window the program provides. Dialog boxes are windows in which programs display important messages to the user or obtain information from the user. Java’s JOptionPane class (package javax.swing) provides prebuilt dialog boxes for both input and output. These are displayed by invoking static JOptionPane methods. Program presents a simple addition application that uses two input dialogs to obtain integers from the user and a message dialog to display the sum of the integers the user enters.
  • 7. • // Addition.java • // Addition program that uses JOptionPane for input and output. • import javax.swing.JOptionPane; // program uses JOptionPane • public class Addition • { • public static void main( String[] args ) • { • // obtain user input from JOptionPane input dialogs • String firstNumber = • JOptionPane.showInputDialog( "Enter first integer" ); • String secondNumber = • JOptionPane.showInputDialog( "Enter second integer" );
  • 10. • private void jButton1ActionPerformed(java.awt.event.Acti onEvent evt) { • JOptionPane.showMessageDialog(null, "Thank you");// TODO add your handling code here: • }
  • 11. Overview of Swing Components
  • 16. Non-AWT Upgraded Components • In addition to offering replacements for all the basic AWT components, the Swing component • set includes twice as many new components.
  • 22. • package hello; • import javax.swing.*; • class jpswrd • { • public void Testing() • { • JPasswordField pwd = new JPasswordField(10); • int action = JOptionPane.showConfirmDialog(null, pwd,"Enter Password",JOptionPane.OK_CANCEL_OPTION); • if(action < 0)JOptionPane.showMessageDialog(null,"Cancel, X or escape key selected"); • else JOptionPane.showMessageDialog(null,"Your password is "+new String(pwd.getPassword())); • System.exit(0); • } • public static void main(String args[]) • {jpswrd p= new jpswrd(); • p.Testing();} • }
  • 24. • setEchoChar('+'); • Set character for password field.
  • 25. JFrame • A Frame is a top-level window with a title and a border. • Frame that adds support for the Swing component architecture.
  • 26. JFrame Features  It’s a window with title, border, (optional) menu bar and user-specified components. .It can be moved, resized, iconified. .It is not a subclass of JComponent. .Delegates responsibility of managing user- specified components to a content pane, an instance of JPanel.
  • 27. Centering JFrame’s • By default, a Jframe is displayed in the upper- left corner of the screen. To display a frame at a specified location, you can use the setLocation(x, y) method in the JFrame class. This method places the upper-left corner of a frame at location (x, y).
  • 28. Class constructors • JFrame() Constructs a new frame that is initially invisible. • JFrame(String title) Creates a new, initially invisible Frame with the specified title.
  • 29. JButton • A button is a component the user clicks to trigger a specific action. A Java application can use several types of buttons, including command buttons, checkboxes, toggle buttons and radio buttons.
  • 30. • all the button types are subclasses of AbstractButton (package javax.swing), which declares the common features of Swing buttons.
  • 32. Buttons That Maintain State • The Swing GUI components contain three types of state buttons—JToggleButton, • JCheckBox and JRadioButton—that have on/off or true/false values. Classes JCheckBox and JRadioButton are subclasses of JToggleButton
  • 33. JLabel • A JLabel displays read-only text, an image, or both text and an image.
  • 34. JFileChooser Allows the the user to choose a file • import javax.swing.JFileChooser; • public class swing_examples { • public static void main(String args[]) • { • JFileChooser fc = new JFileChooser(); • int returnVal = fc.showOpenDialog(null); • if(returnVal == JFileChooser.APPROVE_OPTION) • System.out.println("File: " + fc.getSelectedFile()); • } • }
  • 35. JCheckBox Class • The class JCheckBox is an implementation of a check box - an item that can be selected or deselected, and which displays its state to the user.
  • 40. /*code for checkbox that display bold text*/ • font=new Font("",Font.BOLD,14); • jTextField1.setFont(font); • /*code for checkbox that display italic text*/ • font=new Font("",Font.ITALIC,14); • jTextField1.setFont(font);
  • 43. JRadio Button • The class JRadioButton is an implementation of a radio button - an item that can be selected or deselected, and which displays its state to the user.
  • 45. Mathematical Operations using Radio Buttons • private void jButton1ActionPerformed(java.awt.event.Acti onEvent evt) { • jTextField1.setText(""); • jTextField2.setText(""); • jTextField3.setText("");
  • 46. Addition RadioButton • private void jRadioButton1ActionPerformed(java.awt.event.A ctionEvent evt) { • int num1,num2,result; • num1=Integer.parseInt(jTextField1.getText()); • num2=Integer.parseInt(jTextField2.getText()); • result=num1+num2; • jTextField3.setText(String.valueOf(result));
  • 47. Multiplication RadioButton • private void jRadioButton2ActionPerformed(java.awt.event.A ctionEvent evt) { • int num1,num2,result; • num1=Integer.parseInt(jTextField1.getText()); • num2=Integer.parseInt(jTextField2.getText()); • result=num1*num2; • jTextField3.setText(String.valueOf(result)); // TODO add your handling code here: • }
  • 48. Subtraction RadioButton • private void jRadioButton3ActionPerformed(java.awt.event.A ctionEvent evt) { • int num1,num2,result; • num1=Integer.parseInt(jTextField1.getText()); • num2=Integer.parseInt(jTextField2.getText()); • result=num1-num2; • jTextField3.setText(String.valueOf(result)); // TODO add your handling code here: • }