2

標準の電卓とJavaで作成された関数電卓があり、どちらもメニューバーと、2つを切り替えるための「標準」と「科学」のメニュー項目がありますが、実際に電卓を変更する方法がわかりません。オプションをクリックします。

これまでのコード:

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;
    }
}
4

2 に答える 2

5

CardLayoutなど、レイアウトを使用して2つのパネルを切り替えることができるものを探している可能性があります。

于 2012-04-23T19:14:49.753 に答える
3

実際、私は CardLayout の推進者ですが、別の方法を検討していただきたいと思います。この問題について考えてみると、標準の電卓は実際には単なる関数電卓のサブセット。したがって、科学計算機が必要な場合は科学ボタンを表示して追加の JPanel を表示し、標準の計算機が必要な場合は JPanel を非表示にするだけです。これは、JPanel を適切なレイアウトに追加することで実行できます。たとえば、標準の JPanel に BorderLayout.CENTER を追加した BorderLayout と、科学的な JPanel の BorderLayout.PAGE_END を追加し、呼び出します。setVisible(true)または科学的な JPanel では false です。repaint()関数電卓を保持する JPanelを呼び出すようにしてください。

于 2012-04-23T21:37:38.997 に答える