Sign in to follow this  
Hammonjj

Swing Program Problem

Recommended Posts

Hammonjj    122
I'm trying to right my first simple swing app and it's gone well except for the end. I get all sorts of errors at runtime. All I'm trying to do is take two numbers and add them together. I'm sure it's something minor, but I just can seem to figure it out. I would greatly appreciate any help. Oh, please forgive the poor variable name; this was meant to be a simple, quick and dirty app to test my understanding of event listeners. Thanks James
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Simple_Claculator {
	JTextField inputN1;
	JTextField inputN2;
	JLabel totalOutput;
/**********Main Program************/	
    public static void main (String args[]) {
		Simple_Claculator Calc1 = new Simple_Claculator();
		Calc1.buildGUI();
    }

/**********Build GUI*************/
	void buildGUI() {
		JLabel N1 = new JLabel("First Number:");
		JLabel N2 = new JLabel("Second Number:");
		JLabel total = new JLabel("Total:");
		totalOutput = new JLabel("0.00");
		JTextField inputN1 = new JTextField(5);
		JTextField inputN2 = new JTextField(5);
		JButton add = new JButton("Add");
		JFrame mainFrame = new JFrame();
		JPanel leftside = new JPanel();
		JPanel rightside = new JPanel();

		add.addActionListener(new calculate());
		
		leftside.setLayout(new BoxLayout(leftside, BoxLayout.Y_AXIS));
		leftside.add(N1);
		leftside.add(N2);
		leftside.add(total);
		
		rightside.setLayout(new BoxLayout(rightside, BoxLayout.Y_AXIS));
		rightside.add(inputN1);
		rightside.add(inputN2);
		rightside.add(totalOutput);
		rightside.add(add);
		
		mainFrame.getContentPane().add(BorderLayout.EAST, rightside);
		mainFrame.getContentPane().add(BorderLayout.CENTER, leftside);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setSize(300,110);
		mainFrame.setVisible(true);
		}
	
/*************Inner Listener Class*********/
		class calculate implements ActionListener{
			public void actionPerformed(ActionEvent event) {
				String number1 = inputN1.getText();
				String number2 = inputN2.getText();
				
				double number1Double = Double.parseDouble(number1);
				double number2Double = Double.parseDouble(number2);
				
				double totalN1N2 = number1Double + number2Double;
				
				String totalString = Double.toString(totalN1N2);
				totalOutput.setText(totalString);
				}
		}
}

[Edited by - Hammonjj on October 12, 2007 10:03:20 PM]

Share this post


Link to post
Share on other sites
Wyrframe    2426
The errors you're getting are Null-pointer exceptions, right? Your mistake; you multiply declared some variables. Comments in the source are mine. Read 'em.


import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Simple_Claculator {
// These belong to the class.
JTextField inputN1;
JTextField inputN2;
JLabel totalOutput;

public static void main (String args[]) {
Simple_Claculator Calc1 = new Simple_Claculator();
Calc1.buildGUI();
}


void buildGUI() {
JLabel N1 = new JLabel("First Number:");
JLabel N2 = new JLabel("Second Number:");
JLabel total = new JLabel("Total:");
totalOutput = new JLabel("0.00");

// These belong to the method! It doesn't change the
// class's members, just these local temporaries!
// JTextField inputN1 = new JTextField(5);
// JTextField inputN2 = new JTextField(5);
inputN1 = new JTextField(5);
inputN2 = new JTextField(5);

JButton add = new JButton("Add");
JFrame mainFrame = new JFrame();
JPanel leftside = new JPanel();
JPanel rightside = new JPanel();

add.addActionListener(new calculate());

leftside.setLayout(new BoxLayout(leftside, BoxLayout.Y_AXIS));
leftside.add(N1);
leftside.add(N2);
leftside.add(total);

rightside.setLayout(new BoxLayout(rightside, BoxLayout.Y_AXIS));
rightside.add(inputN1);
rightside.add(inputN2);
rightside.add(totalOutput);
rightside.add(add);

mainFrame.getContentPane().add(BorderLayout.EAST, rightside);
mainFrame.getContentPane().add(BorderLayout.CENTER, leftside);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(300,110);
mainFrame.setVisible(true);
}

class calculate implements ActionListener{
public void actionPerformed(ActionEvent event) {
// This refers to the class's inputN1, inputN2.
String number1 = inputN1.getText();
String number2 = inputN2.getText();

double number1Double = Double.parseDouble(number1);
double number2Double = Double.parseDouble(number2);

double totalN1N2 = number1Double + number2Double;

String totalString = Double.toString(totalN1N2);
totalOutput.setText(totalString);
}
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this