2013-04-06 19 views
1

我的計算器有點問題。主「GUI」具有數字鍵盤和一些操作符,但是我爲所有三角函數添加了JMenuItem,就像它們在新窗口中打開一樣。我在主GUI(+, - ,sqrt,power等)上使用運算符沒有問題,但我似乎無法使Trigonmetric函數正常工作。我在我的代碼中註釋了更多信息(讀取第三個最後一個實例變量t後面的註釋)。計算器:不能在我的主GUI的彈出窗口中使用ActionListener

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


} 
+2

太多的代碼閱讀。不要使用'setBounds()'來定位和調整組件的大小。使用佈局管理器。另外,一個彈出窗口應該是一個JDialog,而不是一個JFrame。如果你解決了這兩個問題,那麼我可能會在稍後看看這個問題。 – camickr 2013-04-06 15:33:06

回答

4

三角函數的計算器變量,CAL,爲null,因爲它從來沒有與當前的初始化計算器實例。您需要將Calculator實例放入Trigonometry類,以便Trig類可以調用Calculator的公共方法。一種方法是通過構造函數參數,另一種方法是通過設置方法,如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的建議,爲null佈局。
  • 改爲閱讀使用佈局管理器,然後使用嵌套的JPanel,每個JPanel使用適當的佈局管理器。
  • 請勿使用多個JFrame。第二個Trig窗口應該是一個JDialog(也是每個camickr的建議)。
  • 學習和使用Java編碼約定。類名以大寫字母和方法開頭,變量名以小寫字母開頭。
  • 在這裏提出問題時,儘量將代碼削減到顯示問題的最低限度,仍然允許您的代碼編譯和運行,但不包含與您的問題無關的代碼。上面提到的當前代碼中的95%對於解決問題和分散注意力是不必要的。

另外:

  • 如果你只希望三角實例計算器的控制下打開,再沒有代碼來顯示它由三角而是由計算器調用。換句話說,不要讓三角函數本身調用setVisible(true),而是讓Calculator在三角函數實例上調用它。

在計算器:

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 

} 
+0

謝謝你的工作 – gthb7 2013-04-06 15:49:13

+1

@ user1946287:不客氣,但下次,請減少*無關*代碼。如果您遇到類似問題,請嘗試創建併發布[sscce](http://sscce.org)。 – 2013-04-06 15:50:10

+1

很好地解釋了+1 – 2013-04-06 16:00:03

相關問題