Image

Imagemr_bucket wrote in Imagejava_dev

blasted Swing apps....

My first Swing application was going pretty darn well...but there always has to be that one blasted, elusive little error. This time, it was in the ActionListener for the one button I have on my GUI.
I left in the surrounding code, because sometimes the compiler seems to pick up an error, but point out some normal code fragment.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TriCalc extends JFrame
					 implements ActionListener,
					 			WindowListener{
	//Declare your fifteen trillion window objects...
	private JLabel ALabel;
	private JTextField AInputField;
	private JTextField AResultField;
	
	private JLabel BLabel;
	private JTextField BInputField;
	private JTextField BResultField;
	
	private JLabel CLabel;
	private JTextField CInputField;
	private JTextField CResultField;
	
	private JLabel aLabel;
	private JTextField aInputField;
	private JTextField aResultField;
	
	private JLabel bLabel;
	private JTextField bInputField;
	private JTextField bResultField;
	
	private JLabel cLabel;
	private JTextField cInputField;
	private JTextField cResultField;
	
	private JButton solveButton;
	public TriCalc(){
		//then instantiate them in the constructor.
		ALabel = new JLabel ("Angle A");
		AInputField = new JTextField ("0.0",6);
		AResultField = new JTextField ("0.0",6);
		
		BLabel = new JLabel ("Angle B");
		BInputField = new JTextField ("0.0",6);
		BResultField = new JTextField ("0.0",6);
		
		CLabel = new JLabel ("Angle C");
		CInputField = new JTextField ("0.0",6);
		CResultField = new JTextField ("0.0",6);
		
		aLabel = new JLabel ("Side a");
		aInputField = new JTextField ("0.0",6);
		aResultField = new JTextField ("0.0",6);
		
		bLabel = new JLabel ("Side b");
		bInputField = new JTextField ("0.0",6);
		bResultField = new JTextField ("0.0",6);
		
		cLabel = new JLabel ("Side c");
		cInputField = new JTextField ("0.0",6);
		cResultField = new JTextField ("0.0",6);
		
		this.solveButton = new JButton ("Solve the Triangle");
		//Then instantiate the layout.
		FlowLayout layout = new FlowLayout();
		//Then get the frame's content pane, and set its layout.
		Container mainWindow = getContentPane();
		mainWindow.setLayout(layout);
		//Next, add the window objects according to how you 
		//want them placed in the layout.
		mainWindow.add(ALabel);
		mainWindow.add(AInputField);
		mainWindow.add(AResultField);
		
		mainWindow.add(BLabel);
		mainWindow.add(BInputField);
		mainWindow.add(BResultField);
		
		mainWindow.add(CLabel);
		mainWindow.add(CInputField);
		mainWindow.add(CResultField);
		
		mainWindow.add(aLabel);
		mainWindow.add(aInputField);
		mainWindow.add(aResultField);
		
		mainWindow.add(bLabel);
		mainWindow.add(bInputField);
		mainWindow.add(bResultField);
		
		mainWindow.add(cLabel);
		mainWindow.add(cInputField);
		mainWindow.add(cResultField);
		
                mainWindow.add(solveButton);

                //Listeners....
		this.solveButton.addActionListener(new solveButtonListener (this));
		this.addWindowListener(new GenericWindowListener());
		
	}
	public void actionPerformed(ActionEvent e){
		String str;
		double A, B, C, a, b, c;
		JButton btn= (JButton)e.getSource();
		if (btn == solveButton)	{
		
		String str = AInputField.getText().trim();
		double A = (new Double(str)).doubleValue();
		str = BInputField.getText().trim();
		double B = (new Double(str)).doubleValue();
		str = CInputField.getText().trim();
		double C = (new Double(str)).doubleValue();
		str = aInputField.getText().trim();
		double a = (new Double(str)).doubleValue();
		str = bInputField.getText().trim();
		double b = (new Double(str)).doubleValue();
		str = cInputField.getText().trim();
		double c = (new Double(str)).doubleValue();

		Triangle tri = new Triangle();
		tri.solveTriangle(A, B, C, a, b, c);

		AResultField.setText(""+tri.getA());
		BResultField.setText(""+tri.getB());
		CResultField.setText(""+tri.getC());
		aResultField.setText(""+tri.geta());
		bResultField.setText(""+tri.getb());
		cResultField.setText(""+tri.getc());
		}
	}
		public void displayAnswers(){
		String str = AInputField.getText().trim();
		double A = (new Double(str)).doubleValue();
		str = BInputField.getText().trim();
		double B = (new Double(str)).doubleValue();
		str = CInputField.getText().trim();
		double C = (new Double(str)).doubleValue();
		str = aInputField.getText().trim();
		double a = (new Double(str)).doubleValue();
		str = bInputField.getText().trim();
		double b = (new Double(str)).doubleValue();
		str = cInputField.getText().trim();
		double c = (new Double(str)).doubleValue();

		Triangle tri = new Triangle();
		tri.solveTriangle(A, B, C, a, b, c);
		//Finally, the method puts the results up on the GUI.
		AResultField.setText(""+tri.getA());
		BResultField.setText(""+tri.getB());
		CResultField.setText(""+tri.getC());
		aResultField.setText(""+tri.geta());
		bResultField.setText(""+tri.getb());
		cResultField.setText(""+tri.getc());
	}
		public void windowClosing(WindowEvent e){
			System.exit(0);
		}
		public void windowActivated(WindowEvent e){}
		public void windowClosed(WindowEvent e){}
		public void WindowDeactivated(WindowEvent e){}
		public void windowDeiconified(WindowEvent e){}
		public void windowIconified(WindowEvent e){}
		public void windowOpened(WindowEvent e){}
	//aha!!  a main method!
	public static void main(String args[]){
		JFrame frm = new TriCalc();
		frm.setSize(300,500);
		frm.setVisible(true);
	}
}

public class solveButtonListener implements ActionListener{
	private TriCalc theGUI;
	solveButton.addActionListener(new solveButtonListener(this));
	public solveButtonListener(TriCalc gui){
		theGUI = gui;
	}
	public void actionPerformed(ActionEvent e){
		theGUI.displayResults();
	}
}

public class GenericWindowListener extends WindowAdapter{
	public void windowClosing (WindowEvent e){
		System.exit(0);
	}
}

There's also the puzzling word this in the same line. That could have something to do with it. I'm not sure that word belongs there--it implied it had to be replaced with something in the book I'm learning from, but the book was very unclear.

Suggestions?