• Post Reply Bookmark Topic Watch Topic
  • New Topic

JAVA GUI'S  RSS feed

 
Patricia Murphy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time i run this code, it says that i need to declare class Card1 in a seperate file. How would i do this?

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class Card1 extends Applet implements ActionListener
{
Panel cardPanel;
Panel firstP, secondP, thirdP;
Panel buttonP;
Button first, second, third;
CardLayout ourLayout;

public void init()
{
cardPanel = new Panel();

ourLayout = new CardLayout();

cardPanel.setLayout (ourLayout);


firstP = new Panel();
firstP.setBackground(Color.blue);

secondP = new Panel();
secondP.setBackground(Color.yellow);

thirdP = new Panel();
thirdP.setBackground(Color.green);


first = new Button("First");
first.addActionListener(this);

second = new Button("Second");
second.addActionListener(this);

third = new Button("Third");
third.addActionListener(this);

buttonP = new Panel();
buttonP.add(first);
buttonP.add(second);
buttonP.add(third);


this.setLayout(new BorderLayout());

this.add(buttonP, BorderLayout.SOUTH);
this.add(cardPanel, BorderLayout.CENTER);

cardPanel.add(firstP, "First"); //blue
cardPanel.add(secondP, "Second"); //yellow
cardPanel.add(thirdP, "Third"); //green

}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == first)
ourLayout.show(cardPanel, "First");

if (e.getSource() == second)
ourLayout.show(cardPanel, "Second");

if (e.getSource() == third)
ourLayout.show(cardPanel, "Third");
}
} // end class
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that it says something like class Card1 is public, should be declared in a file named Card1.java. My guess is that you've accidentally named the file something else. The name of the source file must match exactly the name of the public class defined in the source file. Remember Java is case-sensitive.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java every public class needs declared in its own *.java file with the same name as the class. So your code should be in a file called "Card1.java" .
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
In Java every public class needs declared in its own *.java file with the same name as the class. So your code should be in a file called "Card1.java" .


Every public top-level class that is.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!