Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Multiple Integers in a JTextField

$
0
0
How do I add multiple integers into the JTextField, like if I already pushed one button and I push another, how do I make it so that the second number adds on to the first.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Display {
    public static void main(String args[]){
        
        int num1;
        int num2;
        int sum;
        final int var;
        
        JFrame frame = new JFrame("Calculator");
        frame.setSize(300, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
        final JTextField jtf = new JTextField();
        jtf.setSize(100, 40);
        frame.add(jtf);
        jtf.setLocation(100, 1);
        
        JButton one = new JButton("   1   ");
        one.setSize(50, 20);
        frame.getContentPane().add(one);
        one.setLocation(30, 50);
        one.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               jtf.setText("1");
            }
    }
        
        JButton two = new JButton("   2   ");
        two.setSize(50, 20);
        frame.getContentPane().add(two);
        two.setLocation(90, 50);
        two.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               jtf.setText("2");

            }
    }
        
        JButton three = new JButton("   3   ");
        three.setSize(50, 20);
        frame.getContentPane().add(three);
        three.setLocation(150, 50);
        three.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
                jtf.setText("3");
            }
    }
        
        JButton add = new JButton("   +   ");
        add.setSize(50, 20);
        frame.getContentPane().add(add);
        add.setLocation(210, 50);
        add.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               
            }
    }
        
        JButton four = new JButton("   4   ");
        four.setSize(50, 20);
        frame.getContentPane().add(four);
        four.setLocation(30, 80);
        four.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               jtf.setText("4");
            }
    }
        
        JButton five = new JButton("   5   ");
        five.setSize(50, 20);
        frame.getContentPane().add(five);
        five.setLocation(90, 80);
        five.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               jtf.setText("5");
            }
    }
        
        JButton six = new JButton("   6   ");
        six.setSize(50, 20);
        frame.getContentPane().add(six);
        six.setLocation(150, 80);
        six.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               jtf.setText("6");
            }
    }
        
        JButton sub = new JButton("   -   ");
        sub.setSize(50, 20);
        frame.getContentPane().add(sub);
        sub.setLocation(210, 80);
        sub.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               
            }
    }
        
        JButton seven = new JButton("   7   ");
        seven.setSize(50, 20);
        frame.getContentPane().add(seven);
        seven.setLocation(30, 110);
        seven.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               jtf.setText("7");
            }
    }
        
        JButton eight = new JButton("   8   ");
        eight.setSize(50, 20);
        frame.getContentPane().add(eight);
        eight.setLocation(90, 110);
        eight.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               jtf.setText("8");
            }
    }
        
        JButton nine = new JButton("   9   ");
        nine.setSize(50, 20);
        frame.getContentPane().add(nine);
        nine.setLocation(150, 110);
        nine.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               jtf.setText("9");
            }
    }
        
        JButton mult = new JButton("   *   ");
        mult.setSize(50, 20);
        frame.getContentPane().add(mult);
        mult.setLocation(210, 110);
        mult.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               
            }
    }
        
        JButton zero = new JButton("   0   ");
        zero.setSize(50, 20);
        frame.getContentPane().add(zero);
        zero.setLocation(90, 140);
        zero.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               jtf.setText("0");
            }
    }
        
        JButton div = new JButton("   /   ");
        div.setSize(50, 20);
        frame.getContentPane().add(div);
        div.setLocation(210, 140);
        div.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
               
            }
    }
                
    
        
    }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles