| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2264 人关注过本帖
标题:复合计算器在菜单栏选择标准和科学型计算器
只看楼主 加入收藏
a3575181
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2016-12-13
结帖率:80%
收藏
 问题点数:0 回复次数:1 
复合计算器在菜单栏选择标准和科学型计算器
分别在同一个包用两个不同的类写了一个标准计算器和一个科学性计算器,怎样才能在菜单里选择然后跳转窗口?就好想windons里的计算器那样
   写了个没法运行的菜单,怎么用

或者 用两个按键 分别调用两个类可以吗?  我是小白 不懂
菜单


程序代码:
package demo;
import java.awt.BorderLayout;
import java.awt.CheckboxMenuItem;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
    public class total extends JFrame implements ActionListener{
         
        private  multipy c1 = new multipy();
        private  calculator c2 = new  calculator();
        
        public total() {
            final MenuBar jmb = new MenuBar();
            this.setMenuBar(jmb);
            Menu[] menu = { new Menu("查看"), new Menu("帮助") };
            final MenuItem[][] menuitem = {
                    
                    { new MenuItem("标准型"), new MenuItem("科学型") },
                    { new MenuItem("查看帮助"), new MenuItem("关于我们") } 
                    
            };
            
            for (int i = 0; i < menu.length; i++) {
                jmb.add(menu[i]);
                
                for (int j = 0; j < menuitem.length; j++) {
                    menu[i].add(menuitem[i][j]);
                    menuitem[i][j].addActionListener(new ActionListener() {
                        
                        
                        public void actionPerformed(ActionEvent e) {
                            MenuItem b = (MenuItem) e.getSource();
                            if (b == menuitem[0][0]) {
                                System.out.print("12222");
                            } else if (b == menuitem[0][1]) {
                                System.out.print("111111");
                            } else if (b == menuitem[1][0]) {
                                JOptionPane
                                        .showMessageDialog(
                                                null,
                                                "这是一个简单的计算器,不支持键盘输入,\n计算请单击确定后,用鼠标单击软键盘进行。",
                                                "查看帮助",
                                                JOptionPane.INFORMATION_MESSAGE);
                            } else if (b == menuitem[1][1]) {
                                JOptionPane.showMessageDialog(null, "        实验用复合计算器 "
                                        + "\n" + "               仅供参考", "关于",
                                        JOptionPane.WARNING_MESSAGE);
                            }
                        }
                    });

                }}    
            
            for (int i = 0; i < menu.length; i++) {
                menu[i].insertSeparator(1);
            }
        }
        
    }







