1.11M Members

Button getting "Cannot be resolved" errors

 
0
 

Hello!
I'm currently working on a sort of simple imperial-to-metric converter application for myself, but I seem to have hit a snag. I've got the UI built, and the logic for one of the calculations built, but the button to make it all happen is giving me a problem.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
	 
	public class MetricConverterApp extends JFrame implements ActionListener
	{
		public MetricConverterApp() 
	    {
			
			double kilogram = 0.45359;
			
	         //Title section
	        setTitle("Metric Converter"); 
	        setSize(300,150);
	        setVisible(true);
	        setResizable(false);
	        
	        JTabbedPane jtp = new JTabbedPane();
	        
	        //Builds template of the window
	       getContentPane().add(jtp);
	 
	     //creating an instance of the menu bar for across the program
			MenuBar mnuBar = new MenuBar();
			setMenuBar(mnuBar);
			
			//building and filling the program's File menu
			Menu mnuFile = new Menu("File", true);
			mnuBar.add(mnuFile);
				MenuItem mnuFileExit = new MenuItem("Exit");
				mnuFile.add(mnuFileExit);
				
				
			//building and filling the program's About menu
			Menu mnuAbout = new Menu("About", true);
			mnuBar.add(mnuAbout);
				MenuItem mnuAboutMetricConverter = new MenuItem("About Metric Converter");
				mnuAbout.add(mnuAboutMetricConverter);
				
			//adding ActionListener to each of the menu items
			mnuFileExit.addActionListener(this);
			mnuAboutMetricConverter.addActionListener(this);
			
			//give each menu item an ActionCommand
			mnuFileExit.setActionCommand("Exit");
			mnuAboutMetricConverter.setActionCommand("About");
			
			
	       JPanel jp1 = new JPanel();//Builds first tab
	       jp1.setLayout(new BorderLayout());//Sets up layout of first tab
	       JPanel jp2 = new JPanel();//Builds second tab
	       jp2.setLayout(new BorderLayout());//Sets up layout of second tab
	       JPanel jp3 = new JPanel();//Builds third tab
	       jp3.setLayout(new BorderLayout());//sets up layout of second tab
	       

	       //Builds prompt for first tab
	       JLabel weightPrompt = new JLabel();
	       weightPrompt.setText("Enter the weight in pounds");
	       jp1.add(weightPrompt, BorderLayout.NORTH);
	       
	       //Builds text input for first tab
	       JTextField weightInput = new JTextField();
	       jp1.add(weightInput, BorderLayout.CENTER);
	       
	       //Builds button for first tab
	       JButton metWeight = new JButton("Convert to kilograms");
	       jp1.add(metWeight, BorderLayout.EAST);
	       metWeight.addActionListener(this);
	       
	       
	       //Builds text output for first tab
	       JTextField weightOutput = new JTextField();
	       jp1.add(weightOutput, BorderLayout.SOUTH);
	       
	       
	       //Builds prompt for second tab
	       JLabel lengthPrompt = new JLabel();
	       lengthPrompt.setText("Enter the length in feet");
	       jp2.add(lengthPrompt, BorderLayout.NORTH);
	       
	       //Builds text input for first tab
	       JTextField lengthInput = new JTextField("");
	       jp2.add(lengthInput, BorderLayout.CENTER);
	       
	       //Builds button for second tab
	       JButton metLength = new JButton("Convert to meters");
	       jp2.add(metLength, BorderLayout.EAST);
	       
	       //Builds text output for second tab
	       JTextField lengthOutput = new JTextField();
	       jp2.add(lengthOutput, BorderLayout.SOUTH);
	       
	       
	       //Build prompt for third tab
	       JLabel tempPrompt = new JLabel();
	       tempPrompt.setText("Enter the temperature in farenheit");
	       jp3.add(tempPrompt, BorderLayout.NORTH);
	       
	       //Builds text input for third tab
	       JTextField tempInput = new JTextField("");
	       jp3.add(tempInput, BorderLayout.CENTER);
	       
	       //Builds button for third tab
	       JButton metTemp = new JButton("Convert to celcius");
	       jp3.add(metTemp, BorderLayout.EAST);
	       
	       //Builds text output for third tab
	       JTextField tempOutput = new JTextField();
	       jp3.add(tempOutput, BorderLayout.SOUTH);
	 
	       //Adds tabs to pane
	       jtp.addTab("Weight", jp1);
	       jtp.addTab("Length", jp2);
	       jtp.addTab("Temperature", jp3);
	    }
	     
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			//ActionListener for menu item clicks
       		String arg = e.getActionCommand();
       		if (arg == "Exit")
       			System.exit(0);
       		
       		if (arg == "About")
       		{
       			String message = "Metric Converter v1.0\nDesigned by:\nMe\nFebruary 19, 2011";
       			JOptionPane.showMessageDialog(null,message,"About Metric Converter", JOptionPane.INFORMATION_MESSAGE);
       		}
       		
       		if(e.getSource() == metWeight)
       		{
       		    double value = Double.parseDouble(weightInput.getText());
           		weightOutput.setText((value * kilogram) + "");
       		}
		}

		//main method
	     public static void main (String []args)
	     {
	        MetricConverterApp tab = new MetricConverterApp();
	        
	    }
}

The problem lies in lines 137 - 141. the metWeight and the kilogram variables are kicking back the error of "Cannot be resolved to a variable", and then the weightInput and weightOutput are showing an error of just "Cannot be resolved". When I run the program, I enter in the weight in pounds, then click the button to calculate it to kilograms, but nothing happens, and I think it's somewhere in that bit of code. I was wondering if anyone had any idea what could be causing this? I'd really appreciate any help.

 
0
 

welcome on this forum,

public class MetricConverterApp extends JFrame implements ActionListener{

JButton metWeight;

public MetricConverterApp(){

.
.
.
.
.
.

metWeight = new JButton("Convert to kilograms");
jp1.add(metWeight, BorderLayout.EAST);
metWeight.addActionListener(this);

and every JTextField (where you want to input Numeric value) should be JFormattedTextField

don't extends and implements in main Class

 
0
 

Ok, I added the metWeight, weightInput, weightOutput, and kilogram between the class and the constructor like so:

public class MetricConverterApp extends JFrame implements ActionListener
	{
		JButton metweight;
		JFormattedTextField weightInput;
		JTextField weightOutput;
		double kilogram = 0.45359;
		
		
		public MetricConverterApp() 
	    {

However, the metWeight is still giving me problems...the same error is present, and I've looked at several examples and I've got it right...it's just not working.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article