0

I am designing a calculator and i want a menu bar and a textfield in my application. I used a grid layout. When I use setJmenuBar(menubar) I'm getting a compilation error, but when i use frame.setJmenuBar(menubar) I don't, however the menubar is not visible. I need some help with this.

/* Mini Project         :     Simple Arithematic Calculator
   Project Members      :     Vamsy.M,Azghar.M,Navya.C,Powlomi
   Date                 :     17-04-2015
*/


// Packages and classes imported

import javax.swing.JMenu;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JMenuItem;
import java.awt.FlowLayout;
import javax.swing.JMenuBar;
import javax.swing.*;

public class Calculator extends JPanel implements ActionListener 
{
  public JTextField display = new JTextField("0");
  private String buttonText = "789/456*123-0.=+";
  private double result = 0;
  private String operator = "=";
  private boolean calculating = true;

  public Calculator() 
  {
    display.setColumns(2);
    setLayout(new BorderLayout());
    display.setEditable(false);
    add(display, "North");
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(5,4));
    p.setBackground(new Color(0,0,0,120));
    JButton c=new JButton("%");
    //c.setBackground();
    p.add(c);
    c.addActionListener(this);
    JButton d=new JButton("inv");
    p.add(d);
    d.addActionListener(this);
    JButton e=new JButton("x^2");
    p.add(e);
    e.addActionListener(this);
    JButton f=new JButton("sqrt");
    p.add(f);
    f.addActionListener(this);
    for (int i = 0; i < buttonText.length(); i++) 
    {
      JButton b = new JButton(buttonText.substring(i, i+1 ));
      p.add(b);
      b.addActionListener(this);
    }
    add(p,"Center");
  }

  public void actionPerformed(ActionEvent evt) 
  {
    String cmd = evt.getActionCommand();

    if ('0' <= cmd.charAt(0) && cmd.charAt(0) <= '9' || cmd.equals(".")) 
    {
      if (calculating)
        display.setText(cmd);
      else
        display.setText(display.getText() + cmd);
      calculating = false;
    } 
    else 
    {
      if (calculating) 
      {
        if (cmd.equals("-")) 
        {
          display.setText(cmd);
          calculating = false;
        } 
        else
          operator = cmd;
      } 
      else 
      {
        double x = Double.parseDouble(display.getText());
        calculate(x);
        operator = cmd;
        calculating = true;
      }
    }
  }

  private void calculate(double n) 
  {
    if (operator.equals("+"))
      result += n;
    else if (operator.equals("-"))
      result -= n;
    else if (operator.equals("*"))
      result *= n;
    else if (operator.equals("/"))
      result /= n;
    else if (operator.equals("="))
      result = n;
    else if (operator.equals("inv"))
      result = 1/n;
    else if (operator.equals("x^2"))
      result = n*n;
    else if (operator.equals("sqrt"))
      result = Math.sqrt(n);
    else if (operator.equals("%"))
      result = result%n;
    display.setText("" + result);
  }

  public static void main(String[] args) 
  {
    JFrame frame = new JFrame();
    frame.setTitle("Calculator");
    frame.setSize(300, 300);
    frame.addWindowListener(new WindowAdapter() 
    {
      public void windowClosing(WindowEvent e) 
      {
        System.exit(0);
      }
    });
    JMenuBar menubar = new JMenuBar();
    JMenu name = new JMenu("Options");
    JMenuItem credits = new JMenuItem("Credits");
    setJMenuBar(menubar);
    credits.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        JOptionPane.showMessageDialog(frame,"\tVamsy.M\n\tAzghar.M\n\tNavya.C\n\tPowlomi.R","Credits",JOptionPane.PLAIN_MESSAGE);
      }
    });
    frame.setLocationRelativeTo(null); 
    Container contentPane = frame.getContentPane();
    //contentPane.setBackground(Color.black);
    //contentPane.add(name);
    contentPane.add(new Calculator());
    frame.show();
  }
}
Intermernet
  • 18,604
  • 4
  • 49
  • 61
VaM999
  • 453
  • 1
  • 9
  • 23
  • Your class extends `JPanel`, so calling `setJMenuBar` won't work, since `JPanel` does not contain that method. `JFrame` does though, which is why the error goes away when you do `frame.setJMenuBar` – Vince Apr 20 '15 at 17:29
  • But when i use frame.setJMenuBar , nothing's happening. – VaM999 Apr 20 '15 at 17:30
  • 2
    Well there is quite a few problems with your code. You never post your swing code to the [EDT](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html), you're using a [deprecated method](http://stackoverflow.com/a/15081647/2398375) (`frame.show()`; you should be using `frame.setVisible(true)`, and on top of all that, you never `pack()` your frame after. **I think your main problem** is that you never added the `JMenuItem` to the `JMenu` and never added the `JMenu` to the `JMenuBar`. You need `name.add(credits)` and `menubar.add(name)` – Vince Apr 20 '15 at 17:35
  • I'm not able to run your code snippet – Phantômaxx Apr 20 '15 at 17:35
  • 2
    Read the section from the Swing tutorial on [How to Use Menus](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html) for a working example and better code structure so your components are created on the `Event Dispatch Thread` by using the `invokeLater()` method. – camickr Apr 20 '15 at 17:41
  • Thanks a million @VinceEmigh. It is now working great without any note about deprecated API also. Thank you – VaM999 Apr 20 '15 at 17:44
  • Format code inline, slight grammar changes – Intermernet Apr 23 '15 at 08:32

0 Answers0