Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Swing Sum of a Number using Swing

Related Tutorials


 
 

Share on Google+Share on Google+

Sum of a Number using Swing

Advertisement
In this section, you will learn how to sum of a number using swing. JTextField, JButton is a component of GUI.

Sum of a Number using Swing

     

In this section, you will learn how to sum of a number using swing. JTextField, JButton is a component of GUI.

Brief description of the Component used in this application :

1. JFrame is used to  creating  java standalone application, you must provide GUI for a user. The most common way of creating a frame is, 
using single argument constructor of the JFrame class. The argument of the constructor is the title of the window or frame.

     JFrame f = new JFrame("Calculate");

2. JTextField is a lightweight component that allows the editing of a single line of text. JTextField has a method to establish the string 
used as the command string for the action event that gets fired. The java.awt.TextField used the text of the field as the command string 
or the ActionEvent. JTextField will use the command string set with the setActionCommand method if not null, otherwise it will use the
 text of the field as a compatibility with java.awt.TextField. 

   final JTextField  text1 = new JTextField(20);
   final JTextField text2 = new JTextField(20);

3. JButton with a String as a label, and then drop it in a window. Events are normally handled just as with a Button: you attach an 
ActionListener via the addActionListener method. 

    JButton button = new JButton("Calculate");

Step: Create a file "Calculate.java" to sum of a number using Swing.

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


public class Calculate extends JTextField {

  public static void main(String[] args) {
  JFrame f = new JFrame("Calculate");
  f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), 
  BoxLayout.Y_AXIS));

  final JTextField  text1 = new JTextField(20);
  

  final JTextField text2 = new JTextField(20);
  

  JPanel panel = new JPanel();
  JButton button = new JButton("Calculate");
  panel.add(button);
  f.getContentPane().add(text1);
  f.getContentPane().add(panel);
  f.getContentPane().add(text2);
  
  ActionListener l = new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  System.out.println("Action event from a text field");
  }
  };
  text1.addActionListener(l);
  text2.addActionListener(l);
  button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  int a=Integer.parseInt(text1.getText()); 
  int sum = 0;
  int count=1;
  while(count<=a)
  {
  sum+=count;
  count++;
  }
  text2.setText(String.valueOf(sum));
  }
  });
  f.pack();
  f.setVisible(true);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Output :

Download the full Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 18, 2008

Related Tutorials

Discuss: Sum of a Number using Swing   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
sebaha comp
April 3, 2011
comment

simple source code is
DMCA.com