Results 21 to 40 of 55
- 04-27-2012, 11:30 AM #21
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: Can't call variable from other class
Im using NetBeans IDE now because of the Designer helps, and I've got 2 classes:
- 1 Class that holds a Form and another that holds another Form. The problem is, I want to pass a variable from the first class to the second class, and I don't know how to do it.
So, the first class is called : "Champs1.java" and the second one: "CombateFrame.java"
In both of them I've got a constructor, and I thought I should pass the variable the same way I did with the other classes. Something like:
I would be putting this code in the Champs1 Class, in a button action listener, p.e. But it don't seem to be working...Java Code:CombateFrame.CombateFrame(String X);
- 04-27-2012, 12:21 PM #22
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: Can't call variable from other class
When I try to do this:
in the CombateFrame it gives a strange error: "constructor Champs1 in class Champs1 cannot be applied to given types"... The hint is to create a constructor but I already have one...Java Code:Champs1 Champs1Frame = new Champs1();
- 04-27-2012, 12:31 PM #23
Re: Can't call variable from other class
What does the definition of the Champs1 class look like?
If you don't understand my response, don't ignore it, ask a question.
- 04-27-2012, 12:35 PM #24
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: Can't call variable from other class
Java Code:public class Champs1 extends javax.swing.JFrame implements ActionListener { // VARIABLES public Champs1(Main mainFrame) { //Constructor where I have all the buttons action listeners, values given to variables, etc... }
- 04-27-2012, 12:37 PM #25
Re: Can't call variable from other class
Where is the constructor this statement is asking for?
Champs1 Champs1Frame = new Champs1(); //<<<<<<< NO ARGSIf you don't understand my response, don't ignore it, ask a question.
- 04-27-2012, 12:40 PM #26
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: Can't call variable from other class
Ok I tried to create a "second constructor(?)" in the class Champs1:
Am I doing right? What Arguments should I be calling?Java Code:Champs1() { }
- 04-27-2012, 12:52 PM #27
Re: Can't call variable from other class
That constructor does not take any arguments. It will have to assign default values to the class variables.
If you don't understand my response, don't ignore it, ask a question.
- 05-01-2012, 06:01 PM #28
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: Can't call variable from other class
But I've got this:
How I can pass the variables assigned by the button, like Champ1, attack1, etc to the constructor Champ1() { } ? It may look like a noob question, but I don't know how to do it. :/Java Code:public class Champs1 extends javax.swing.JFrame implements ActionListener { //Variavéis Main champs1Frame; ChampsList ChampsList = new ChampsList(); Combate Combate = new Combate(); CombateFrame CombateFrame; public static String Champ1, Champ2; public double HP1, HP2, attack1, attack2, defense1, defense2; public int champ1Ref, champ2Ref; private int player = 1; Champs1() { } public Champs1(Main mainFrame) { mainFrame = champs1Frame; CombateFrame = new CombateFrame(this); initComponents(); //Desenhar frame // Ver se todos escolheram as personagens Thread checkThread = new Thread(new Runnable() { public void run() { while (player != 0) { if (player == 3) { //Podemos acrescentar aqui os outros botões todos para serem desactivados bContinue.setEnabled(true); } } } }); checkThread.start(); /* * Temos de passar todos os stats para a class de combate que começo a * achar que a sua única função vai ser fazer check à vitória xd Nesta * classe vamos declarar as personagens basicamente. Eu bem disse que * iamos ter de reescrever tudo isto, mas n é nada que dê demasiado * trabalho. :/ */ bAntonio.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ChampsList.antonio(); bAntonio.setEnabled(false); if (player == 1) { Champ1 = ChampsList.Champ; attack1 = ChampsList.Attack; defense1 = ChampsList.Defense; HP1 = ChampsList.HP; champ1Ref = ChampsList.champRef; jLabel5.setText(Champ1); }
- 05-01-2012, 06:05 PM #29
Re: Can't call variable from other class
To pass args to a constructor (or method) put them in the ()s.
AClass aCls = new AClass(the, args, go, here);
The constructor must be coded to accept that number and type of argument.If you don't understand my response, don't ignore it, ask a question.
- 05-01-2012, 09:53 PM #30
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: Can't call variable from other class
Java Code:public class Champs1 extends javax.swing.JFrame implements ActionListener { //Variavéis Main champs1Frame; ChampsList ChampsList = new ChampsList(); Combate Combate = new Combate(); //Champs1 Champs1Obj = new Champs1(Champ1); CombateFrame CombateFrame; public String Champ1, Champ2; public double HP1, HP2, attack1, attack2, defense1, defense2; public int champ1Ref, champ2Ref; private int player = 1; public Champs1(Main mainFrame) { //BUTTONS, etc. }Sorry if my questions are getting a bit annoying. :/Java Code:public class CombateFrame extends javax.swing.JFrame { Champs1 Champs1Frame = new Champs1(); // What's wrong with this line? It gives me an error: "Constructor Champs1 in class Champs1 cannot be applied to given types
- 05-01-2012, 10:00 PM #31
Re: Can't call variable from other class
Were there error messages?
If you don't understand my response, don't ignore it, ask a question.
- 05-01-2012, 10:05 PM #32
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
- 05-01-2012, 10:11 PM #33
Re: Can't call variable from other class
What was the error message?
If you don't understand my response, don't ignore it, ask a question.
- 05-01-2012, 10:12 PM #34
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
- 05-01-2012, 10:15 PM #35
Re: Can't call variable from other class
Please post the full text of the error message.
Is there a constructor that has exactly matching arguments as in the new statement? Number of args and type of argIf you don't understand my response, don't ignore it, ask a question.
- 05-01-2012, 10:25 PM #36
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: Can't call variable from other class
Constructor Champs1 in class Champs1 cannot be applied to given types;
required: Main
found: no arguments
reason: actual and formal argument lists differ in length
I only got the:
constructor in the Champs1 class. If I create a new one it messes up with other things. And if I try to call the Champs1 constructor the frame "calling" stops working...Java Code:public Champs1 (Main mainFrame) { }
- 05-01-2012, 10:27 PM #37
Re: Can't call variable from other class
If that is the only constructor, you MUST call it with one arg of type Main. A type can be the name of a class or a primitive like int.
Java Code:Main aMainRef = .... // somewhere define a variable of type Main and give it a value ... = new Champs1(aMainRef); // call the constructor with the correct type
Last edited by Norm; 05-01-2012 at 10:30 PM.
If you don't understand my response, don't ignore it, ask a question.
- 05-01-2012, 10:46 PM #38
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: Can't call variable from other class
But why does he need to work with the Main class if I don't need anything from there? Because I changed the Main class name to MainMenu, just to avoid confusions and now it requires an arg of the type MainMenu...
- 05-01-2012, 11:12 PM #39
Re: Can't call variable from other class
If the Champs1 class does not need a reference to a Main object, add a constructor to the Champs1 class that has no arguments
or rewrite the current constructor and remove its argsIf you don't understand my response, don't ignore it, ask a question.
- 05-03-2012, 01:43 PM #40
Member
- Join Date
- Apr 2012
- Posts
- 30
- Rep Power
- 0
Re: Can't call variable from other class
But I need the args to call the frame...
Java Code:public class Champs1 extends javax.swing.JFrame implements ActionListener { //Variavéis MainMenu champs1Frame; ChampsList ChampsList = new ChampsList(); Combate Combate = new Combate(); //Champs1 Champs1Obj = new Champs1(Champ1); CombateFrame CombateFrame; public String Champ1, Champ2; public double HP1, HP2, attack1, attack2, defense1, defense2; public int champ1Ref, champ2Ref; private int player = 1; public Champs1(MainMenu mainFrame) { mainFrame = champs1Frame; CombateFrame = new CombateFrame(this); initComponents(); //Desenhar frame bContinue.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dispose(); CombateFrame.setVisible(true); //Combate.combate(Champ1, player); } }); }I just pasted the essencial...Java Code:public class CombateFrame extends javax.swing.JFrame { Champs1 CombateFrame; MainMenu mainRef; String champ1Name; //Champs1 Champs1Obj = new Champs1(Champ1); /** * Creates new form CombateFrame */ public CombateFrame(Champs1 CombateFrame_) { //champ1Name = Champs1Frame.Champ1; CombateFrame_ = CombateFrame; initComponents(); lChamp1Name.setText(champ1Name); }
Thanks.
PS. As you can see I need the args in the constructors, so I can call the frames I need. I can't see another way :/
Similar Threads
-
Call a public string variable
By onstock in forum New To JavaReplies: 3Last Post: 11-23-2011, 12:25 PM -
Eclipse Compile Error: Call validateValue(Class<T>, String, Object, Class<?>...)
By Tomshi in forum EclipseReplies: 0Last Post: 03-27-2011, 05:49 AM -
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM -
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
How can I call abstract class methods from another class
By srinivas2828 in forum New To JavaReplies: 13Last Post: 03-12-2010, 02:33 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks