0

In my online Java class, I have this code:

/* This is option 1 of my Java final project. This is a program which displays a frame with a button that, when you click it, increases the number below it. */

import java.awt.*; // imports everything I need for this project
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class option1 { // This is where I define most of my stuff. Technical restrictions prevent me from doing this in the class where the program is itself executed (see comment towards the end).

    public static void main(String[] args) {

        final Frame mainFrame = new OptionOne(); // First I define my frame, button, and labels ...
        Button button = new Button("Button");
        final Label label1a = new Label();
        final Label label1b = new Label();
        final Label label1c = new Label();
        final Label label2 = new Label();

        label1a.setBounds(15, 30, 270, 10); // ... and set their bounds and the labels' text. Labels 1a, 1b, 1c give instructions, while label2 displays the number.
        label1a.setText("This is a program where you click a button");
        label1b.setBounds(15, 45, 270, 10);
        label1b.setText("and the number below it increases each time");
        label1c.setBounds(15, 60, 270, 10);
        label1c.setText("you click. To start, just click the button.");
        label2.setBounds(150, 220, 50, 30);
        label2.setText("0");
        button.setBounds(100, 100, 50, 50);

/* After this point, things get a little more complicated. Technical restrictions on usage of variables accross classes mean that I had to make some unusual code decisions, as you will see. */

        label2.addPropertyChangeListener(label2.getText(), new PropertyChangeListener() { // This part is used to detect a change in the appearance of label2, and to react appropriately to it.
            @Override // All the "@Override"s are here because it gives a compiling error if I don't put them.
            public void propertyChange(PropertyChangeEvent evt) {

            }
        });

        button.addMouseListener(new MouseListener() { // This part tells the program what to do when you click the button.
            @Override // the only @Override that isn't here for technical reasons (see earlier comment at first @Override) 
            public void mouseClicked(MouseEvent e) {
                int value = Integer.parseInt(label2.getText());
                label2.setText(String.valueOf(value + 1));  
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        mainFrame.add(label1a); // Now that the labels and button are fully defined, we are now ready to add them.
        mainFrame.add(label1b);
        mainFrame.add(label1c);
        mainFrame.add(label2);
        mainFrame.add(button);
    }
}

class OptionOne extends Frame { // technical restrictions prevent me from putting most of my code in this class. in this code I define the actual frame itself, for its final use.

    OptionOne() {
        setTitle("Final Project Option 1");
        setSize(300, 300);
    show();
    }
}

and I need to run it as an applet. Directly putting it into HTML code like

<head></head>
<body>
<applet code="OptionOne.class" width="300" height="300">
</applet>
</body>

gives me a "java.lang.reflect.invocationtargetexception" error. Why am I getting this error? I know the error must be in the HTML code because the program when put in the compiler runs fine, so what am I doing wrong?

EDIT: Now I have the code changed to:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class option1main extends Panel {

    public static void main(String[] args) {

        Button button = new Button("Button");
        final Label label1a = new Label();
        final Label label1b = new Label();
        final Label label1c = new Label();
        final Label label2 = new Label();

        label1a.setBounds(15, 30, 270, 10);
        label1a.setText("This is a program where you click a button");
        label1b.setBounds(15, 45, 270, 10);
        label1b.setText("and the number below it increases each time");
        label1c.setBounds(15, 65, 270, 10);
        label1c.setText("you click. To start, just click the button.");
        label2.setBounds(150, 220, 50, 30);
        label2.setText("0");
        button.setBounds(100, 100, 50, 50);

        label2.addPropertyChangeListener(label2.getText(), new PropertyChangeListener() {
            @Override // All the "@Override"s are here because it gives a compiling error if I don't put them.
            public void propertyChange(PropertyChangeEvent evt) {

            }
        });

        button.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                int value = Integer.parseInt(label2.getText());
                label2.setText(String.valueOf(value + 1));  
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }
}

public class option1 {

    option1main() {
        final Frame mainFrame = new OptionOne();
        mainFrame.add(label1a);
        mainFrame.add(label1b);
        mainFrame.add(label1c);
        mainFrame.add(label2);
        mainFrame.add(button);
    }

}

public class panel1 extends Applet {

    /* init() { - not sure what to add here...? */

}

class OptionOne extends Frame {

    OptionOne() {
        setTitle("Final Project Option 1");
        setSize(300, 300);
    show();
    }
}

When compiling it gives me the error, "invalid method declaration: return type required" on the line "option1main() {". Why is that?

randomguy537
  • 155
  • 1
  • 1
  • 7
  • You've read the tutorials right? You understand that in order to display an applet, the class needs to extend from `Applet`? You do realise that there is a significant difference between a windowed application and a applet? – MadProgrammer Dec 23 '14 at 02:43
  • updated post, i have a different error now – randomguy537 Dec 23 '14 at 16:26
  • Option1main is not a valid constructor or option1 – MadProgrammer Dec 23 '14 at 18:54
  • then what should i do? – randomguy537 Dec 23 '14 at 21:54
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Dec 24 '14 at 03:12
  • Do you understand the difference between a constructor and a method? – MadProgrammer Dec 24 '14 at 03:44

1 Answers1

2
  1. Move your base UI to a separate class which extends from Panel
  2. Allow you current (option1) class use this class and add it to the Frame instance you created.
  3. Create a new class which extends from Applet. In this classes init method, create an instance of the Panel class (from step 1) and add it to the applet
  4. Change your <applet code="OptionOne.class" width="300" height="300"> to point to the applet class you created in step 3.

Side notes...

  • Consider using Swing or JavaFX, no offense, AWT is 15+ years out of date and most people don't have (or are out of) experience with it
  • Consider making use of appropriate layout managers in order to simply issues with cross platform rendering differences
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366