I WAS WONDERING IF SOMEONE COULD HELP ME MOVE THE JTextField above all my buttons like a normal calculator. Also, after this I don't know how to connect the basic calculations to operation buttons. When the user clicks a button the text appears in the textfield but I don't know how to make the operation buttons work.

CODE:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import java.awt.peer.ButtonPeer;
import java.util.*;
import java.math.*;

public class Calculator extends JFrame
{
	double sum=0;
	double subtraction=0;
	double division=0;
	double multiplication=0;
	private final Font BIGGER_FONT = new Font("TimesNewRoman", Font.PLAIN, 20);
	JTextField textField1;
	
	public static void main(String args[])
	{
		
		Calculator c = new Calculator();
	    JFrame f = new JFrame("Calculator");
		Container lm = c.getContentPane();
		c.setSize(800,500);
		c.setLocation(100,100);
		lm.setBackground(Color.blue);
		c.setVisible(true);
		
	    c.setTitle("Calculator");
	}
	
	public Calculator()
	{
        	   setLayout(new GridLayout(7,7,10,5));
        	   
        	   textField1 = new JTextField();
       	       add(textField1);       	        
       	       textField1.setSize(1000,1000);
       	       
			   setSize(100,150);
			   JButton num0 = new JButton("0");
			   add(num0);
			   num0.addActionListener(new bListener());
			   num0.setLayout(new GridLayout(1,6));
			   
			   setSize(100,150);
			   JButton num1 = new JButton("1");
			   add(num1);
			   num1.addActionListener(new bListener());
			   num1.setLayout(new GridLayout(0,1));
			   
			   setSize(100,150);
			   JButton num2 = new JButton("2");
			   add(num2);
			   num2.addActionListener(new bListener());
			   num2.setLayout(new GridLayout(0,2));

			   setSize(100,150);
			   JButton num3 = new JButton("3");
			   add(num3);
			   num3.addActionListener(new bListener());
			   num3.setLayout(new GridLayout(0,3));

			   setSize(100,150);
			   JButton num4 = new JButton("4");
			   add(num4);
			   num4.addActionListener(new bListener());
			   num4.setLayout(new GridLayout(0,4));

			   
			   setSize(100,150);
			   JButton num5 = new JButton("5");
			   add(num5);
			   num5.addActionListener(new bListener());
			   num5.setLayout(new GridLayout(0,5));

			   setSize(100,150);
			   JButton num6 = new JButton("6");
			   add(num6);
			   num6.addActionListener(new bListener());
			   num6.setLayout(new GridLayout(0,6));

			   setSize(100,150);
			   JButton num7 = new JButton("7");
			   add(num7);
			   num7.addActionListener(new bListener());
			   num7.setLayout(new GridLayout(0,7));

			   setSize(100,150);
			   JButton num8 = new JButton("8");
			   add(num8);
			   num8.addActionListener(new bListener());
			   num8.setLayout(new GridLayout(0,8));

			   setSize(100,150);
			   JButton num9 = new JButton("9");
			   add(num9);
			   num9.addActionListener(new bListener());
			   num9.setLayout(new GridLayout(1,0));
			   
			   setSize(100,150);
			   JButton add = new JButton("+");
			   add(add);
			   add.addActionListener(new bListener());
			   add.setLayout(new GridLayout(1,1));

			   setSize(100,150);
			   JButton minus = new JButton("-");
			   add(minus);
			   minus.addActionListener(new bListener());
			   minus.setLayout(new GridLayout(1,2));

			   setSize(100,150);
			   JButton multiply = new JButton("x");
			   add(multiply);
			   multiply.addActionListener(new bListener());
			   multiply.setLayout(new GridLayout(1,3));

			   setSize(100,150);
			   JButton divide = new JButton("/");
			   add(divide);
			   divide.addActionListener(new bListener());
			   divide.setLayout(new GridLayout(1,4));
			   
			   setSize(100,150);
			   JButton cos = new JButton("cos");
			   add(cos);
			   cos.addActionListener(new bListener());
			   cos.setLayout(new GridLayout(1,5));
			   
			   setSize(100,150);
			   JButton sin = new JButton("sin");
			   add(sin);
			   sin.addActionListener(new bListener());
			   sin.setLayout(new GridLayout(1,6));

			   setSize(100,150);
			   JButton tan= new JButton("tan");
			   add(tan);
			   tan.addActionListener(new bListener());
			   tan.setLayout(new GridLayout(1,7));
			   
			   setSize(100,150);
			   JButton arcsin = new JButton("arc sin");
			   add(arcsin);
			   arcsin.addActionListener(new bListener());
			   arcsin.setLayout(new GridLayout(1,8));
			   
			   setSize(100,150);
			   JButton arccos = new JButton("arc cos");
			   add(arccos);
			   arccos.addActionListener(new bListener());
			   arccos.setLayout(new GridLayout(1,9));
			   
			   setSize(100,150);
			   JButton arctan = new JButton("arc tan");
			   add(arctan);
			   arctan.addActionListener(new bListener());
			   arctan.setLayout(new GridLayout(2,0));
			   
			   
			   setDefaultCloseOperation(EXIT_ON_CLOSE);
			   

			   //User Interface code ends here
			}
	
	class bListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) {
			JButton temp = (JButton)e.getSource();
			String btext = temp.getText();
			String tftext = textField1.getText();
			
			textField1.setText(tftext + btext);
		}
	
	
		
	}
	}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Please edit your code and add CODE tags to preserve the formatting. See http://www.java-forums.org/misc.php?do=bbcode#code

If you want your text field all by it self at the top of the window, you'll need to use another layout manager. You can nest your usage of layout managers. Have one for the textfield at the top and the rest of the buttons with another layout manager in the center. See the BorderLayout for that style.

I don't know how to make the operation buttons work.

Can you describe what action you want taken when the operation buttons are pressed.

actions I want are adding, subtracting, dividing, and multiplying. What layout managers should I use?

What layout managers should I use?

Look at the BorderLayout.

actions I want are adding, subtracting, dividing, and multiplying.

For addition: take the first operand and add it to the second
Do similar for the rest of the actions replacing add with the appropriate operation.

To know which action to take, you could have a different listener for each button
Or you could see which component was pressed and key your actions on that.

Edited 5 Years Ago by NormR1: n/a

This article has been dead for over six months. Start a new discussion instead.