Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Need help correcting a JFrame program for class!

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help correcting a JFrame program for class!

    Hey there, I'm a beginner at Java and about 3/4 of the way through my first class on it. In this assignment, I need to make a JFrame with panels, and a JButton that switches to the opposite panel when clicked. According to the Java editor that I use, Eclipse, "The serializable class JMovingFrame does not declare a static final serialVersionUID field of type long." I have no idea what I'm doing wrong here, and help would be MUCH appreciated! By the way, my command prompt is saying that "javac is not an internally or externally recognized command." when I try to compile programs using it, so this problem may be Eclipse-related, I'm really not sure!

    Here is my code:

    ---------------------------------------------------------------------------------------------

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class JMovingFrame extends JFrame implements ActionListener
    {
    private final int WIDTH = 500;
    private final int HEIGHT = 500;
    private JButton button = new JButton("Press Me!");
    private Container con = getContentPane();
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    public JMovingFrame()
    {
    super("J Moving Button");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    con.add(panel1);
    con.add(panel2);
    panel1.add(button);
    button.addActionListener(this);
    setSize(WIDTH, HEIGHT);
    }
    public void actionPerformed(ActionEvent event)
    {
    panel1.remove(button);
    panel2.add(button);
    }
    public static void main(String[] args)
    {
    JMovingFrame frame = new JMovingFrame();
    frame.setVisible(true);
    }
    }


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Need help correcting a JFrame program for class!

    Quote Originally Posted by AlexAndAHalf View Post
    "The serializable class JMovingFrame does not declare a static final serialVersionUID field of type long."
    Add "private static final long serialVersionUID = 1L;" to your class level variables, right above "private final int WIDTH = 500;".


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help correcting a JFrame program for class!

    That removed the warning, but for some reason when I run my program, there is no visible button. I'm really not understanding why, any ideas?

    Thanks so much for the help by the way.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need help correcting a JFrame program for class!

    Quote Originally Posted by AlexAndAHalf View Post
    "The serializable class JMovingFrame does not declare a static final serialVersionUID field of type long."
    If I remember correctly this is only a warning. You code should still compile and run. Alternatively you could go to the API and read about the Serializable class and find out what the error means.
    By the way, my command prompt is saying that "javac is not an internally or externally recognized command."
    This means you have not set up your environment variables correctly. Google should yield plenty of information.
    this problem may be Eclipse-related
    How would a command prompt issue be Eclipse related? If you are using Eclipse then why aren't you using it to compile and run your code?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help correcting a JFrame program for class!

    Sorry, my wording was unclear. You are absolutely right, the program compiled and ran just fine. With my statement about javac not working in the Command Prompt, I was implying that I wasn't using the Command Prompt to compile, and saying that the problem may be Eclipse-related. I worded it awkwardly, I apologize.

    But my main problem now is that my JButton will not show up in it's frame, any idea why this is?

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help correcting a JFrame program for class!

    Quote Originally Posted by WhiteSoup12 View Post
    Add "private static final long serialVersionUID = 1L;" to your class level variables, right above "private final int WIDTH = 500;".
    Read OP's problem description first, before giving such a bad advices.

    @AlexAndHalf: You need to read about Swing. And as far as javac is an issue, check, if java is installed or not. If yes, check the ClassPath in your system environment variables.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help correcting a JFrame program for class!

    Quote Originally Posted by AlexAndAHalf View Post
    Sorry, my wording was unclear. You are absolutely right, the program compiled and ran just fine. With my statement about javac not working in the Command Prompt, I was implying that I wasn't using the Command Prompt to compile, and saying that the problem may be Eclipse-related. I worded it awkwardly, I apologize.

    But my main problem now is that my JButton will not show up in it's frame, any idea why this is?
    Yes coz you are adding buttons to the panel, and panel in the container and where are you adding conatianer to Frame?

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help correcting a JFrame program for class!

    @Mr.777 Sounds logical, I thought the container was automatically a part of the frame. So how to I add the container to the frame?

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help correcting a JFrame program for class!

    Quote Originally Posted by AlexAndAHalf View Post
    @Mr.777 Sounds logical, I thought the container was automatically a part of the frame. So how to I add the container to the frame?
    Well, you must add all the components in the frame and then,
    Container whateverObj = frameName.getContentPane();
    Becuase containers are the top level and JFrame, JPanel etc extends Container, so you need to assign Container something that you want it to show.

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help correcting a JFrame program for class!

    Well, may be i am little bad with explaining all this. Let me quote an example here.
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.*;
     
    public class Test extends JFrame {
     
      public Test(  ) {
     
        setSize(200, 200);
        setLocation(200, 200);
        JButton button = new JButton("Click");
        button.addActionListener(new ActionListener(  ) {
          public void actionPerformed(ActionEvent ae) {
            System.out.println("You clicked me.");
          }
        });
     
        Container content = getContentPane(  );
        content.setLayout(new FlowLayout(  ));
        content.add(button);
      }
     
      public static void main(String[] args) {
        JFrame frame = new Test(  );
        frame.addWindowListener(new WindowAdapter(  ) {
          public void windowClosing(WindowEvent we) { System.exit(0); }
        });
        frame.setVisible(true);
      }
    }
    This example might help you in what are you asking about.

Similar Threads

  1. Two class program help
    By BuhRock in forum Object Oriented Programming
    Replies: 4
    Last Post: July 4th, 2012, 05:27 PM
  2. Help with JFrame program that calculates average
    By ePerKar3 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 4th, 2011, 08:48 AM
  3. Java error correcting, testing for a square issue i think
    By djl1990 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2011, 03:17 AM
  4. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM
  5. Imitating a JFrame extended program with JPanel; help needed...
    By emigrant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 02:30 PM

Tags for this Thread