Hello. I am trying to get back into Java and wanted to start by trying to make a functioning GUI calculator. I have the basic framework for it, but I am having some trouble with the operator. I would like to take what is on the textfield and put that into an ArrayList. Then read the ArrayList to take care of the operators that need immediate attention to follow the order of precedence (Multiple Divide Add Subtract). I also can't seem to get my clear (C) and clear entry (CE) buttons to function correctly either. Any suggestions?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
	JFrame window;                    
    Container content;
    JPanel topPanel;                   
    JTextField expr,result;
    JButton equals;
    JPanel bottomPanel;             
    JPanel digitsPanel,opsPanel;
    JButton[] digits;                  
    JButton[] ops;  

    private String operator= "=";
    private boolean startNumber= true;
    private int resultTotal;
    public Calculator()
    	window = new JFrame("Java GUI Calculator");
        window .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        content = window.getContentPane();
        ButtonListener bListener = new ButtonListener();
        topPanel = new JPanel();
        topPanel.setLayout(  new FlowLayout() );  
        expr = new JTextField(32);
        expr.setFont(new Font("Serif", Font.BOLD, 20));
        topPanel.add( expr );
        equals = new JButton("==");
        equals.setFont(new Font("Serif", Font.BOLD, 40));
        equals.addActionListener(bListener );
        topPanel.add( equals );
        result= new JTextField(8);
        result.setFont(new Font("Serif", Font.BOLD, 20));
        topPanel.add( result );
        bottomPanel = new JPanel();
        bottomPanel.setLayout(  new FlowLayout() ); 
        digitsPanel  = new JPanel();
        digitsPanel.setLayout(  new GridLayout(4,4) );
        digits = new JButton[12];
        for( int i=0; i<12 ; i++)
        	digits[i]=new JButton( ""+i );
            digits[i].setFont(new Font("Serif", Font.BOLD, 40));
            digits[i].addActionListener(bListener );
            digitsPanel.add( digits[i] );
        digits[10].setText( "CE" );
        digits[11].setText( "C" );
        bottomPanel.add( digitsPanel );
        opsPanel  = new JPanel();
        opsPanel.setLayout(  new GridLayout(4,1) );
        JButton[] ops = new JButton[4];
        String[] opsLabels = {"+", "-", "*", "/" };
        ops = new JButton[4];
        for( int i=0; i<4 ; i++)
        	ops[i]=new JButton( opsLabels[i] );
            ops[i].setFont(new Font("Serif", Font.BOLD, 40));
            ops[i].addActionListener(bListener );
            opsPanel.add( ops[i] );
        bottomPanel.add( opsPanel );
        content.add(topPanel, BorderLayout.NORTH);
        content.add(bottomPanel, BorderLayout.SOUTH);
    class ButtonListener implements ActionListener
    	public void actionPerformed(ActionEvent e)
    		for (int i=0 ; i<=9 ; i++)
    			if (e.getSource() == digits[i])
    				expr.setText( expr.getText() + ""+i );
    public static void main(String [] args)
    	new Calculator();
6 Years
Discussion Span
Last Post by coolsport04

I know this isn't correct, but what if I wanted something along the lines/format of this:

ArrayList<String> math = new ArrayList<String>();

for(int i=0, i<math.size(); i++)
	String solve = math.get(i);
	System input = new Scanner(System.in);
	String values = input.nextInt();
	char[] content = solve.toCharArray();
	String sorted = new String(content);

I want to somehow use an arrayList to get the input from the expr textfield and then sort the array so that when I loop through the operations are done in order.

change JTextField to the JFormattedTextField with number formatter, then change

expr.setText( expr.getText() + ""+i );


expr.setValue(((Number) expr.getValue()).doubleValue()+ i);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.