Results 1 to 13 of 13
- 04-14-2011, 11:13 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
Err: invalid method declaration; return type required
I dunno what i am doing wrong.... i know it saying i need to put in a return type, but im kinda new and i dont see where it is supposed to go or what it is supposed to be.
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Hw09_msj extends JFrame { private Container cont; private JRadioButton uCase; private JRadioButton lCase; private ButtonGroup buttonGroup; private JLabel label; public changeCase() { super("Case Changer"); cont = getContentPane(); cont.setLayout(new FlowLayout()); uCase = new JRadioButton("UPPERCASE", true); lCase = new JRadioButton("lowercase"); label = new JLabel("WATCH ME CHANGE CASE"); cont.add(uCase); cont.add(lCase); cont.add(label); buttonGroup = new ButtonGroup(); buttonGroup.add(uCase); buttonGroup.add(lCase); changeCase.RadioButtonHandler rbh = new changeCase.RadioButtonHandler(null); uCase.addItemListener(rbh); lCase.addItemListener(rbh); setSize(300, 200); setVisible(true); } public static void main(String[] args) { changeCase cc = new changeCase(); cc.setDefaultCloseOperation(3); } private class RadioButtonHandler implements ItemListener { private RadioButtonHandler() { } public void itemStateChanged(ItemEvent ie) { if (ie.getSource() == changeCase.uCase) changeCase.label.setText(changeCase.label.getText().toUpperCase()); else if (ie.getSource() == changeCase.lCase) changeCase.label.setText(changeCase.label.getText().toLowerCase()); } } }
-
The name of the constructor needs to be the same as the name of the class. Please check your tutorial for how to create constructors and classes for the details on this and other issues with creating classes and constructors.
- 04-14-2011, 11:26 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
I'm sorry would you be willing to copy and paste the part your talking about, please forgive my naivety
-
No, I want you to gain an understanding enough to fix it for yourself.
Do you understand what part above is the name of the class? Please post it for us.
Do you know what a constructor is? If so, what is the name of your constructor above?
If you don't understand these concepts, you absolutely must read the tutorials first to be able to understand our advice. I'm asking you to expend a little effort in solving this, but it is effort well spent and will help you in the future as you will have a deeper understanding of what you are doing.
- 04-14-2011, 11:43 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
My class is Hw09_msj and changeCase cc = new changeCase is my constructor, but what im having a problem with is my class HAS to be Hw09_msj and i tried changing my constructor to Hw09_msj cc = new Hw09_msj abd then making the changes through out my code, but that gave me even more errors....
-
Close, but "Hw09_msj cc = new Hw09_msj abd" is not the constructor. It is trying to create an instance of the class by calling the constructor.
Rather the constructor is in the your code posted above and is like a method, but without a return type... this line:
So to answer my question, your constructor name is "changeCase". What happens if you change this to the class name?Java Code:public changeCase()
- 04-14-2011, 11:57 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
i get 14 errors, so then i change ALL cases of changeCase to Hw09_msj and i get 7 errors...
Java Code:--------------------Configuration: <Default>-------------------- C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:32: cannot find symbol symbol : constructor RadioButtonHandler(<nulltype>) location: class Hw09_msj.RadioButtonHandler Hw09_msj.RadioButtonHandler rbh = new Hw09_msj.RadioButtonHandler(null); ^ C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:55: non-static variable uCase cannot be referenced from a static context if (ie.getSource() == Hw09_msj.uCase) ^ C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:56: non-static variable label cannot be referenced from a static context Hw09_msj.label.setText(Hw09_msj.label.getText().toUpperCase()); ^ C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:56: non-static variable label cannot be referenced from a static context Hw09_msj.label.setText(Hw09_msj.label.getText().toUpperCase()); ^ C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:57: non-static variable lCase cannot be referenced from a static context else if (ie.getSource() == Hw09_msj.lCase) ^ C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:58: non-static variable label cannot be referenced from a static context Hw09_msj.label.setText(Hw09_msj.label.getText().toLowerCase()); ^ C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:58: non-static variable label cannot be referenced from a static context Hw09_msj.label.setText(Hw09_msj.label.getText().toLowerCase()); ^ 7 errors Process completed.
-
- 04-15-2011, 12:03 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Hw09_msj extends JFrame { private Container cont; private JRadioButton uCase; private JRadioButton lCase; private ButtonGroup buttonGroup; private JLabel label; public Hw09_msj() { super("Case Changer"); cont = getContentPane(); cont.setLayout(new FlowLayout()); uCase = new JRadioButton("UPPERCASE", true); lCase = new JRadioButton("lowercase"); label = new JLabel("WATCH ME CHANGE CASE"); cont.add(uCase); cont.add(lCase); cont.add(label); buttonGroup = new ButtonGroup(); buttonGroup.add(uCase); buttonGroup.add(lCase); Hw09_msj.RadioButtonHandler rbh = new Hw09_msj.RadioButtonHandler(null); uCase.addItemListener(rbh); lCase.addItemListener(rbh); setSize(300, 200); setVisible(true); } public static void main(String[] args) { Hw09_msj cc = new Hw09_msj(); cc.setDefaultCloseOperation(3); } private class RadioButtonHandler implements ItemListener { private RadioButtonHandler() { } public void itemStateChanged(ItemEvent ie) { if (ie.getSource() == Hw09_msj.uCase) Hw09_msj.label.setText(Hw09_msj.label.getText().toUpperCase()); else if (ie.getSource() == Hw09_msj.lCase) Hw09_msj.label.setText(Hw09_msj.label.getText().toLowerCase()); } } }
-
OK, so you see that you've fixed your initial problem, right?
So now change:
Java Code:Hw09_msj.RadioButtonHandler rbh = new Hw09_msj.RadioButtonHandler(null);
to
Java Code:RadioButtonHandler rbh = new RadioButtonHandler();
For your other problems, get rid of the "Hw09_msj." stuck in front of variables and methods.
- 04-15-2011, 12:06 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Since you are using an inner class the inner class can access all of the class variables. You can change the label directly in the handler with just callig it label, no need to add the outer class in there. This also applies when creating an instance of the handler in the main class.
- 04-15-2011, 12:39 AM #12
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
This is what i have now
and im getting this errorJava Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Hw09_msj extends JFrame { private Container cont; private JRadioButton uCase; private JRadioButton lCase; private ButtonGroup buttonGroup; private JLabel label; public Hw09_msj() { super("Case Changer"); cont = getContentPane(); cont.setLayout(new FlowLayout()); uCase = new JRadioButton("UPPERCASE", true); lCase = new JRadioButton("lowercase"); label = new JLabel("WATCH ME CHANGE CASE"); cont.add(uCase); cont.add(lCase); cont.add(label); buttonGroup = new ButtonGroup(); buttonGroup.add(uCase); buttonGroup.add(lCase); RadioButtonHandler rbh = new RadioButtonHandler(null); uCase.addItemListener(rbh); lCase.addItemListener(rbh); setSize(300, 200); setVisible(true); } public static void main(String[] args) { Hw09_msj cc = new Hw09_msj(); cc.setDefaultCloseOperation(3); } private class RadioButtonHandler implements ItemListener { private RadioButtonHandler() { } public void itemStateChanged(ItemEvent ie) { if (ie.getSource() == uCase) label.setText(label.getText().toUpperCase()); else if (ie.getSource() == lCase) label.setText(label.getText().toLowerCase()); } } }
Java Code:--------------------Configuration: <Default>-------------------- C:\Users\Matt\Desktop\COP 2551\Hw09_msj.java:32: cannot find symbol symbol : constructor RadioButtonHandler(<nulltype>) location: class Hw09_msj.RadioButtonHandler RadioButtonHandler rbh = new RadioButtonHandler(null); ^ 1 error Process completed.
-
You passing a parameter into the RadioButtonHandler constructor, but if you look at the RadioButtonHandler you'll see that the constructor you've created for it takes no parameter -- so pass it none.
Similar Threads
-
JavaBean - invalid method declaration and return type required
By jprog in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 05-07-2011, 06:13 PM -
invalid method declaration; return type required
By XaviannNJ in forum JCreatorReplies: 7Last Post: 11-25-2010, 01:59 PM -
Error: invalid method declaration; return type required
By silvia in forum AWT / SwingReplies: 3Last Post: 06-05-2010, 08:05 PM -
invalid method declaration error
By bsarules in forum Java AppletsReplies: 7Last Post: 05-25-2010, 06:06 PM -
Error: invalid method declaration
By silvia in forum New To JavaReplies: 1Last Post: 07-27-2007, 12:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks