-4

I have successful compiled and ran the following code but the applet windows shows nothing except blank space,what could be the problem?

Im Not trying to create an applet here im trying to create a Jframe program

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

//<applet code="calc.class" width=400 height=200></applet>
public class calc extends JFrame implements ActionListener {
    JFrame f1;
    JPanel p1 = new JPanel();
    JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15,
            l16, l17, l18, l19, l20, l21, l22, l23, l24;
    JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15,
            b16;
    JTextField t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11;

    public void init() {
        f1 = new JFrame("Administrator");
        p1.setLayout(new GridLayout(3, 2));
        p1.add(l1);
        p1.add(t1);
        p1.add(l2);
        p1.add(b1);
        p1.add(b2);
        f1.add(p1);
        f1.setSize(300, 300);
        f1.setVisible(true);

    }

    public void actionPerformed(ActionEvent AE) {

    }

    public void main(String[] args) {
        calc s = new calc();
        s.init();
    }
}

In the CMD Window i get the following exception

java.lang.ClassCastException: calc cannot be cast to java.applet.Applet
        at sun.applet.AppletPanel.createApplet(AppletPanel.java:795)
        at sun.applet.AppletPanel.runLoader(AppletPanel.java:724)
        at sun.applet.AppletPanel.run(AppletPanel.java:378)
        at java.lang.Thread.run(Thread.java:722)

EDIT

As per your comments i have added a Main class and changed the file name to cool.java,still its not working

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//<applet code="calc.class" width=400 height=200></applet>
public class calc extends JFrame implements ActionListener
{
JFrame f1;
JPanel p1=new JPanel();
JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15,l16,l17,l18,l19,l20,l21,l22,l23,l24;
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
    JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11;
public void init()
{
f1=new JFrame("Administrator");
p1.setLayout(new GridLayout(3,2));
        p1.add(l1);p1.add(t1);p1.add(l2);
        p1.add(b1);p1.add(b2);
        f1.add(p1);
        f1.setSize(300,300);
        f1.setVisible(true);

}
 public void actionPerformed(ActionEvent AE)
 {

 }

}
public class cool
{
 public static void main(String[]args)
 {
 calc s=new calc();
 s.init();
 }
 }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
