-3

Here is my code(for an applet):

class Calculator extends JApplet{

JTextField numberbox; //all neccessary components
JLabel answerbox;
JButton add;
JButton subtract;
JButton multiply;
JButton divide;
JButton enter;
double anumber, bnumber; //for calculation
int function = 1;
String answer;

public void init(){ //startup the applet
try{
SwingUtilities.invokeAndWait(new Runnable(){
public void run(){
guiInit();
}
});
}
catch(Exception e){
System.out.println("Can't create: "+e);
}
}

public void guiInit(){
setLayout(null); //absolute positioning
numberbox = new JTextField(""); //setting text, size and position of all     components
numberbox.setBounds(5,5,300,200);
answerbox = new JLabel("");
answerbox.setBounds(700, 200, 50,20);
add = new JButton("+");
add.setBounds(5, 210, 50, 20);
subtract = new JButton("-");
subtract.setBounds(60, 210, 50, 20);
multiply = new JButton("x");
multiply.setBounds(115, 210, 50, 20);
divide = new JButton("/");
divide.setBounds(170, 210, 50, 20);
enter = new JButton("Enter");
enter.setBounds(225, 210, 50, 20);

add.addActionListener(new ActionListener(){//action listeners for each button
public void actionPerformed(ActionEvent le){
if(numberbox.getText() == ""){//if nothing is in the text box, use the last     answer instead
    anumber = Double.parseDouble(answer);
}
else{
    anumber = Double.parseDouble(numberbox.getText());//read the text box
}
numberbox.setText("");//clear the box for the next value
function = 1;//set the operator to be used
}
});

subtract.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent le){
if(numberbox.getText() == ""){
    anumber = Double.parseDouble(answer);
}
else{
    anumber = Double.parseDouble(numberbox.getText());
}
numberbox.setText("");
function = 2;
}
});

multiply.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent le){
if(numberbox.getText() == ""){
    anumber = Double.parseDouble(answer);
}
else{
    anumber = Double.parseDouble(numberbox.getText());
}
numberbox.setText("");
function = 3;
}
});

divide.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent le){
if(numberbox.getText() == ""){
    anumber = Double.parseDouble(answer);
}
else{
    anumber = Double.parseDouble(numberbox.getText());
}
numberbox.setText("");
function = 4;
}
});

enter.addActionListener(new ActionListener(){//if the enter button is     pressed
public void actionPerformed(ActionEvent le){
bnumber = Double.parseDouble(numberbox.getText());//set the second value
numberbox.setText("");//clear the box for next question
if(function == 1){//checking to see what operator should be used
answer = Double.toString(anumber+bnumber);
answerbox.setText(answer);//displaying the answer
}
else if(function ==2){//other operators
answer = Double.toString(anumber-bnumber);
answerbox.setText(answer);
}
else if(function == 3){
answer = Double.toString(anumber*bnumber);
answerbox.setText(answer);
}
else if(function == 4){
answer = Double.toString(anumber/bnumber);
answerbox.setText(answer);
}
else{
answerbox.setText("Error");
}
}
});

getContentPane().add(numberbox);//adding the components to the pane
getContentPane().add(answerbox);
getContentPane().add(add);
getContentPane().add(subtract);
getContentPane().add(multiply);
getContentPane().add(divide);
getContentPane().add(enter);

}
}

sorry for the long code post but I really have no idea what is causing this. I have used applets before but never gotten this exception. I really dont understand any of the research I did on this topic either.

from what I understand, it had something to do with the exception, and therefore I would assume the guiInit() method.

I really dont understand reflection or what in my code uses it

thanks for any possible help, I am obviously new to java.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Wilson
  • 1
  • 4
    Please post the full stack trace of the exception. – Arnaud Dec 06 '16 at 16:16
  • 5
    And please provide a [mcve], formatted properly. It's *really* hard to read at the moment. (You've apologised for it being long, but you should attempt to reduce it as far as possible.) – Jon Skeet Dec 06 '16 at 16:19

1 Answers1

2
class Calculator extends JApplet{

Should be:

// an applet must be declared **public**
public class Calculator extends JApplet{

General advice

  1. For better help sooner, post a MCVE or Short, Self Contained, Correct Example.
  2. Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow!
  3. Always copy/paste error and exception output!
  4. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
  5. 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.
  6. See Java Plugin support deprecated and Moving to a Plugin-Free Web.
  7. See What is a stack trace, and how can I use it to debug my application errors?
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433