Complete java swing

51
Java Swings

Transcript of Complete java swing

Page 1: Complete java swing

Java Swings

Page 2: Complete java swing

• 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

Page 3: Complete java swing
Page 4: Complete java swing
Page 5: Complete java swing
Page 6: Complete java swing

Simple GUI-Based Input/Output with JOptionPane

• Most applications you use on a daily basis use windows ordialog 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.

Page 7: Complete java swing

• // 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" );

Page 8: Complete java swing
Page 9: Complete java swing
Page 10: Complete java swing

• private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

• JOptionPane.showMessageDialog(null, "Thank you");// TODO add your handling code here:

• }

Page 11: Complete java swing

Overview of Swing Components

Page 12: Complete java swing
Page 13: Complete java swing
Page 14: Complete java swing
Page 15: Complete java swing
Page 16: 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.

Page 17: Complete java swing
Page 18: Complete java swing
Page 19: Complete java swing
Page 20: Complete java swing
Page 21: Complete java swing
Page 22: 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();} • }

Page 23: Complete java swing

output

Page 24: Complete java swing

• setEchoChar('+');• Set character for password field.

Page 25: Complete java swing

JFrame

• A Frame is a top-level window with a title and a border.

• Frame that adds support for the Swing component architecture.

Page 26: Complete java swing

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.

Page 27: Complete java swing

Centering JFrame’s

• By default, a Jframe is displayed in the upper-left corner of the screen. To display a frameat 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).

Page 28: Complete java swing

Class constructors

• JFrame()Constructs a new frame that is initially invisible.

• JFrame(String title) Creates a new, initially invisible Frame with the specified title.

Page 29: Complete java swing

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.

Page 30: Complete java swing

• all the button types are subclasses of AbstractButton (package javax.swing), which declares the common features of Swing buttons.

Page 31: Complete java swing
Page 32: 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

Page 33: Complete java swing

JLabel

• A JLabel displays read-only text, an image, or both text and an image.

Page 34: Complete java swing

JFileChooserAllows 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());

• }• }

Page 35: Complete java swing

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.

Page 36: Complete java swing

Class constructorsof JcheckBox

Page 37: Complete java swing
Page 38: Complete java swing
Page 39: Complete java swing

output

Page 40: Complete java swing

/*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);

Page 41: Complete java swing
Page 42: Complete java swing
Page 43: 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.

Page 44: Complete java swing
Page 45: Complete java swing

Mathematical Operations using Radio Buttons

• private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

• jTextField1.setText("");• jTextField2.setText("");• jTextField3.setText("");

Page 46: Complete java swing

Addition RadioButton

• private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {

• int num1,num2,result;• num1=Integer.parseInt(jTextField1.getText());• num2=Integer.parseInt(jTextField2.getText());• result=num1+num2;• jTextField3.setText(String.valueOf(result));

Page 47: Complete java swing

Multiplication RadioButton

• private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent 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:• }

Page 48: Complete java swing

Subtraction RadioButton

• private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent 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:• }

Page 49: Complete java swing
Page 50: Complete java swing
Page 51: Complete java swing