如何将Jtextfield和菜单栏添加到java框架?

时间:2015-04-20 17:26:06

标签: java swing jframe

我正在设计一个计算器,我想在我的应用程序中使用菜单栏和文本字段。我使用了网格布局。当我使用setJmenuBar(menubar)我收到编译错误时,但是当我使用frame.setJmenuBar(menubar)时,我不会,但是菜单栏不可见。我需要一些帮助。

/* 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();
  }
}

0 个答案:

没有答案