Results 1 to 5 of 5
  1. #1
    SirSen is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Need help with my code, its short

    Hi, I am having a problem with the code, I want to make 2 frames and switch between them, by making one visible and the other not visible and viceversa. However it doesnt seem to work, perhaps u can help me.
    The code is the following:

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.io.*;
    import java.lang.*;
    import javax.swing.*;
    
    public class Project extends Frame implements ActionListener
    {
        TextField display;
        Panel p1= new Panel();
        Panel p2= new Panel();
        Button checker;
        Choice metrounit;
        TextField display2;
        Button changer, changer2;
        Project c;
        JFrame frame1, frame2;
        
        
        
        
        
          
        
        public Project(){
        JFrame frame1 = new JFrame("Calculator");
            JFrame frame2 = new JFrame("Calculator2");
        frame1.setLayout(new BorderLayout());
        frame2.setLayout(new BorderLayout());
        Button checker= new Button("Check");
        display = new TextField(30);
        display2 = new TextField(30);
        Button changer= new Button("Change");
        Button changer2= new Button("Change2");
        metrounit = new Choice();
        metrounit.add("Hey");
        metrounit.add("Decimetre");
        
        display.addActionListener(this);
        checker.addActionListener(this);
        
        p1.add(changer);
        p1.add(metrounit);
        p1.add(display);
        p1.add(checker);
        frame1.add(p1,BorderLayout.NORTH);
            
            p2.add(display2);
            p2.add(changer2);
            frame2.add(p2,BorderLayout.NORTH);
            display2.setText("It worked");
        
        
        frame1.setVisible(true);
        frame2.setVisible(false);
        addWindowListener(new w());
        setSize(400,400);
        setResizable(false);
        pack();
    }
        
        
        
        
        
        
        
        
        
    
        
        public void actionPerformed(ActionEvent ae)
        {
            
            String str=ae.getActionCommand();
            if(str.equals(changer))
            {
                frame1.setVisible(false);
                frame2.setVisible(true);
            
            
            
            }
            if(str.equals("Change2"))
            {
                frame1.setVisible(true);
                frame2.setVisible(false);
            
            
            
            }
            
            if (str.equals("Check")){
                String choice1= metrounit.getSelectedItem();
                display.setText(" ");
                if(choice1== "Hey"){
                display.setText("Hey");
                }
            }
            
        
        }
           public static void main(String args[])
    {
    Project c = new Project();
    };
        
        
        
        
        
    class w extends WindowAdapter
    {
    public void windowClosing(WindowEvent e)
    {
    setVisible(false);
    System.exit(0);
    }
    }
    
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,757
    Rep Power
    19

    Default Re: Need help with my code, its short

    What does "it doesn't seem to work" mean?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Need help with my code, its short

    Did you search the forum before asking the question? I responded to a similar question a month ago. Here is the link.

    JFRAME : How to close old frame when a new frame occurs...


    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    SirSen is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Need help with my code, its short

    Thanks to both of you but, still it doesn't work. And by that I mean now the frame new frame that should appear doesnt, I am not really sure wait I've done wrong. The purpose of the program is for me to practice how to make frame visible and the other not.
    It is supposed to make the first frame disappear when i click the button but make the 2nd frame appear at the same time
    Last edited by SirSen; 05-15-2013 at 03:02 AM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,757
    Rep Power
    19

    Default Re: Need help with my code, its short

    You have declared a bunch of attributes for your class, including the 2 JFrames, some JButtons etc etc.
    Then, in your constructor, you hide these attributes by declaring local variables with the same names.
    Consequently 'frame1' and 'frame2' (the attributes) are still null.
    It might even be having an effect on the buttons...I haven't run this.

    So the first thing to do is don't declare new local variables, just initialise the attributes you already have.
    Then see where it fails (if anywhere).
    If it is still failing, then add debugging code (System.out.println()) into the actionPerformed, printing out every value you are checking so you can see what's happening.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 9
    Last Post: 10-23-2012, 10:50 AM
  2. Cant find the problem with this short code.
    By MarioBrawler in forum New To Java
    Replies: 8
    Last Post: 05-26-2012, 12:59 AM
  3. Replies: 3
    Last Post: 02-20-2011, 12:15 PM
  4. Short-cut key
    By tskumarme in forum New To Java
    Replies: 2
    Last Post: 05-28-2008, 04:16 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
  •