Complete java swing

Post on 09-Feb-2017

409 views 6 download

Transcript of Complete java swing

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

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.

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

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

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

• }

Overview of Swing Components

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.

• 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 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).

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.

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.

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

• }• }

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 constructorsof JcheckBox

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

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.

Mathematical Operations using Radio Buttons

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

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

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

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:• }

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:• }