2014-04-03 142 views
-1

我剛剛在Java中製作一個GUI計算器,我一直在使用eclipse,事情是我沒有得到任何語法錯誤,但是當我編譯時,我得到了一些編譯錯誤,弄清楚爲什麼。這些是我收到的錯誤。計算器編譯問題

Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint 
at java.awt.GridBagLayout.addLayoutComponent(Unknown Source) 

at java.awt.Container.addImpl(Unknown Source) 

at java.awt.Container.add(Unknown Source) 

at javax.swing.JFrame.addImpl(Unknown Source) 

at java.awt.Container.add(Unknown Source) 

at Calculator1.<init>(Calculator1.java:81) 

at Calculator1.main(Calculator1.java:178) 

這是我的源代碼:

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

public class Calculator1 extends JFrame{ 


    JButton add, subtract, multiply, division; //These are going to be all the buttons 
    JTextField num1, num2; //All the fields I need 
    JLabel result, enter1, enter2; //The labels 

    public Calculator1(){ //Starting of constructor class 

     setLayout(new GridBagLayout()); //Declaring I'm going to be using the GridBagLayout 
     GridBagConstraints c = new GridBagConstraints(); //Making a GridBagLayout Object 

     enter1 = new JLabel("1st :"); //New JLabel with 1st being put into enter1 
     c.fill = GridBagConstraints.HORIZONTAL; //Will set the size 
     c.gridx = 0; //Position x 
     c.gridy = 0; //Position y 
     add(enter1, c); //Add options to our object c 

     num1 = new JTextField(10); //How big can user enter a value upto 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 0; 
     c.gridwidth = 3; 
     add(num1, c); 

     enter2 = new JLabel("2nd :"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 1; 
     c.gridheight = 1; 
     add(enter2, c); 

     num2 = new JTextField(10); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 1; 
     c.gridwidth = 3; 
     add(num2, c); 

     add = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 2; 
     c.gridwidth = 1; 
     add(add, c); 

     subtract = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 2; 
     c.gridwidth = 1; 
     add(subtract, c); 

     multiply = new JButton("*"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 2; 
     c.gridwidth = 1; 
     add(multiply, c); 

     division = new JButton("/"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 2; 
     c.gridwidth = 1; 
     add(division, c); 

     result = new JLabel(""); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 4; 
     c.gridwidth = 4; 
     add(result, add); 

     event a = new event(); 
     add.addActionListener(a); //If user clicks add on the button, it should load the adding function from event class 
     subtract.addActionListener(a); 
     multiply.addActionListener(a); 
     division.addActionListener(a); 

    } 

    public class event implements ActionListener{ 


     public void actionPerformed(ActionEvent a) { //This will be used to do what the user wants 

      double number1, number2; 

      try{ 

       number1 = Double.parseDouble(num1.getText()); //This will convert num1 into a double 

      } catch(NumberFormatException e){ //If user enters a value that's not a double such as a String, it will display this message 

       result.setText("Illegal Data for first field"); 
       result.setForeground(Color.red); 
       return; 

      } 

      try{ 

       number2 = Double.parseDouble(num2.getText()); 

      } catch(NumberFormatException e){ 

       result.setText("Illegal Data for second field"); 
       result.setForeground(Color.red); 
       return; 

      } 

      String op = a.getActionCommand(); //Put whatever the user clicks into op, op is just a name 

      if(op.equals("+")){ 

       double sum = number1 + number2; 

       result.setText(number1 + "+" + number2 + "=" + sum); 
       result.setForeground(Color.red); 


      } 
      else if(op.equals("-")){ 

       double diff = number1-number2; 

       result.setText(number1 + "-" + number2 + "=" + diff); 
       result.setForeground(Color.red); 


      } 
      else if(op.equals("*")){ 

       double times = number1*number2; 

       result.setText(number1 + "*" + number2 + "=" + times); 
       result.setForeground(Color.red); 


      } 
      else if(op.equals("/")){ 

      if(number2 == 0){ 

       result.setText("Cannot divide by 0 you dumb shit"); 
       result.setForeground(Color.red); 
      } 

      else{ 

       double divide = number1/number2; 

       result.setText(number1 + "/" + number2 + "=" + divide); 
       result.setForeground(Color.red); 

      } 

      } 

     } 



    } 

    public static void main(String args[]){ 

     Calculator1 Gui = new Calculator1(); //Creating our calculator Object 
     Gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Gui.setVisible(true); 
     Gui.setSize(250, 175); 
     Gui.setTitle("My Java calculator"); 
    } 


} 
+0

您是否只能包含相關代碼的各個部分? –

+3

這不是一個編譯錯誤......它是一個運行時異常。 _「不能添加到佈局:約束必須是GridBagConstraint」_聽起來很明顯。 –

+1

在發佈之前,您應該先嚐試製作[最小,完整,可驗證的示例](http:// stackoverflow.com/help/mcve)。 – AJMansfield

回答

1

的錯誤是一個運行而不是編譯錯誤 - 變量add是一個Swing組件,而不是預期的佈局管理約束。在添加result組件時使用以下內容

add(result, c); 
+0

謝謝,我看不到那個愚蠢的錯誤,我很匆忙,但我無法注意到,謝謝。它現在工作:)。我打算以最佳答案投票 – DonTahmid