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



Home Answers Viewqa Java-Beginners adding of two numbers in designing of frame
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


shyamalagowri
adding of two numbers in designing of frame
2 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners


hello sir, now i'm create two textfield for mark1&mark2 from db.how to add these two numbers in another one text field.how to write a coding... if u have another one idea pls tell me........


Advertisement
View Answers

November 23, 2010 at 11:47 AM


Hi Friend,

Try the following code:

import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
class  SumOfNumbers{

    public static void main(String[] args){
    JFrame f=new JFrame();
    JLabel label1=new JLabel("First Number: ");
    JLabel label2=new JLabel("Second Number: ");
    JLabel label3=new JLabel("Result: ");
    final JTextField text1=new JTextField(20);
    final JTextField text2=new JTextField(20);
    final JTextField text3=new JTextField(20);
    JButton button=new JButton("Calculate");
    try{
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           Connection con = DriverManager.getConnection("jdbc:odbc:student");
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from student where id=1");
           int mar1=0,mar2=0;
           if(rs.next()){
               mar1=rs.getInt("marks1");
               mar2=rs.getInt("marks2");
           }
           text1.setText(Integer.toString(mar1));
           text2.setText(Integer.toString(mar2));
           button.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
               int num1=Integer.parseInt(text1.getText());
               int num2=Integer.parseInt(text2.getText());
               int result=num1+num2;
               text3.setText(Integer.toString(result));
               }
            });
        }
        catch(Exception e){
        }
        JPanel p=new JPanel(new GridLayout(4,2));
        p.add(label1);
        p.add(text1);
        p.add(label2);
        p.add(text2);
        p.add(label3);
        p.add(text3);
        p.add(button);

        f.add(p);
        f.setVisible(true);
        f.pack();
    }
}

Thanks



November 27, 2012 at 5:07 PM


adding of two numbers in designing of frame package swings;

import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JTextPane;

public class Swings extends JFrame implements ActionListener {

JButton jb1;
JTextField jt3;
JButton jb2;
JLabel jl1;
JTextField jt1;
JLabel jl2;
JTextField jt2;
float c;

Swings() {

    jb1 = new JButton("Sum");
    jt3 = new JTextField(10);
    jb2 = new JButton("reset");
    jl1 = new JLabel("First number");
    jt1 = new JTextField(10);
    jl2 = new JLabel("Second number number");
    jt2 = new JTextField(10);
    Container con = getContentPane();
    JPanel jp = new JPanel(new FlowLayout(FlowLayout.LEFT));

    jp.add(jl1);
    jp.add(jt1);
    jp.add(jl2);
    jp.add(jt2);
    jp.add(jb1);
    jp.add(jt3);
    jp.add(jb2);

    con.add(jp);
    jb1.addActionListener(this);
    jb2.addActionListener(this);

}

public static void main(String[] args) throws IOException {
    Swings sw = new Swings();
    sw.setVisible(true);
    sw.setSize(250, 250);
    sw.setResizable(true);
    sw.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jb1) {
        if ((jt1 != null && !"".equals(jt1)) && (jt2 != null && !"".equals(jt2))) {
            try {
                float a = Integer.parseInt(jt1.getText());
                float b = Integer.parseInt(jt1.getText());
                c = a + b;
                jt3.setText(Float.toString(c));
            } catch (NumberFormatException ae) {
                System.out.println("This is not a number");
                System.out.println(ae.getMessage());
            }
        }
    } else {
        jt1.setText("");
        jt2.setText("");
        jt3.setText("");
    }

}

}



Related Tutorials/Questions & Answers:
adding of two numbers in designing of frame
adding of two numbers in designing of frame  hello sir, now i'm create two textfield for mark1&mark2 from db.how to add these two numbers in another one text field.how to write a coding... if u have another one idea pls
Adding two numbers
Adding two numbers  Accepting value ffrom the keyboard and adding two numbers
Advertisements
adding two numbers - Java Beginners
adding two numbers  hii friends....... this is my program...]=Integer.parseInt(in.readLine()); int s=x[1]+y[1]; System.out.println("Sum of two...]; System.out.println("Sum of two no. is:"+s); } } For read more
adding two numbers using bitwise operators
adding two numbers using bitwise operators  adding two integer numbers with using bitwise opeators   Hi Friend, Try the following code...[] args) { System.out.println("Adding 5 and 6......"); int x=5,y=6; int xor
adding two numbers with out using any operator
adding two numbers with out using any operator  how to add two numbers with out using any operator   import java.math.*; class AddNumbers { public static void main(String[] args) { BigInteger num1=new
Addition of two numbers
Addition of two numbers  addition of two numbers
Listing all even numbers between two numbers
Listing all even numbers between two numbers  Hi, How to write code to list all the even numbers between two given numbers? Thanks   Hi... the numbers. Check the tutorial Write a program to list all even numbers between two
Applet for add two numbers
); add(text2); label3 = new Label("Sum of Two Numbers...Applet for add two numbers  what is the java applet code for add two numbers?   import java.awt.Graphics; import javax.swing.*; public
problem in adding a fullscreen frame nd adding a glass pane to it
problem in adding a fullscreen frame nd adding a glass pane to it  hi , here is my code import java.awt.*; import javax.swing.*; import java.awt.ToolKit; class MyFrame extends Jframe{ Container c; JPanel glass; MyFrame
Add two big numbers
Add two big numbers       In this section, you will learn how to add two big numbers. For adding two numbers implement two big decimal numbers then apply the Sum() method
Add two big numbers - Java Beginners
Add two big numbers - Java Beginners  Hi, I am beginner in Java and leaned basic concepts of Java. Now I am trying to find example code for adding big numbers in Java. I need basic Java Beginners example. It should easy
mysql difference between two numbers
mysql difference between two numbers  How to get total bate difference between two dates for example 1/01/2012 and 1/02/2012 in MYSQL?   ... between two date. The syntax of DATEDIFF is .. SELECT DATEDIFF('2012-01-31 23:59
To find first two maximum numbers in an array
To find first two maximum numbers in an array  Java program to find first two maximum numbers in an array,using single loop without sorting array
Comparing Two Numbers
Comparing Two Numbers      ... of comparing two numbers and finding out the greater one. First of all, name a class "Comparing" and take two numbers in this class. Here we have taken a=24
Swapping of two numbers
Swapping of two numbers      ... program to calculate swap of two numbers. Swapping is used where  you want... ability. In this program we will see how we can swap two numbers. We can do
Write a program to list all even numbers between two numbers
Write a program to list all even numbers between two numbers       Java Even Numbers - Even... all the even numbers between two numbers. For this first create a class named
Swapping of two numbers in java
Swapping of two numbers in java In this example we are going to describe swapping of two numbers in java  without using the third number in java. We... values from the command prompt. The swapping of two numbers is based on simple
Swapping of two numbers without using third variable
Swapping of two numbers without using third variable In this tutorial we will learn about swapping of two number in java without using third variable. This is simple program to swap two value using arithmetic operation Swapping of two
JavaScript determine add, product of two numbers
JavaScript determine add, product of two numbers In this section, you... numbers. To find this, we have created two textboxes and four button...("Sum of two numbers is: "+sum); } function divide
FRAME
FRAME  WHILE I'M RUNNINGFILE OF A GUI PROGRAMME(JDBC CONNECTION) INSTEAD OF OUTPUT FRAME ONLY TWO BLANK FRAMES ARE GETTING DISPLAYD... CAN ANYONE HELP ME TO SOLVE DS PROBLEM
FRAME
FRAME  WHILE I'M RUNNINGFILE OF A GUI PROGRAMME(JDBC CONNECTION) INSTEAD OF OUTPUT FRAME ONLY TWO BLANK FRAMES ARE GETTING DISPLAYD... CAN ANYONE HELP ME TO SOLVE DS PROBLEM
Add Two Numbers in Java
Add Two Numbers in Java     ... of two numbers! Sum: 32... these arguments and print the addition of those numbers. In this example, args
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
frame
frame  how to creat a frame in java   Hi Friend, Try the following code: import java.awt.*; import javax.swing.*; import java.awt.event.*; class RetrieveDataFromTextFields{ public static void main(String[] args
Find L.C.M. of two numbers in Java
Find L.C.M. of two numbers in Java Program In this section, you will learn how to find LCM(Least Common Element) of two integers.The least common multiple is the smallest positive integer that is a multiple of both the numbers. Here we
Swap two numbers without using third variable
Swap two numbers without using third variable In this section we are going to swap two variables without using the third variable. For this, we have used... arithmetic operations like we have added both the numbers and stored
Writing and testing method of addition of two numbers
to test the functionality of adding two numbers. So we have created our class named... Writing and testing method of addition of two numbers  ... a method named sum() which takes two int parameters and return addition of these two
Adding an employee
Adding an employee  coding for adding an employee
frame update another frame.
frame update another frame.   How do I make a link or form in one frame update another frame
adding view to viewcontroller
adding view to viewcontroller  How to add a frame window that opens on button click .. in iPhone application
Advertisements
 

 

 

DMCA.com