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);
}
}
Re: Need help correcting a JFrame program for class!
Quote:
Originally Posted by
AlexAndAHalf
"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;".
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.
Re: Need help correcting a JFrame program for class!
Quote:
Originally Posted by
AlexAndAHalf
"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.
Quote:
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.
Quote:
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?
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?
Re: Need help correcting a JFrame program for class!
Quote:
Originally Posted by
WhiteSoup12
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.
Re: Need help correcting a JFrame program for class!
Quote:
Originally Posted by
AlexAndAHalf
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?
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?
Re: Need help correcting a JFrame program for class!
Quote:
Originally Posted by
AlexAndAHalf
@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,
Code :
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.
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.
Code :
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.