标准计算器
程序代码:
package demo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class multipy extends JFrame implements ActionListener{
    private final String[] KEYS = { "7", "8", "9", "*", "4", "5", "6",
            "+", "1", "2", "3", "-", "0", ".", "+/-", "=" };
    private final String[] COMMAND = { "C","CE", "←" ,"/"};
    private JButton keys[] = new JButton[KEYS.length];
    private JButton commands[] = new JButton[COMMAND.length];
    private JTextField resultText = new JTextField("0");
    private boolean firstDigit = true;
    private double resultNum = 0.0;
    private String operator = "=";
    private boolean operateValidFlag = true;
    
    public static void main (String[] args) {
        multipy multipy= new multipy();
   }
    public multipy(){
        
        resultText.setHorizontalAlignment(JTextField.RIGHT);
        JPanel TJP = new JPanel();
        TJP.setLayout(new BorderLayout());
        TJP.add(resultText);
    
        JPanel JP = new JPanel();
        JP.setLayout(new GridLayout(4,5, 5, 5));
        for (int i = 0; i < KEYS.length; i++) {
            keys[i] = new JButton(KEYS[i]);
            JP.add(keys[i]);
           keys[i].setFont(new Font("黑体", Font.PLAIN, 22));
            }
        JPanel MJP = new JPanel();
        MJP.setLayout(new GridLayout(1, 1, 5, 5));
        for (int i = 0; i < COMMAND.length; i++) {
            commands [i] = new JButton(COMMAND[i]);
            MJP.add(commands[i]);
            commands[i].setFont(new Font("黑体", Font.PLAIN, 22));
            }
        commands[0].setForeground(Color.cyan);
        commands[1].setForeground(Color.cyan);
        keys[3].setForeground(Color.red);
        keys[7].setForeground(Color.red);
        keys[11].setForeground(Color.red);
        commands[3].setForeground(Color.red);
        
        JPanel jp = new JPanel();
        jp.setLayout(new BorderLayout());
        jp.add(MJP);
        jp.add(JP);
        
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add("Center",MJP);
        getContentPane().add("South",JP);
        getContentPane().add("North", TJP);

        for (int i = 0; i < KEYS.length; i++) {
            keys[i].addActionListener(this);
        }
        for (int i = 0; i < COMMAND.length; i++) {
            commands[i].addActionListener(this);
        }
       
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
          this.pack();
        }

 public void actionPerformed(ActionEvent e) {
        String symbol = e.getActionCommand();
        if (symbol.equals(COMMAND[0])){
                    resultText.setText("0");
                    firstDigit = true;
                    operator = "=";
        }else if (symbol.equals(COMMAND[1])){
            resultText.setText("0");
        }else if (symbol.equals(COMMAND[2])) {
            String text = resultText.getText();
             int i = text.length();
              if (i > 0) {
                text = text.substring(0, i-1);
                }
                 if (text.length()==0) {
                   resultText.setText("0");}
                else {
                     resultText.setText(text);
                }
         }else if ("0123456789.".indexOf(symbol) >= 0) {
         handleNumber(symbol);   
        }else {
            handleOperator(symbol);
        }
        }
    private void handleNumber(String key) {
         if (firstDigit) {
                resultText.setText(key);
            } else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)) {
                resultText.setText(resultText.getText() + ".");
            } else if (!key.equals(".")) {
                resultText.setText(resultText.getText() + key);
            }firstDigit = false;
        }
        
    
    private void handleOperator(String key) {
        if (operator.equals("/")) {
                if (getNumberFromText()==0.0) {
                    operateValidFlag = false;
                    resultText.setText("除数不能为零");
                } else {
                    resultNum /= getNumberFromText();
                }
            } else if (operator.equals("+")) {
                resultNum += getNumberFromText();
            } else if (operator.equals("-")) {
                resultNum -= getNumberFromText();
            } else if (operator.equals("*")) {
                resultNum *= getNumberFromText();
            } else if (operator.equals("+/-")) {
              resultNum = resultNum * (-1);
            } else if (operator.equals("=")) {
                resultNum = getNumberFromText();
            }
            if (operateValidFlag) {
                long t1;
                double t2;
                t1 = (long) resultNum;
                t2 = resultNum - t1;
                if (t2==0) {
                    resultText.setText(String.valueOf(t1));
                } else {
                    resultText.setText(String.valueOf(resultNum));
        }
        }
            operator = key;
            firstDigit = true;
            operateValidFlag = true;
        
        }
    private double getNumberFromText() {
        double result = 0.0;
        try {
            result = Double.valueOf(resultText.getText()).doubleValue();
        }   catch (NumberFormatException e) {
        }
        return result;
        }
        }




科学计算器
程序代码:
package demo;

import java.awt.BorderLayout;
import java.awt.CheckboxMenuItem;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
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;

 
public class calculator extends JFrame implements ActionListener {
    MenuBar mb;   
    Menu mnFile,mnEdit,mnFormat,mnHelp;  
    MenuItem miNew,miOpen,miSave,miSaveAs,miExit,miFont;
    CheckboxMenuItem miBinary;
    private final String[] KEYS = { "7", "8", "9", "/", "4", "5", "6",
            "*",  "1", "2", "3", "-", "0", ".", "=","+"};
    private final String[] COMMAND = { "←", "CE", "C" };
    private final String[] M = { "sin", "cos", "inv", "square","sqrt"};
    private JButton keys[] = new JButton[KEYS.length];
    private JButton commands[] = new JButton[COMMAND.length];
    private JButton m[] = new JButton[M.length];
    private JTextField resultText = new JTextField("0");
    private boolean firstDigit = true;
    private double resultNum = 0.0;
    private String operator = "=";
    private boolean operateValidFlag = true;
    public calculator() {
        super();

        init();

        this.setBackground(Color.LIGHT_GRAY);
        this.setTitle("计算器");

        this.setLocation(500, 300);

        this.setResizable(true);

        this.pack();
    }

 
    private void init() {

        resultText.setHorizontalAlignment(JTextField.RIGHT);

        resultText.setEditable(true);

        resultText.setBackground(Color.WHITE);

 

        JPanel calckeysPanel = new JPanel();

        calckeysPanel.setLayout(new GridLayout(4,5, 3, 3));
        for (int i = 0; i < KEYS.length; i++) {
            keys[i] = new JButton(KEYS[i]);
            calckeysPanel.add(keys[i]);
            keys[i].setForeground(Color.blue);
        }

        JPanel commandsPanel = new JPanel();

        commandsPanel.setLayout(new GridLayout(1, 3, 3, 3));
        for (int i = 0; i < COMMAND.length; i++) {
            commands[i] = new JButton(COMMAND[i]);
            commandsPanel.add(commands[i]);
            commands[i].setForeground(Color.red);
        }

 

        JPanel calmsPanel = new JPanel();

        calmsPanel.setLayout(new GridLayout(5, 1, 3, 3));
        for (int i = 0; i < M.length; i++) {
            m[i] = new JButton(M[i]);
            calmsPanel.add(m[i]);
            m[i].setForeground(Color.red);
        }

 
        JPanel panel1 = new JPanel();
     
        
        panel1.setLayout(new BorderLayout(3, 3));
        panel1.add("North", commandsPanel);
        panel1.add("Center", calckeysPanel);

 
        JPanel top = new JPanel();
        top.setLayout(new BorderLayout());
        top.add("Center", resultText);
       
        getContentPane().setLayout(new BorderLayout(3, 5));
        getContentPane().add("North", top);
        getContentPane().add("Center", panel1);
        getContentPane().add("West", calmsPanel);

        for (int i = 0; i < KEYS.length; i++) {
            keys[i].addActionListener(this);
        }
        for (int i = 0; i < COMMAND.length; i++) {
            commands[i].addActionListener(this);
        }
        for (int i = 0; i < M.length; i++) {
            m[i].addActionListener(this);
        }
    }

 
    public void actionPerformed(ActionEvent e) {
       
        String label = e.getActionCommand();
        if (label.equals(COMMAND[0])) {
           
            handleBackspace();
        } else if (label.equals(COMMAND[1])) {
          
            resultText.setText("0");
        } else if (label.equals(COMMAND[2])) {
          
            handleC();
        } else if ("0123456789.".indexOf(label) >= 0) {
         
            handleNumber(label);
        
        } else {
        
            handleOperator(label);
        }
    }

    private void handleBackspace() {
        String text = resultText.getText();
        int i = text.length();
        if (i > 0) {
            text = text.substring(0, i - 1);
            if (text.length() == 0) {
                resultText.setText("0");
                firstDigit = true;
                operator = "=";
            } else {
                resultText.setText(text);
            }
        }
    }

 
    private void handleNumber(String key) {
        if (firstDigit) {
                  resultText.setText(key);
        } else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)) {
            resultText.setText(resultText.getText() + ".");
        } else if (!key.equals(".")) {
            resultText.setText(resultText.getText() + key);
        }
        firstDigit = false;
    }
    private void handleC() {
       
        resultText.setText("0");
        firstDigit = true;
        operator = "=";
    }

 
    private void handleOperator(String key) {
        if (operator.equals("/")) {
            if (getNumberFromText() == 0.0) {
                operateValidFlag = false;
                resultText.setText("除数不能为零");
            }else {resultNum /= getNumberFromText();}
        
        }else if (operator.equals("+")) {
            
            resultNum += getNumberFromText();
            
        } else if (operator.equals("-")) {
            
            if(firstDigit){resultNum = resultNum * (-1);
               
            firstDigit=false;
            
            }else{resultNum -= getNumberFromText();}
            
        }else if (operator.equals("*")) {
           
            resultNum *= getNumberFromText();
            
        }else if (operator.equals("sqrt")) {
         
            resultNum = Math.sqrt(resultNum);
         
        }else if (operator.equals("square")) {
            
            resultNum *= resultNum;
            
        }else if (operator.equals("sin")) {
            
               resultNum = Math.sin(resultNum);
            
        }else if (operator.equals("cos")) {
               
               resultNum = Math.cos(resultNum);
            
        }else if (operator.equals("inv")&&operator.equals("cos")) {
               
               resultNum = Math.acos(resultNum);
            
        }else if (operator.equals("inv")&&operator.equals("sin")) {
               
               resultNum = Math.asin(resultNum);
            
        }else if (operator.equals("=")) {
            
            resultNum = getNumberFromText();
        }
        if (operateValidFlag) {
            long t1;
            double t2;
            t1 = (long) resultNum;
            t2 = resultNum - t1;
            if (t2 == 0) {
                resultText.setText(String.valueOf(t1));
            } else {
                resultText.setText(String.valueOf(resultNum));
            }
        }
        operator = key;
        firstDigit = true;
        operateValidFlag = true;
    }

 

    private double getNumberFromText() {
        double result = 0;
        try {
            result = Double.valueOf(resultText.getText()).doubleValue();
        } catch (NumberFormatException e) {
        }
        return result;
    }

 
    public static void main(String args[]) {
        
         calculator calculator1 = new calculator();
        calculator1.setVisible(true);
        calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }}



[此贴子已经被作者于2017-4-4 23:18编辑过]

搜索更多相关主题的帖子: 计算器 菜单栏 
2017-04-04 23:09
a3575181
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2016-12-13
收藏
得分:0 
参考网上的代码  自己写了一个   有点乱

程序代码:
package demo;
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

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

public  class combinedcaculater implements ActionListener{
private multipy c1 = new multipy();
private calculator c2 = new calculator();
JMenuBar mb = new JMenuBar();

public combinedcaculater(){
    c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    c2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mb = menu();
    c1.setMenuBar(null);   c1.setVisible(false);
    c2.setJMenuBar(mb);    c2.setVisible(true);
}



public JMenuBar menu(){
        JMenuBar jmb = new JMenuBar();
        JMenu[] jmu = { new JMenu("查看"),  new JMenu("帮助") };
        final JMenuItem[][] mi = {
                { new JMenuItem("标准型"), new JMenuItem("科学型") },    
                { new JMenuItem("提示"), new JMenuItem("关于") } };
        for (int i = 0; i < jmu.length; i++) {
            jmb.add(jmu[i]);
            for (int j = 0; j < mi.length; j++) {
                jmu[i].add(mi[i][j]);
                mi[i][j].addActionListener(
                        new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JMenuItem b = (JMenuItem) e.getSource();
                        if (b == mi[0][0]) {
                            c1.setMenuBar(null);   c1.setVisible(false);
                            c2.setJMenuBar(mb);    c2.setVisible(true);
                        } else if (b == mi[0][1]) {
                            c2.setMenuBar(null);   c2.setVisible(false);
                            c1.setJMenuBar(mb);    c1.setVisible(true);
                                
                        } else if (b == mi[1][0]) {
                            JOptionPane
                                    .showMessageDialog(
                                            null,
                                            "       仅支持单击按键输入 "
                                                    + "\n" + "         不支持键盘输入",
                                            "提示",
                                            JOptionPane.INFORMATION_MESSAGE);
                        } else if (b == mi[1][1]) {
                            JOptionPane.showMessageDialog(null, "       实验用复合型计算器 "
                                    + "\n" + "            version:1.0.0\n              (仅供参考)", "关于",
                                    JOptionPane.WARNING_MESSAGE);
                        }
                    }
                });

            }
        }
        for (int i = 0; i < jmu.length; i++) {
            jmu[i].insertSeparator(1);
        }
        return jmb;
    }

public static void main(String args[]) {
    combinedcaculater win = new combinedcaculater();
   }



}


2017-04-05 13:46
快速回复:复合计算器在菜单栏选择标准和科学型计算器
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037072 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved