I am having a compile-time problem with a simple program I am writing. When I attempt to compile, I get the error "non-static variable this cannot be referenced from a static context". The error point to where I try to create a new instance of 'ConvertToOunce s' inside the main method. Any help would be greatly appreciated.
Here is my source code. -
/******************************************************************************\
-
|
-
|Try to make gui for both Projects 2a and 2b
-
|
-
|
-
\******************************************************************************/
-
-
import javax.swing.*;
-
import javax.swing.JOptionPane.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
-
public class WeightConverter extends JPanel{
-
-
//main method will instantiate and display GUI
-
-
public static void main(String[] args){
-
-
JFrame frame = new JFrame("Weight Converter");
-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
-
JTabbedPane pane = new JTabbedPane();
-
pane.addTab ("Convert To Ounces", new ConvertToOuncesPane());
-
-
}
-
//ConvertToOuncesPane class will create the pane for converting to ounces.
-
class ConvertToOuncesPane{
-
-
//Set up new component identifiers
-
JLabel tonsLabel, stonesLabel, poundsLabel, ouncesLabel, resultLabel;
-
JTextField tonsField, stonesField, poundsField, ouncesField;
-
JButton btnConvert, btnClear, btnExit;
-
String resultString = "Total Number of Ounces: ";
-
-
/*****************************************************************************\
-
|
-
|this method will be the primary constructor method for the class
-
|
-
\*****************************************************************************/
-
public ConvertToOuncesPane(){
-
-
//set up the labels for the input boxes
-
tonsLabel = new JLabel("Enter number of tons:");
-
stonesLabel = new JLabel("Enter number of stones:");
-
poundsLabel = new JLabel("Enter number of pounds:");
-
ouncesLabel = new JLabel("Enter number of pounds:");
-
resultLabel = new JLabel(resultString + "---");
-
-
//set up the input boxes
-
tonsField = new JTextField(10);
-
stonesField = new JTextField(10);
-
poundsField = new JTextField(10);
-
ouncesField = new JTextField(10);
-
-
//set up the convert button and add action listener
-
btnConvert = new JButton("Convert");
-
btnConvert.addActionListener (new ConvertToListener());
-
-
//set up the clear button and add action listener
-
btnClear = new JButton("Clear");
-
btnClear.addActionListener (new ClearToListener());
-
-
//set up the exit button and add action listener
-
btnExit = new JButton("Exit");
-
btnExit.addActionListener (new ExitToListener());
-
-
//add components to the pane
-
add (tonsLabel);
-
add (tonsField);
-
add (stonesLabel);
-
add (stonesField);
-
add (poundsLabel);
-
add (poundsField);
-
add (ouncesLabel);
-
add (ouncesField);
-
add (resultLabel);
-
add (btnConvert);
-
add (btnClear);
-
add (btnExit);
-
-
//set default values for textFields
-
tonsField.setText("0");
-
stonesField.setText("0");
-
poundsField.setText("0");
-
ouncesField.setText("0");
-
-
//set up the look of the pane
-
setBackground(Color.white);
-
setPreferredSize(new Dimension(200, 280));
-
-
-
-
}
-
-
/**********************************************************************\
-
|
-
|the ConvertToListener class is dedicated to the convert button
-
|on the Convert To Ounces Pane
-
|
-
\**********************************************************************/
-
private class ConvertToListener implements ActionListener{
-
-
public void actionPerformed(ActionEvent event){
-
-
//set up variables for the ConvertListener class
-
int tons = 0, stones = 0, pounds = 0, ounces = 0, totalOunces = 0;
-
final int CONVERT_TONS = 32000, CONVERT_STONES = 224, CONVERT_POUNDS = 16;
-
String errorMessage = "You must enter a number.";
-
boolean testField;
-
-
//collect, test, and parse integers from each textField
-
//test tonsField
-
testField = testInt(tonsField.getText());
-
-
if (testField == true)
-
tons = Integer.parseInt(tonsField.getText());
-
else
-
JOptionPane.showMessageDialog(null, errorMessage);
-
-
//test stonesField
-
testField = testInt(stonesField.getText());
-
-
if (testField == true)
-
stones = Integer.parseInt(stonesField.getText());
-
else
-
JOptionPane.showMessageDialog(null, errorMessage);
-
-
//test poundsField
-
testField = testInt(poundsField.getText());
-
-
if (testField == true)
-
pounds = Integer.parseInt(poundsField.getText());
-
else
-
JOptionPane.showMessageDialog(null, errorMessage);
-
-
//test ouncesField
-
testField = testInt(ouncesField.getText());
-
-
if (testField == true)
-
ounces = Integer.parseInt(ouncesField.getText());
-
else
-
JOptionPane.showMessageDialog(null, errorMessage);
-
-
//perform the conversion calculation
-
totalOunces = tons * CONVERT_TONS + stones * CONVERT_STONES + pounds * CONVERT_POUNDS + ounces;
-
-
//display the result
-
resultLabel.setText(resultString + totalOunces);
-
}
-
-
//set up a try-catch in the event a letter is entered into a field
-
boolean testInt(String test){
-
try{
-
Integer.parseInt(test);
-
}
-
catch(NumberFormatException exception){
-
return false;
-
}
-
return true;
-
}
-
-
-
-
-
}
-
/***************************************************************************\
-
|
-
|the ClearToListener class is dedicated to the Clear button on
-
|the Convert To Ounces Pane
-
|
-
\***************************************************************************/
-
private class ClearToListener implements ActionListener{
-
-
/****************************************************************\
-
|
-
|this method will restore the default zeros to all text fields
-
|
-
\****************************************************************/
-
public void actionPerformed(ActionEvent event){
-
//restore default values to textFields
-
tonsField.setText("0");
-
stonesField.setText("0");
-
poundsField.setText("0");
-
ouncesField.setText("0");
-
-
}
-
}
-
/*******************************************************************************\
-
|
-
|the ExitToListener class is dedicated to the Exit button on the
-
|Convert To Ounces Pane
-
|
-
\*******************************************************************************/
-
-
private class ExitToListener implements ActionListener{
-
/***********************************************************\
-
|
-
|this method will end the program when the user
-
|clicks the exit button
-
|
-
\***********************************************************/
-
public void actionPerformed(ActionEvent event){
-
System.exit(0);
-
}
-
}
-
-
}
-
-
-
-
-
}
-
4 2216
That can't be the error you are getting now with that code. You haven't used this at all in that main method. Your call to addTab however is incorrect. The closest overload for that method takes a String and a Component. You are passing a string as the first parameter (correct) but passing a ConvertToOunces Pane object which is not a Component as the second parameter and that won't compile.
JosAH 11,448
Recognized Expert MVP
That ConvertToOunces Pane is an inner class. Objects of inner class types are
tied to an object of the outer class. You don't have an object of the outer class
so you can't instantiate (new) an object of that inner class in your main method
which runs in a static context (read: no object(s) in scope).
You either make an object of the outer class or make that inner class a static
class, a so called static nested class.
kind regards,
Jos
Thank you for the help so far. I changed my inner class to be a static class and all seems well, except that in my main method where i try 'pane.addTab' i get a cannot find symbol error. My text book shows that i have the syntax right, but I still can't get it to work.
Here is my revised source code: -
import javax.swing.*;
-
import javax.swing.JOptionPane.*;
-
import javax.swing.JTabbedPane.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
-
public class WeightConverter extends JPanel{
-
-
//main method will instantiate and display GUI
-
-
public static void main(String[] args){
-
-
JFrame frame = new JFrame("Weight Converter");
-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
-
JTabbedPane pane = new JTabbedPane();
-
ConvertToOuncesPane panel_1 = new ConvertToOuncesPane();
-
-
pane.addTab("Convert", panel_1);
-
-
frame.getContentPane().add(pane);
-
frame.pack();
-
frame.setVisible(true);
-
}
-
//ConvertToOuncesPane class will create the pane for converting to ounces.
-
static class ConvertToOuncesPane{
-
-
//Set up new component identifiers
-
JLabel tonsLabel, stonesLabel, poundsLabel, ouncesLabel, resultLabel;
-
JTextField tonsField, stonesField, poundsField, ouncesField;
-
JButton btnConvert, btnClear, btnExit;
-
String resultString = "Total Number of Ounces: ";
-
-
/*****************************************************************************\
-
|
-
|this method will be the primary constructor method for the class
-
|
-
\*****************************************************************************/
-
public ConvertToOuncesPane(){
-
-
//set up the labels for the input boxes
-
tonsLabel = new JLabel("Enter number of tons:");
-
stonesLabel = new JLabel("Enter number of stones:");
-
poundsLabel = new JLabel("Enter number of pounds:");
-
ouncesLabel = new JLabel("Enter number of pounds:");
-
resultLabel = new JLabel(resultString + "---");
-
-
//set up the input boxes
-
tonsField = new JTextField(10);
-
stonesField = new JTextField(10);
-
poundsField = new JTextField(10);
-
ouncesField = new JTextField(10);
-
-
//set up the convert button and add action listener
-
btnConvert = new JButton("Convert");
-
btnConvert.addActionListener (new ConvertToListener());
-
-
//set up the clear button and add action listener
-
btnClear = new JButton("Clear");
-
btnClear.addActionListener (new ClearToListener());
-
-
//set up the exit button and add action listener
-
btnExit = new JButton("Exit");
-
btnExit.addActionListener (new ExitToListener());
-
-
//add components to the pane
-
JFrame frame = new JFrame();
-
frame.add (tonsLabel);
-
frame.add (tonsField);
-
frame.add (stonesLabel);
-
frame.add (stonesField);
-
frame.add (poundsLabel);
-
frame.add (poundsField);
-
frame.add (ouncesLabel);
-
frame.add (ouncesField);
-
frame.add (resultLabel);
-
frame.add (btnConvert);
-
frame.add (btnClear);
-
frame.add (btnExit);
-
-
//set default values for textFields
-
tonsField.setText("0");
-
stonesField.setText("0");
-
poundsField.setText("0");
-
ouncesField.setText("0");
-
-
//set up the look of the pane
-
frame.setBackground(Color.white);
-
frame.setPreferredSize(new Dimension(200, 280));
-
-
-
-
}
-
-
/**********************************************************************\
-
|
-
|the ConvertToListener class is dedicated to the convert button
-
|on the Convert To Ounces Pane
-
|
-
\**********************************************************************/
-
private class ConvertToListener implements ActionListener{
-
-
public void actionPerformed(ActionEvent event){
-
-
//set up variables for the ConvertListener class
-
int tons = 0, stones = 0, pounds = 0, ounces = 0, totalOunces = 0;
-
final int CONVERT_TONS = 32000, CONVERT_STONES = 224, CONVERT_POUNDS = 16;
-
String errorMessage = "You must enter a number.";
-
boolean testField;
-
-
//collect, test, and parse integers from each textField
-
//test tonsField
-
testField = testInt(tonsField.getText());
-
-
if (testField == true)
-
tons = Integer.parseInt(tonsField.getText());
-
else
-
JOptionPane.showMessageDialog(null, errorMessage);
-
-
//test stonesField
-
testField = testInt(stonesField.getText());
-
-
if (testField == true)
-
stones = Integer.parseInt(stonesField.getText());
-
else
-
JOptionPane.showMessageDialog(null, errorMessage);
-
-
//test poundsField
-
testField = testInt(poundsField.getText());
-
-
if (testField == true)
-
pounds = Integer.parseInt(poundsField.getText());
-
else
-
JOptionPane.showMessageDialog(null, errorMessage);
-
-
//test ouncesField
-
testField = testInt(ouncesField.getText());
-
-
if (testField == true)
-
ounces = Integer.parseInt(ouncesField.getText());
-
else
-
JOptionPane.showMessageDialog(null, errorMessage);
-
-
//perform the conversion calculation
-
totalOunces = tons * CONVERT_TONS + stones * CONVERT_STONES + pounds * CONVERT_POUNDS + ounces;
-
-
//display the result
-
resultLabel.setText(resultString + totalOunces);
-
}
-
-
//set up a try-catch in the event a letter is entered into a field
-
boolean testInt(String test){
-
try{
-
Integer.parseInt(test);
-
}
-
catch(NumberFormatException exception){
-
return false;
-
}
-
return true;
-
}
-
-
-
-
-
}
-
/***************************************************************************\
-
|
-
|the ClearToListener class is dedicated to the Clear button on
-
|the Convert To Ounces Pane
-
|
-
\***************************************************************************/
-
private class ClearToListener implements ActionListener{
-
-
/****************************************************************\
-
|
-
|this method will restore the default zeros to all text fields
-
|
-
\****************************************************************/
-
public void actionPerformed(ActionEvent event){
-
//restore default values to textFields
-
tonsField.setText("0");
-
stonesField.setText("0");
-
poundsField.setText("0");
-
ouncesField.setText("0");
-
-
}
-
}
-
/*******************************************************************************\
-
|
-
|the ExitToListener class is dedicated to the Exit button on the
-
|Convert To Ounces Pane
-
|
-
\*******************************************************************************/
-
-
private class ExitToListener implements ActionListener{
-
/***********************************************************\
-
|
-
|this method will end the program when the user
-
|clicks the exit button
-
|
-
\***********************************************************/
-
public void actionPerformed(ActionEvent event){
-
System.exit(0);
-
}
-
}
-
-
}
-
-
-
-
-
}
-
JosAH 11,448
Recognized Expert MVP
Read the API documentation: the addTab method adds a tab which must be a
Component. Your ConvertToOunces Pane class isn't a Component.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: D. Roshani |
last post by:
Hello !
I wonder if any one can help me to create a cosomize sorting order (as Macro or added small program
in c++ or c# which does this work) in a Access Database contaning one table only words encoded in
ISO-8859-1
A a B b C c D d E e É é F f G g H h I i Í í J j
1 2 3 4 5 6 7 8 9 10 11 12
Jh jh K k L l ll M m N n O o P p Q q R r rr S s...
|
by: wukexin |
last post by:
Help me, good men. I find mang books that introduce bit "mang header
files",they talk too bit,in fact it is my too fool, I don't learn it, I have
do a test program, but I have no correct doing result in any way. Who can
help me, I thank you very very much.
list.cpp(main program)
//--------------------------------------------------------------------------
-
#pragma hdrstop
#pragma argsused
|
by: Mahmood Ahmad |
last post by:
Hello,
I have written a program that reads three types of records, validates
them acording to certain requirements and writes the valid records
into a binary file. The invalid records are supposed to be reported on
the printer but I have commented those pieces of code and have got
those records printed on the screen. I am using Microsoft Visual C++
6.0 on Microsoft XP (Home) platform.
I am facing some problems in getting desire...
|
by: Drewdog |
last post by:
I am getting some error messages which I can't figure out their
meaning. I have the code setup, I think it's correct but it doesn't
work. My goal is to get this program to read from a data file and
basically put it into an output file that I can access as a test file.
Here is my header file:
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
|
by: Protoman |
last post by:
Hi!!! I need some help on a project I'm that calculates leap years; I'm
getting errors that I have no idea what they mean; here's the code:
#include <iostream>
#include <cstdlib>
using namespace std;
class Year
{
public:
| |
by: Naren |
last post by:
Hello All,
Can any one help me in this file read problem.
#include <stdio.h>
int main()
{
|
by: Zach |
last post by:
Hello,
Please forgive if this is not the most appropriate newsgroup for this
question. Unfortunately I didn't find a newsgroup specific to regular
expressions.
I have the following regular expression.
^(.+?) uses (?!a spoon)\.$
|
by: Amali |
last post by:
I'm newdie in c programming. this is my first project in programming.
I have to write a program for a airline reservation. this is what i
have done yet. but when it runs it shows the number of seats as 0 and
the flight no. is also repeating. If any can tell why is this please
help me.
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
|
by: tomwolfstein |
last post by:
Hi.
I am trying to write a wrapper for the standard VC1 decoder, and I need to resolve a "TypeLoadException"
The decoder comes an an executable which I've turned into a .dll. This decoder has about a ton of structures, most of the containing other structures, arrays of structures, and unions of structures.
I need help converting the following to managed code:
The unmanaged structure is this:
typedef struct
{
vc1_eBlkType eBlkType; /**...
|
by: Kira Yamato |
last post by:
I've posted this in another thread, but I suppose I should've started a
new thread for it instead.
I cannot get the following short program to compile under g++:
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |