【问题标题】:Changing forms between standard calculator and scientific with menu items in Java使用 Java 中的菜单项在标准计算器和科学计算器之间更改表格
【发布时间】:2012-04-23 19:12:54
【问题描述】:

我有一个标准计算器和一个用java创建的科学计算器,都有菜单栏和菜单项“标准”和“科学”在两者之间切换,但我不知道如何真正拥有计算器单击选项时更改。

到目前为止的代码:

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

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class regCalc implements ActionListener 
{    
    JFrame frame = new JFrame ("Standard Calculator");

    //JPanels for standard and scientific
    JPanel panelstand = new JPanel (new FlowLayout ()); 
    JPanel panelsci = new JPanel (new FlowLayout());

    JMenuBar menuBar = new JMenuBar();

    JMenu view = new JMenu ("View");
    JMenuItem standard = new JMenuItem("Standard");
    JMenuItem scientific = new JMenuItem("Scientific");

    JTextArea text = new JTextArea (1,20);

    JButton but1 = new JButton ("1");
    JButton but2 = new JButton ("2");
    JButton but3 = new JButton ("3");
    JButton but4 = new JButton ("4");
    JButton but5 = new JButton ("5");
    JButton but6 = new JButton ("6");
    JButton but7 = new JButton ("7");
    JButton but8 = new JButton ("8");
    JButton but9 = new JButton ("9");
    JButton but0 = new JButton ("0");

    JButton butAdd = new JButton ("+");
    JButton butSub = new JButton ("-");
    JButton butMult = new JButton ("*");
    JButton butDiv = new JButton ("/");
    JButton butEq = new JButton ("=");
    JButton butClear = new JButton ("C");

    Double number1, number2, result;
    int addc =0, subc=0, multc=0, divc=0;

    public void ui()
    {
    frame.setVisible(true);

    panelstand.setVisible(true);
    panelsci.setVisible(false);

    frame.setSize(270,230);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setJMenuBar(menuBar);
    view.add(standard);
    view.add(scientific);
    menuBar.add(view);

    frame.add(panelstand);

    panelstand.add(text);

    panelstand.add(but1);
    panelstand.add(but2);
    panelstand.add(but3);
    panelstand.add(but4);
    panelstand.add(but5);
    panelstand.add(but6);
    panelstand.add(but7);
    panelstand.add(but8);
    panelstand.add(but9);
    panelstand.add(but0);

    panelstand.add(butAdd);
    panelstand.add(butSub);
    panelstand.add(butMult);
    panelstand.add(butDiv);
    panelstand.add(butEq);
    panelstand.add(butClear);

    but1.addActionListener(this);
    but2.addActionListener(this);
    but3.addActionListener(this);
    but4.addActionListener(this);
    but5.addActionListener(this);
    but6.addActionListener(this);
    but7.addActionListener(this);
    but8.addActionListener(this);
    but9.addActionListener(this);
    but0.addActionListener(this);

    butAdd.addActionListener(this);
    butSub.addActionListener(this);
    butMult.addActionListener(this);
    butDiv.addActionListener(this);
    butEq.addActionListener(this);
    butClear.addActionListener(this);   
}

    @Override
    public void actionPerformed(ActionEvent e) {

        Object source = e.getSource();

    if (source == but1)
    {
        text.append("1");
    }

    if (source == but2)
    {
        text.append("2");
    }

    if (source == but3)
    {
        text.append("3");
    }

    if (source == but4)
    {
        text.append("4");
    }

    if (source == but5)
    {
        text.append("5");
    }

    if (source == but6)
    {
        text.append("6");
    }

    if (source == but7)
    {
        text.append("7");
    }

    if (source == but8)
    {
        text.append("8");
    }

    if (source == but9)
    {
        text.append("9");
    }

    if (source == but0)
    {
        text.append("0");
    }

    if (source == butAdd)
    {
        number1=number_Reader();
        text.setText("");
        addc=1;
        subc=0;
        multc=0;
        divc=0;
    }

    if (source == butSub)
    {
        number1=number_Reader();
        text.setText("");
        addc=0;
        subc=1;
        multc=0;
        divc=0;
    }

    if (source == butMult)
    {
        number1=number_Reader();
        text.setText("");
        addc=0;
        subc=0;
        multc=1;
        divc=0;
    }

    if (source == butDiv)
    {
        number1=number_Reader();
        text.setText("");
        addc=0;
        subc=0;
        multc=0;
        divc=1;
    }

    if (source == butEq)
    {
        number2=number_Reader();
        if (addc > 0)
        {
            result = number1+number2;
            text.setText(Double.toString(result));
        }

        if (subc > 0)
        {
            result = number1-number2;
            text.setText(Double.toString(result));
        }

        if (multc > 0)
        {
            result = number1*number2;
            text.setText(Double.toString(result));
        }

        if (divc > 0)
        {
            result = number1/number2;
            text.setText(Double.toString(result));
        }
    }

    if (source == butClear)
    {
        number1=0.0;
        number2=0.0;
        text.setText("");
    }

    if (view == scientific)
    {
        frame.add(scientific);
    }

}

    public double number_Reader()
    {
        double num1;
        String s;
        s=text.getText();
        num1=Double.valueOf(s);
        return num1;
    }
}

【问题讨论】:

    标签: java swing calculator


    【解决方案1】:

    您可能正在寻找诸如CardLayout 之类的东西,它使您能够使用布局在两个面板之间切换。

    【讨论】:

      【解决方案2】:

      实际上,虽然我是 CardLayout 的大力推动者,但我希望您考虑另一种选择。如果您考虑这个问题,您会发现(至少在我看来)您不想在切换计算器类型时完全更改视图,而是希望增加或减少视图,因为标准计算器实际上只是科学计算器的一个子集。因此,如果需要科学计算器,您可以简单地显示一个带有科学按钮的附加 JPanel,并在需要标准计算器时隐藏 JPanel。这可以通过将 JPanel 添加到适当的布局来完成,例如使用标准 JPanel 添加 BorderLayout.CENTER 和科学 JPanel BorderLayout.PAGE_END 的 BorderLayout,然后在科学 JPanel 上调用 setVisible(true) 或 false。确保在装有科学计算器的 JPanel 上调用 repaint()

      【讨论】:

      • 好建议。我刚刚在标准的 OS X 计算器上尝试过它,它确实做到了(直到你切换到程序员模式......)。我唯一要更改的是在 NORTH 中添加显示(显示结果的位置),以便在切换到科学模式时显示跨越两个面板
      • 是的,正确的。尽管@berry 的答案是一个非常好的答案,也是一个有效的解决方案。 1+ 给他。
      • 非常感谢!如果我还有其他问题,我会告诉大家。
      • 你知道我会合并这些代码吗?我将它们分别创建为两个单独的类
      • @dannyh:如果没有看到您的代码或不了解您的程序是如何设置的,很难说任何话。希望您没有扩展 JFrame 的类——这就是为什么这不是一件好事的一个例子。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      • 2017-11-05
      • 2019-08-31
      • 1970-01-01
      相关资源
      最近更新 更多