techno
  • 6,100
  • 16
  • 86
  • 192
  • Actually JFrame != JApplet, since this is a Swing applet. Also, class names start with an upper case character so it should be "Calc", not "calc". – camickr Feb 25 '13 at 05:24
  • Have a look at [This Link](http://stackoverflow.com/questions/14672182/button-and-textfield-dont-show-up-in-java/14673412#14673412) to see when to extends `JFrame`. – joey rohan Feb 25 '13 at 05:24
  • so how am i supposed to run it? – techno Feb 25 '13 at 05:25
  • @techno: well how the heck do you *want* to run it? As a stand alone or as an applet? You tell us. – Hovercraft Full Of Eels Feb 25 '13 at 05:26
  • @HovercraftFullOfEels i just want to run it – techno Feb 25 '13 at 05:27
  • 2
    If you want to run it as an application (ie. JFrame) then read the Swing tutorial on [Compiling and Running Swing Programs](http://docs.oracle.com/javase/tutorial/uiswing/start/compile.html). If you want to run it as an Applet, the tutorial also has sections on "How to Make Applets" or "How to Use Top Level Containers" which will help with your JFrame. – camickr Feb 25 '13 at 05:28
  • Your full concept is wrong.You call this an Applet, but cannot see a single `javax.swing.JApplet`.`JApplets` and `JFrame` are two different containers. – joey rohan Feb 25 '13 at 05:29
  • 1
    Yes, I think you are suffering from Acute Tutorial Deficiency Syndrome, or ATDS. The only treatment is a headlong dive into the Java tutorials without stopping for air. While this therapy is quite rigorous and harsh, fortunately the condition is quite curable if the therapy is applied aggressively and frequently enough. – Hovercraft Full Of Eels Feb 25 '13 at 05:29
  • @HovercraftFullOfEels i have an exam now there is a question from this ,could you help me out with a quick fix,just need to run it – techno Feb 25 '13 at 05:32
  • ATDS, yeah..:D -1 You should know the concept before asking a question. – joey rohan Feb 25 '13 at 05:32
  • -1 and voting to close: it's either not-a-question (you are not at all prepared for your exam ...) or too localized (you are not at all prepared for your exam ...) - and refuse to learn (f.i. formatting your code to make it at least easily readable) Plus you should start learning java naming conventions and stick to them (again to make your code at least easily readable) – kleopatra Feb 25 '13 at 11:53
  • any way i passed in the exam,i got a javascript program – techno Feb 25 '13 at 14:36

3 Answers3

6

You've a lot of errors...

  • You appear to be trying to run this class from HTML code as if it were an applet, and since it isn't an applet, you're getting an error.
  • Your class extends JFrame and your displaying it, but...
  • you're putting all of your components in another JFrame and putting nothing into the JFrame that you are in fact trying to display, the current object, the "this".

I suggest:

  • Discard your code above. Sorry but it is completely worthless and it is not worth trying to "save". Start over.
  • If you need to display an applet, then don't use JFrames.
  • Instead create a class that extends JApplet and in its init() method, stuff its contentPane with your GUI component goodies.
  • If you want to display a stand alone class, then yes, use JFrames.
  • If so, then don't have your class extend JFrame, but instead simply use a JFrame object and display it.
  • Gear your class code towards creating a JPanel, and then display that in the JFrame.
  • Read the Swing GUI tutorials to learn all the gory details on how to create a Swing GUI, but applet and standalone.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • im not trying to create an an applet,im trying to create a Jframe based program,but its the appletviewer mycode.java the way to run the Jframe programs – techno Feb 25 '13 at 05:26
  • 3
    @techno: ***NO***, you don't display JFrames with the applet viewer. You simply run them as standard Java classes. – Hovercraft Full Of Eels Feb 25 '13 at 05:27
  • so java calc ? "java calc Error: Could not find or load main class calc" – techno Feb 25 '13 at 05:28
  • @techno: you will want to post the entire error message. A quick glance at your code suggests that you're not declaring our main method static, and it must be static to work: `public static void main(String[] args) {....` Memorize this as you will use it a *lot*! – Hovercraft Full Of Eels Feb 25 '13 at 05:32
  • Not working,i have tried adding a main method and a separate class and renamed the file to that class name,still nothing – techno Feb 25 '13 at 05:36
  • @techno: "not working" doesn't give us much to go on, doesn't tell us what you could possibly be doing wrong, or what assumptions you may be making that are incorrect. So at this point it is very hard for us to help. You can fix this, you can help us by posting your latest code as an edit to your main question (add the new code tot he bottom) and some text that explains your problems with this code,...all the details that would help us better understand things. Good luck! – Hovercraft Full Of Eels Feb 25 '13 at 05:40
  • @techno, I'm amazed at how fast you read the tutorials. Why don't your read them, copy the copy so you start with working code and then make changes. – camickr Feb 25 '13 at 05:51
  • @camickr can i get a link with a working code :P i have tried the first program from here http://zetcode.com/tutorials/javaswingtutorial/firstprograms/ but it does not run – techno Feb 25 '13 at 05:53
  • 1
    @techno, I did give you links to working programs in the comments section at the top of the page. – camickr Feb 25 '13 at 15:48
1

Couple of immediate comments:

  • your class 'calc' is not an extension of java.applet.Applet or javax.swing.JApplet so it cannot be used immediately as an applet.
  • You've never instantiated any of the objects you're adding to the layout in init

I think you need to go back and re-read on how to deal with Applets and java in general.

EDIT since the OP has come back and attempted editing their question to make it more relevant, I'm going to try and flesh out some more.

I believe the answer provided by @MadProgrammer is probably the most concise and provides the most detail of where to look.

The one additional thing I would do is to mention that Java no longer picks up '.' (current directory) as part of the classpath unless explicitly stated.

To run this program you should attempt launching with:

java -cp . Calc

Classes in the 'default' package are discouraged as of late. Consider placing in a package as well.

Dave G
  • 9,639
  • 36
  • 41
  • This is very good advice, 1+ up-vote from me, but now the question has change and we're finding out that he in fact wants to create a standalone GUI. You may wish to alter some portions of this answer so that it will remain relevant. – Hovercraft Full Of Eels Feb 25 '13 at 05:35
1

To add to all the excellent answers that have already been posted...

You've not initializing anything, so all your components null, this is basic Java/Swing.

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

public class Calc extends JFrame implements ActionListener {

//    JFrame f1; // Don't need this, as you are already extending from a JFrame!!
    JPanel p1 = new JPanel();
    JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15, l16, l17, l18, l19, l20, l21, l22, l23, l24;
    JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16;
    JTextField t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11;

    public Calc() {
        super("Administrator");
        // You've not initalised anything, so it would normally throw a NullPointerException
        p1 = new JPanel(new GridLayout(3, 2));

        l1 = new JLabel("l1");
        t1 = new JTextField(2);
        l2 = new JLabel("l2");
        b1 = new JButton("B1");
        b2 = new JButton("B2");

        p1.add(l1);
        p1.add(t1);
        p1.add(l2);
        p1.add(b1);
        p1.add(b2);
        add(p1);
        // Use pack instead...
//        setSize(300, 300);
        pack();
        setVisible(true);

    }

    public void actionPerformed(ActionEvent AE) {
    }

    public static void main(String[] args) {
        Calc s = new Calc();
    }
}

I would (seriously) go back and have a read of

I'd also recommend you have a read of Code Conventions for the Java Programing Language (AKA How to make friends and influence people)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • i used your code compiled it and " java calc Error: Could not find or load main class calc" this is what i get how im i supposed to run this thing? – techno Feb 25 '13 at 06:08
  • Really, runs just fine for me. To start with, it's Cal, not calculate. Secondly, did you compile it? Thirdly, what IDE are you using? – MadProgrammer Feb 25 '13 at 06:18
  • No Ide just cmd i just want to get this thing running i compiled it?What you mean by Cal? Calc is the class name – techno Feb 25 '13 at 06:20
  • is there a way to run this within appletviewer or something – techno Feb 25 '13 at 06:21
  • I pretty sure it's already been mentioned, `JFrame`'s can't be used within the applet viewer. Assuming that `Calc` is in the default package. `javac Calc` followed by `java Calc` should work just find. Java is case sensetive, so there is a difference between `calc` and `Calc`. I would strongly recommend that you get yourself an IDE, it well make your life simpler in the long run. – MadProgrammer Feb 25 '13 at 08:12