Results 1 to 20 of 34
Thread: Problem with java frames
- 09-12-2010, 05:06 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 29
- Rep Power
- 0
Problem with java frames
Hello, first of all i am sorry for the bad English.
Now i am writing a java program on ubuntu using 'gEdit Text Editor' and compile using terminal.
how can i connect 2 classes?
example:
This is the second class.Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class program1 extends JFrame implements ActionListener{ JButton b; public program1(){ this.setSize(150,140); this.setTitle("Java Program"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); Container c = getContentPane(); b = new JButton ("Click me to continue.."); b.setActionCommand("cl"); b.addActionListener(this); c.add(b, BorderLayout.NORTH); } public static void main (String args []) throws Exception{ new program1();}}
i want to create a program where a program1 class will apear with a command an then by presing that command program1 class will disappear and the program2 class will apear how can i do that?Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class program2 extends JFrame implements ActionListener{ JButton b; public program2(){ this.setSize(150,140); this.setTitle("Java Program 2"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); Container c = getContentPane(); b = new JButton ("Click me to exit.."); b.setActionCommand("exit"); b.addActionListener(this); c.add(b, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e){ if(e.getContentPane().equals("exit")){ System.exit(0);} } public static void main (String args []) throws Exception{ new program2();}}
with out IDE like netbeans etc.
Thank you in advance
- 09-12-2010, 05:29 PM #2
Frames appear and disappear by setting them visible (true or false)
For one class to "connect" to another class, you can use the new operator to create a new instance of the second class from the first class.
SecondClass secCls = new SecondClass();
- 09-12-2010, 05:33 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 29
- Rep Power
- 0
thank you for your reply..
the first class and the second class will be in the same file (program.java <= that is going to compile) or in 2 defferent *.java files?
edit: ooo thank you my friend!!!
working like a harm
The result is:
and another questionJava Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class program1 extends JFrame implements ActionListener{ JButton b; public program1(){ this.setSize(150,140); this.setTitle("Java Program"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); Container c = getContentPane(); b = new JButton ("Click me to continue.."); b.setActionCommand("cl"); b.addActionListener(this); c.add(b, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("cl")){ program2 s = new program2(); s.setVisible(true);}} public static void main (String args []) throws Exception{ new program1();}} class program2 extends JFrame implements ActionListener{ JButton b; public program2(){ this.setSize(150,140); this.setTitle("Java Program 2"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); Container c = getContentPane(); b = new JButton ("Click me to exit.."); b.setActionCommand("exit"); b.addActionListener(this); c.add(b, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("exit")){ System.exit(0);} } public static void main (String args []) throws Exception{ new program2();}}
if in first class i have a string ex: String c="hello world";
how can i get the text into the second class?Last edited by JavaCy; 09-12-2010 at 05:42 PM.
- 09-12-2010, 05:46 PM #4
Many ways.how can i get the text into the second class
Class with String calls method in other class with the String as an arg
secCls.setString("THE STRING");
Class wanting the String can call a method in the class with String that returns the String.
String str = hasString.getString();
String getString() {
return "THE STRING";
}
- 09-12-2010, 05:55 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 29
- Rep Power
- 0
- 09-12-2010, 06:00 PM #6
hasString is a reference to the class that has the String.
AClass hasString = new AClass();
getString() is a method in the above class that returns a String.
- 09-12-2010, 06:05 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 29
- Rep Power
- 0
secCls.setString("THE STRING");
is not working =/
program1.java:7: <identifier> expected
program2.setString("THE STRING");
^
program1.java:7: illegal start of type
program2.setString("THE STRING");
^
2 errors
- 09-12-2010, 06:11 PM #8
There is not enough source shown to determine why you are getting an error.
Can you show the lines of code around the errors?
Are the lines of code you show inside of a method?
-
- 09-12-2010, 06:17 PM #10
Member
- Join Date
- Sep 2010
- Posts
- 29
- Rep Power
- 0
thank you.Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.*; public class program1 extends JFrame implements ActionListener{ JButton b; // string name is the string that i want to send to the program 2 class String name="aaaa"; public program1(){ this.setSize(150,140); this.setTitle("Java Program"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); Container c = getContentPane(); b = new JButton ("Click me to continue.."); b.setActionCommand("cl"); b.addActionListener(this); c.add(b, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("cl")){ program2 s = new program2(); //here i send the name string into s which is the program 2 class.. s.setString(name); s.setVisible(true);}} public static void main (String args []) throws Exception{ new program1();}} // first class stop here above is the second class class program2 extends JFrame implements ActionListener{ JButton b; AClass hasString = new AClass(); // the m String is the string that i want to print it into the screen String m = hasString.getString(); public program2(){ this.setSize(150,140); this.setTitle("Java Program 2"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); Container c = getContentPane(); b = new JButton ("Click me to exit.."); b.setActionCommand("exit"); b.addActionListener(this); c.add(b, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("exit")){ // with a pop up window i will print the m which is the String where i get from the first class.. JOptionPane.showMessageDialog(null, m );} } public static void main (String args []) throws Exception{ new program2();}}
-
Wow, that code is very hard to read. Is all of your code really fully left justified? No indentations at all?
Which line is causing your error(s)?
-
I see what you're trying to do. Norm's code example above was not to be taken literally and simply pasted into your program. Rather it was a generic example of how to do something. So you shouldn't blindly copy and paste code here into your program, but instead think on what he is demonstrating in his post and then incorporate the ideas into your own classes and code. If anything confuses you then ask. Also, please look up Java code formatting conventions including indentation. If you use proper conventions, more will read your code and be able to understand it.
For example to apply Norm's recommendations in a concrete way, give your second GUI class a private String field, say passedString and a public setString method that accepts a String parameter, and sets the passedString field inside of this method. Also, you may want to look into having your second window be a dialog such as a JDialog and not a JFrame.
Luck.Last edited by Fubarable; 09-12-2010 at 07:00 PM.
- 09-12-2010, 09:30 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 29
- Rep Power
- 0
ok thanks Fubarable
i read it and i change it here is it:
error returned:Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.*; public class program1 extends JFrame implements ActionListener{ JButton b; String name = "aaaa"; public program1(){ this.setSize(150,140); this.setTitle("Java Program"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); Container c = getContentPane(); b = new JButton ("Click me to continue.."); b.setActionCommand("cl"); b.addActionListener(this); c.add(b, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("cl")){ program2 s = new program2(); s.setString(name); s.setVisible(true);}} public static void main (String args []) throws Exception{ new program1();}} // first class stop here above is the second class class program2 extends JFrame implements ActionListener{ JButton b; program1 has = new program1(); String str = has.getString(); public program2(){ this.setSize(150,140); this.setTitle("Java Program 2"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); Container c = getContentPane(); b = new JButton ("Click me to exit.."); b.setActionCommand("exit"); b.addActionListener(this); c.add(b, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("exit")){ // with a pop up window i will print the m which is the String where i get from the first class.. JOptionPane.showMessageDialog(null, str );} } public static void main (String args []) throws Exception{ new program2();}}
I understand how worksJava Code:program1.java:35: cannot find symbol symbol : method setString(java.lang.String) location: class program2 s.setString(name); ^ program1.java:50: cannot find symbol symbol : method getString() location: class program1 String str = has.getString(); ^ 2 errors
i have to create a object in first class
program2 give = new program2();
then i have to setString with the method
and in the second class i have to do the same thing but to get not to set String but compiler return error =/
- 09-12-2010, 09:34 PM #14
Where is the setString() method defined in program2? The compiler can't find it.
You need to add that method to the program2 class
Same problem with getString(). The compiler can not find a definition for in the program1 class.
you need to add that method to the program1 class.Last edited by Norm; 09-12-2010 at 09:38 PM.
- 09-12-2010, 09:54 PM #15
Member
- Join Date
- Sep 2010
- Posts
- 29
- Rep Power
- 0
OMG i have headache...
i change it my friend
error:Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.*; public class program1 extends JFrame implements ActionListener{ JButton b; String name = "aaaa"; public program1(){ this.setSize(150,140); this.setTitle("Java Program"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); Container c = getContentPane(); b = new JButton ("Click me to continue.."); b.setActionCommand("cl"); b.addActionListener(this); c.add(b, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("cl")){ program2 s = new program2(); s.getString(name); s.setVisible(true);}} public static void main (String args []) throws Exception{ new program1();}} // first class stop here above is the second class class program2 extends JFrame implements ActionListener{ JButton b; program1 s2 = new program1(); String str = s2.setString(); public program2(){ this.setSize(150,140); this.setTitle("Java Program 2"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(new FlowLayout()); this.setVisible(true); Container c = getContentPane(); b = new JButton ("Click me to exit.."); b.setActionCommand("exit"); b.addActionListener(this); c.add(b, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("exit")){ // with a pop up window i will print the m which is the String where i get from the first class.. JOptionPane.showMessageDialog(null, str );} } public static void main (String args []) throws Exception{ new program2();}}
i can't understand what's going wrong..Java Code:program1.java:34: cannot find symbol symbol : method getString(java.lang.String) location: class program2 s.getString(name); ^ program1.java:49: cannot find symbol symbol : method setString() location: class program1 String str = s2.setString(); ^ 2 errors
- 09-12-2010, 10:08 PM #16
What did you change? You are getting the same errors for the same reasons.i change it my friend
- 09-12-2010, 10:12 PM #17
Member
- Join Date
- Sep 2010
- Posts
- 29
- Rep Power
- 0
I have move the setString into the second class (from the first class) and the getString into first class (from the second)
- 09-12-2010, 10:13 PM #18
Where did you define the methods you are trying to use?
Do you know how to define a method in a class?
I can see two methods you have defined: main() and actionPerformed().
- 09-12-2010, 10:24 PM #19
Member
- Join Date
- Sep 2010
- Posts
- 29
- Rep Power
- 0
Ny friend i cant understand what are you trying to say..
Please if you can and if that is easy for you complete my code with the corect lines.. And after or the Following day i am going to read and learn it too.. I am writing from iphone wright now.
Thank you in adcance.
This forum is very friendly!! :)
- 09-12-2010, 10:28 PM #20
Similar Threads
-
jsp frames
By vasug in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-16-2010, 05:00 PM -
Switching Frames
By jonnytabpni in forum New To JavaReplies: 1Last Post: 11-08-2009, 10:12 PM -
Help regarding Frames
By ramesh.8189 in forum AWT / SwingReplies: 14Last Post: 02-15-2009, 08:12 AM -
What are the hot java frames works on demand
By mallaravi in forum Web FrameworksReplies: 1Last Post: 10-28-2008, 01:34 PM -
will java webBrowser work with frames
By elipford in forum New To JavaReplies: 2Last Post: 06-04-2007, 03:26 PM


LinkBack URL
About LinkBacks


Bookmarks