Hi! Im new to Java and iv been studying GUI's in it. Just started with swing and tried to make this giu for a calcularot. but it doesnt execute and gives me "java.lang.NoClassDefFoundError" error. im a total noob so i cant figure it out. i'd really appreciate it someone could point out what im doing wrong here :/ ..

The following is the code:

package Calculator_gui;

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


public class Calculator_gui extends JFrame {
    private JButton btns[]=new JButton[10];
    private JButton signBtns[]=new JButton[4];
    private JTextField result;


    public Calculator_gui()
    {
        JPanel main_p=new JPanel(new FlowLayout());
        result=new JTextField("0",12);
        result.setEditable(false);
        main_p.add(result);
        this.setContentPane(main_p);

        JPanel btpanel=new JPanel(new GridLayout(4,3)); //for numpad
        for(int i=0;i<10;i++)
        {
            btns[i]=new JButton(i+"");
            btpanel.add(btns[i]);
        }

        JPanel signpanel=new JPanel(new GridLayout(4,1));   //for sign buttons
        signBtns[0]=new JButton("+");
        signpanel.add(signBtns[1]);
        signBtns[1]=new JButton("-");
        signpanel.add(signBtns[2]);
        signBtns[2]=new JButton("*");
        signpanel.add(signBtns[3]);
        signBtns[3]=new JButton("/");
        signpanel.add(signBtns[3]);

        this.setLayout(new BorderLayout());
        this.add(main_p,BorderLayout.NORTH);
        this.add(btpanel,BorderLayout.CENTER);
        this.add(signpanel,BorderLayout.EAST);


        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(200,200);
        this.setTitle("Caculator");

    }

    public static void main(String[] args) {
   // Run the GUI codes in the Event-dispatching thread for thread-safety
   SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
         new Calculator_gui();  // Let the constructor do the job
      }
   });
    }
}

Recommended Answers

All 3 Replies

  1. where you lost signpanel.add(signBtns[0]);

  2. probably have to check all numbering for next JButtons

  3. then array should be declared private JButton signBtns[] = new JButton[5];

  4. to dissable, comment code line //this.setContentPane(main_p);

@OP
As mKorbel was mentioning I noticed the root of a few of the errors were coming from you trying to access elements outside of the range of the declared array. For instance, you were an index too high when adding them to the signPanel, so I just did the following:

 signBtns[0] = new JButton("+");
        signpanel.add(signBtns[0]);
        signBtns[1]=new JButton("-");
        signpanel.add(signBtns[1]);
        signBtns[2]=new JButton("*");
        signpanel.add(signBtns[2]);
        signBtns[3]=new JButton("/");
        signpanel.add(signBtns[3]);

That cleared up some of the errors, however; make the suggested changes that mKorbel said, and see how your code is looking from there.

Okay. got the issue resolved. Thanks for the help guys :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.