Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    JavaCy is offline Member
    Join Date
    Sep 2010
    Posts
    29
    Rep Power
    0

    Default 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:

    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();}}
    This is the second class.

    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();}}
    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?
    with out IDE like netbeans etc.
    Thank you in advance

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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();

  3. #3
    JavaCy is offline Member
    Join Date
    Sep 2010
    Posts
    29
    Rep Power
    0

    Default

    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:

    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 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();}}
    and another question
    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    how can i get the text into the second class
    Many ways.
    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";
    }

  5. #5
    JavaCy is offline Member
    Join Date
    Sep 2010
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    String str = hasString.getString();

    can you explain me how works?
    hasString is the String name?
    ex:
    String hasString;

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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.

  7. #7
    JavaCy is offline Member
    Join Date
    Sep 2010
    Posts
    29
    Rep Power
    0

    Default

    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

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by JavaCy View Post
    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
    Without seeing your code, it's hard to know what you're doing wrong. Please post this code, but also please take care to post well-formatted code since if it is easier to read, more of us will read it.

    Luck.

  10. #10
    JavaCy is offline Member
    Join Date
    Sep 2010
    Posts
    29
    Rep Power
    0

    Default

    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();}}
    thank you.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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)?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  13. #13
    JavaCy is offline Member
    Join Date
    Sep 2010
    Posts
    29
    Rep Power
    0

    Default

    ok thanks Fubarable

    i read it and i change it here is it:

    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();}}
    error returned:

    Java 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 understand how works
    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 =/

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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.

  15. #15
    JavaCy is offline Member
    Join Date
    Sep 2010
    Posts
    29
    Rep Power
    0

    Default

    OMG i have headache...
    i change it my friend

    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();}}
    error:

    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
    i can't understand what's going wrong..

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    i change it my friend
    What did you change? You are getting the same errors for the same reasons.

  17. #17
    JavaCy is offline Member
    Join Date
    Sep 2010
    Posts
    29
    Rep Power
    0

    Default

    I have move the setString into the second class (from the first class) and the getString into first class (from the second)

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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().

  19. #19
    JavaCy is offline Member
    Join Date
    Sep 2010
    Posts
    29
    Rep Power
    0

    Default

    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!! :)

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    OK. We'll wait until you get to your computer.
    Meanwhile read up in your textbook on how to define a method in a class.

Page 1 of 2 12 LastLast

Similar Threads

  1. jsp frames
    By vasug in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-16-2010, 05:00 PM
  2. Switching Frames
    By jonnytabpni in forum New To Java
    Replies: 1
    Last Post: 11-08-2009, 10:12 PM
  3. Help regarding Frames
    By ramesh.8189 in forum AWT / Swing
    Replies: 14
    Last Post: 02-15-2009, 08:12 AM
  4. What are the hot java frames works on demand
    By mallaravi in forum Web Frameworks
    Replies: 1
    Last Post: 10-28-2008, 01:34 PM
  5. will java webBrowser work with frames
    By elipford in forum New To Java
    Replies: 2
    Last Post: 06-04-2007, 03:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •