1

我的计算器有点问题。主“GUI”有数字键盘和一些运算符,但我为所有三角函数添加了一个 JMenuItem,因为它们在新窗口中打开。我在主 GUI 上使用运算符(+、-、sqrt、power 等)没有问题,但我似乎根本无法让三角函数工作。我在代码中的注释中添加了更多信息(阅读名为 t 的第三个最后一个实例变量后面的注释)。

public class Calculator extends JFrame
{
    //TILVIKSBREYTUR    
    JFrame rammi;                //Rammi inniheldur panel
    JPanel pnl;                  //Panel inniheldur takka 
    private final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 20); //Letur
    public JTextField txt;       //Textareitur
    private JButton b0;          //b0 - b9 eru númeratakkar
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b4;
    private JButton b5;
    private JButton b6;
    private JButton b7;
    private JButton b8;
    private JButton b9;
    private JButton bDecPoint;   //Decimal point
    private JButton bc;          //Clear takki, endurstillir textareit
    private JButton bPlus;       //Plús takki
    private JButton bMinus;      //Mínus takki
    private JButton bPower;      //Veldatakki
    private JButton bSqrt;       //Kvaðratrótartakki
    private JButton bDivide;     //Deilingar takki
    private JButton bMult;       //Margföldunar takki
    private JButton bxRoot;       //N-tu rótartakki
    private JButton bEqual;      //Jafnt og takki
    private JButton bPer;        //prósent
    private JButton bAns;        //síðasta útkoma
    private JButton bPi;         //Pi
    private JButton bSvigi1;     //Svigi
    private JButton bSvigi2;     //Svigi
    private JSeparator sep1;     //Separator, skilur að textareit og lyklaborð
    private JSeparator sep2;     //Separatorm skilur að númer og virkja
    public  boolean number;      //tala
    public  boolean degPressed;  //Er Degrees valið ? 
    public  boolean trigOpen;    //Er Trigonmetry flipinn opinn?
    public  boolean trigOpenOp;  //hjálpar breyta fyrir virkni
    public  String  equalOp;     //Strengur fyrir virkja
    public  String ans;
    public  String ansOp;        //Strengur
    private JMenuBar menuBar;    //Menu bar
    private JMenu file;          //Flipi í menu bar
    private JMenuItem trigon;    //Hornaföll
    private JRadioButtonMenuItem deg;  //undirgluggi flipa í menubar
    private JRadioButtonMenuItem rad;  //undirgluggi flipa í menubar

    private Trigonmetry t;      //if I do: private Trigonmetry t = new Trigonmetry then it works
                                //perfectly but if I do that then the Trigonmetry window opens up 
                                //when I open the Calculator but I just want Trigonmetry window
                                //to open up when Trigonmetry MenuItem i pressed 

    private CalculatorOp op = new CalculatorOp();  //CalculatorOp classi sem sér um virkni virkjanna


    public Calculator(){
        initUI();
    }

    public final void initUI(){
        //TILVIKSBREYTUM GEFID GILDI
        rammi = new JFrame(); 
        degPressed = false;
        trigOpen   = false;
        trigOpenOp = false;
        trigon = new JMenuItem("Trigonmetry");
        pnl = new JPanel();
        txt = new JTextField("0.0");
        menuBar = new JMenuBar();
        file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);
        deg = new JRadioButtonMenuItem("Degrees");
        rad = new JRadioButtonMenuItem("Radians", true);
        number = true;
        equalOp = "=";
        ansOp   = "ANS";
        sep1 = new JSeparator();
        sep2 = new JSeparator(SwingConstants.VERTICAL);
        b0 = new JButton("0");
        b1 = new JButton("1");
        b2 = new JButton("2");
        b3 = new JButton("3");
        b4 = new JButton("4");
        b5 = new JButton("5");
        b6 = new JButton("6");
        b7 = new JButton("7");
        b8 = new JButton("8");
        b9 = new JButton("9");
        bc = new JButton("C");
        bPer = new JButton("%");
        bAns = new JButton("ANS");
        bDecPoint = new JButton(".");
        bPlus = new JButton("+");
        bMinus = new JButton("-");
        bPower = new JButton("x^n");
        bSqrt = new JButton("√x");
        bxRoot = new JButton("n√x");
        bDivide = new JButton("/");
        bMult = new JButton("*");
        bEqual = new JButton("=");
        //bSvigi1 = new JButton("(");
        //bSvigi2 = new JButton(")");

        //HÖNNUN RAMMA
        setTitle("GCalc");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(370, 280));

        //STADSETNING HLUTA
        txt.setHorizontalAlignment(JTextField.RIGHT);
        txt.setFont(BIGGER_FONT);
        pnl.setLayout(null);
        //tölur og clear
        bc.setBounds(10, 10, 50, 30);
        pnl.add(bc);
        bDecPoint.setBounds(10, 190, 50, 30);
        pnl.add(bDecPoint);
        b0.setBounds(70, 190, 50, 30);
        pnl.add(b0);
        b1.setBounds(10, 150, 50, 30);
        pnl.add(b1);
        b2.setBounds(70, 150, 50, 30);
        pnl.add(b2);
        b3.setBounds(130, 150, 50, 30);
        pnl.add(b3);
        b4.setBounds(10, 110, 50, 30);
        pnl.add(b4);
        b5.setBounds(70, 110, 50, 30);
        pnl.add(b5);
        b6.setBounds(130, 110, 50, 30);
        pnl.add(b6);
        b7.setBounds(10, 70, 50, 30);
        pnl.add(b7);
        b8.setBounds(70, 70, 50, 30);
        pnl.add(b8);
        b9.setBounds(130, 70, 50, 30);
        pnl.add(b9);
        //virkjar
        bAns.setBorder(null);
        bAns.setBounds(130, 190, 50, 30);
        pnl.add(bAns);
        bPlus.setBorder(null);
        bPlus.setBounds(200, 70, 30, 30);
        pnl.add(bPlus);
        bMinus.setBorder(null);
        bMinus.setBounds(240, 70, 30, 30);
        pnl.add(bMinus);
        bSqrt.setBorder(null);
        bSqrt.setBounds(240, 150, 30, 30);
        pnl.add(bSqrt);
        bPower.setBorder(null);
        bPower.setBounds(200, 150, 30, 30);
        pnl.add(bPower);
        bDivide.setBorder(null);
        bDivide.setBounds(200, 110, 30, 30);
        pnl.add(bDivide);
        bMult.setBorder(null);
        bMult.setBounds(240, 110, 30, 30);
        pnl.add(bMult);
        bxRoot.setBorder(null);
        bxRoot.setBounds(280, 70, 30, 30);
        pnl.add(bxRoot);
        bPer.setBorder(null);
        bPer.setBounds(320, 70, 30, 30);
        pnl.add(bPer);
        //bSvigi1.setBorder(null);
        //bSvigi1.setBounds(280, 110, 30, 30);
        //pnl.add(bSvigi1);
        //bSvigi2.setBorder(null);
        //bSvigi2.setBounds(320, 110, 30, 30);
        //pnl.add(bSvigi2);
        bEqual.setBounds(200, 190, 70, 30);
        pnl.add(bEqual);
        //seperators & textfield
        sep1.setBounds(20, 50, 330, 10);
        pnl.add(sep1);
        sep2.setBounds(190, 60, 10, 160);
        pnl.add(sep2);
        txt.setBounds(70, 10, 280, 30);
        pnl.add(txt);
        add(pnl);

        //Setjum inní Menu
        file.add(deg);
        file.add(rad);
        file.add(trigon);
        menuBar.add(file);
        getContentPane().add(menuBar, BorderLayout.NORTH);

        //HLUTIR SYNILEGIR
        setVisible(true);

        //NUMBER LISTENER
        ActionListener numberListener = new NumberListener();
        b0.addActionListener(numberListener);
        b1.addActionListener(numberListener);
        b2.addActionListener(numberListener);
        b3.addActionListener(numberListener);
        b4.addActionListener(numberListener);
        b5.addActionListener(numberListener);
        b6.addActionListener(numberListener);
        b7.addActionListener(numberListener);
        b8.addActionListener(numberListener);
        b9.addActionListener(numberListener);
        bDecPoint.addActionListener(numberListener);
        //bSvigi1.addActionListener(numberListener);
        //bSvigi2.addActionListener(numberListener);

        //OPERATOR LISTENER
        ActionListener operatorListener = new OperatorListener();
        bPlus.addActionListener(operatorListener);
        bMinus.addActionListener(operatorListener);
        bDivide.addActionListener(operatorListener);
        bMult.addActionListener(operatorListener);
        bSqrt.addActionListener(operatorListener);
        bPower.addActionListener(operatorListener);
        bEqual.addActionListener(operatorListener);
        bxRoot.addActionListener(operatorListener);
        bPer.addActionListener(operatorListener);
        if(trigOpen){
        t.bCos.addActionListener(operatorListener); 
        }
        //ANS LISTENER
        ActionListener ansListener = new AnsListener();
        bAns.addActionListener(ansListener);

        //CLEAR LISTENER
        ActionListener clearListener = new ClearListener();
        bc.addActionListener(clearListener);

        //Menu LISTENER
        ActionListener menuListener = new MenuListener();
        trigon.addActionListener(menuListener);

        //RadioButton LISTENER
        ActionListener radioListener = new RadioListener();
        deg.addActionListener(radioListener);
        //RadioButton LISTENER 2
        ActionListener radioListener2 = new RadioListener2();
        rad.addActionListener(radioListener2);


    }
    /**E: Núllstillir
    private void action() {
        number = true; 
        txt.setText("0.0");
        equalOp  = "=";
        op.setTotal("0.0");
    }  */

    //VIRKNI RADIO TAKKANS SEM BREYTIR YFIR Í GRÁÐUR
    class RadioListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            degPressed = true;
        }
    }

    //VIRKNI RADIO TAKKANS SEM BREYTIR YFIR Í RADÍANA
    class RadioListener2 implements ActionListener{
        public void actionPerformed(ActionEvent e){
            degPressed = false;
        }
    }

    //VIRKNI CLEAR TAKKANS
    class ClearListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            txt.setText("0.0");
        }
    }


    //VIRKNI TRIGONMETRY FLIPA
    class MenuListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            t = new Trigonmetry();
            trigOpen = true;
        }
    }

    //VIRKNI ANS TAKKA
    class AnsListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            ansOp = e.getActionCommand();
            if (ansOp.equals("ANS")) {
                txt.setText(ans);
            }
            else
                txt.setText("0.0");
        }
    }


    //VIRKNI TALNA TAKKA
    class NumberListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            String digit = event.getActionCommand(); 
            if (number){
                txt.setText(digit);
                number = false;
            } 
            else{
                txt.setText(txt.getText() + digit);
            }
        }
    }   

    // VIRKNI "VIRKJA"
    class OperatorListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if (number){
                //action();
                txt.setText("0.0");
            }
            else{
                number = true; 
                String displayText = txt.getText();
                if (equalOp.equals("=")) {
                op.setTotal(displayText);
                } 
                else if (equalOp.equals("+")) {
                    op.add(displayText);
                } 
                else if (equalOp.equals("-")) {
                    op.subtract(displayText);
                } 
                else if (equalOp.equals("*")) {
                    op.multiply(displayText);
                } 
                else if (equalOp.equals("/")) {
                    op.divide(displayText);
                }
                else if(equalOp.equals("x^n")){
                    op.power(displayText);
                }
                else if(equalOp.equals("√x")){
                    op.squareRoot(displayText);
                }
                else if(equalOp.equals("n√x")){
                    op.xRoot(displayText);
                }
                else if(equalOp.equals("%")){
                    op.perCent(displayText);
                }
            //  else if(t.operand.equals("cos")){
                //  if(degPressed){
                    //  op.cOsDeg(displayText);
                    //}
                    //else
                    //  op.cOs(displayText);
                //}
                //else if(equalOp.equals("sin")){
                //  op.sIn(displayText);
                //}
                //else if(equalOp.equals("tan")){
                //  op.tAn(displayText);
                //}
                txt.setText("" + op.getTotalString());
                ans = txt.getText();
                equalOp = e.getActionCommand();
                //t.operand = e.getActionCommand();

            }
        }
    }   

    //*************** MAIN ***************//
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                Calculator calc = new Calculator();
            }
        });
    }
}

这是我的 Tigonmetry 类,如您所见,我也尝试在那里设置一个 actionListener

public class Trigonmetry extends JFrame
{
    JFrame frm;                  //rammi
    public JButton bCos;         //Cosinus
    public JButton bSin;         //Sinus
    public JButton bTan;         //Tangens
    public JButton baCos;        //arc Cosinus
    public JButton baSin;        //arc Sinus
    public JButton baTan;        //arc Tangens
    private JPanel panel;        //Panel
    public String operand;
    public Calculator cal;
    private CalculatorOp opp = new CalculatorOp();

    public Trigonmetry(){
        InitialUI();
    }

    public final void InitialUI(){
        frm = new JFrame(); 
        operand = "";
        bCos = new JButton("cos");
        bSin = new JButton("sin");
        bTan = new JButton("tan");
        baCos = new JButton("acos");
        baSin = new JButton("asin");
        baTan = new JButton("atan");
        panel = new JPanel();

        //HÖNNUN RAMMA
        setTitle("Trigonmetry");
        setLocationRelativeTo(null);
        setMinimumSize(new Dimension(180, 110));
        panel.setLayout(null);

        //TAKKAR
        bSin.setBorder(null);
        bSin.setBounds(20, 10, 40, 30);
        panel.add(bSin);
        bCos.setBorder(null);
        bCos.setBounds(70, 10, 40, 30);
        panel.add(bCos);
        bTan.setBorder(null);
        bTan.setBounds(120, 10, 40, 30);
        panel.add(bTan);
        baCos.setBorder(null);
        baCos.setBounds(20, 50, 40, 30);
        panel.add(baCos);
        baSin.setBorder(null);
        baSin.setBounds(70, 50, 40, 30);
        panel.add(baSin);
        baTan.setBorder(null);
        baTan.setBounds(120, 50, 40, 30);
        panel.add(baTan);
        add(panel);


        //HLUTIR SYNILEGIR
        setVisible(true);

        //OPERATORLISTENER
        ActionListener operandListener = new OperandListener();
        bCos.addActionListener(operandListener);
        bSin.addActionListener(operandListener);
        bTan.addActionListener(operandListener);
    }
        // VIRKNI "VIRKJA"
    class OperandListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if (cal.number){
                //action();
                cal.txt.setText("0.0");
            }
            else{
                cal.number = true; 
                String displayText1 = cal.txt.getText();
                if (cal.equalOp.equals("=")) {
                opp.setTotal(displayText1);
                } 
                else if(operand.equals("cos")){
                    if(cal.degPressed){
                        opp.cOsDeg(displayText1);
                    }
                    else
                        opp.cOs(displayText1);
                }
                else if(operand.equals("sin")){
                    if(cal.degPressed){
                        opp.sInDeg(displayText1);
                    }
                    else
                        opp.sIn(displayText1);
                }
                else if(operand.equals("tan")){
                    if(cal.degPressed){
                        opp.tAnDeg(displayText1);
                    }
                    else
                        opp.tAn(displayText1);
                }
                cal.txt.setText("" + opp.getTotalString());
                operand = e.getActionCommand();
            }
        }
    }   


}
4

1 回答 1

4

Trigonometry 的 Calculator 变量 cal 为空,因为它从未使用当前的 Calculator 实例初始化。您需要将 Calculator 实例放入 Trigonometry 类,以便 Trig 类可以调用 Calculator 的公共方法。一种方法是通过构造函数参数,另一种方法是通过 setter 方法,例如setCalculator(Calculator calculator) {...}. 构造函数参数技术的示例:

在 Calculator.java 中:

Trigonometry t = new Trigonometry(this);

在三角函数类中:

// constructor now accepts a Calculator parameter
public Trigonmetry(Calculator calculator) {
  // initialize the cal field with the current Calculator instance
  cal = calculator; 
  InitialUI();
}

还:

  • setBounds(...)根据 camickr 的建议,不要使用和空布局。
  • 而是阅读布局管理器的使用,然后使用嵌套的 JPanel,每个 JPanel 都使用适当的布局管理器。
  • 不要使用多个 JFrame。第二个 Trig 窗口应该是一个 JDialog(也是根据 camickr 的建议)。
  • 学习和使用 Java 编码约定。类名以大写字母开头,方法和变量名以小写字母开头。
  • 在这里提出问题时,请尝试将您的代码削减到显示您的问题的最低限度,仍然允许您的代码编译和运行,但不包含与您的问题无关的代码。上面发布的 95% 的当前代码对于解决问题和分散我们的注意力都是不必要的。

还:

  • 如果您只希望 Trigonometry 实例在 Calculator 的控制下打开,那么没有代码显示它由 Trigonometry 调用,而是由 Calculator 调用。换句话说,不要setVisible(true)对自身进行 Trigonometry 调用,而是让 Calculator 在 Trigonometry 实例上调用它。

在计算器中:

class MenuListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
     t.setVisible(true);
     trigOpen = true;
  }
}

在三角学中:

public final void InitialUI() { // this should be named initialUi()

  // ..... code deleted for brevity

  panel.add(baTan);
  add(panel);
  // !!  setVisible(true); // *** don't call this here ***
  ActionListener operandListener = new OperandListener();

  // ..... code deleted for brevity

}
于 2013-04-06T15:38:02.867 回答