2014-09-11 88 views
0

我想製作一個計算器,這裏是我的代碼到目前爲止。所以我的問題是,一旦我點擊下一個按鈕,結果會重疊。我必須從給定的答案輸入答案,如果答案是正確的,那麼它會給我一個綠色的圖像,如果它是錯誤的,它會給我一個紅色的圖像。但是,然後,當我點擊下一個...圖像重疊有時數字不會移動。請幫忙。圖片或圖像覆蓋Java

public Calculatorx() { 
     con = getContentPane(); 
     con.setLayout(null); 
     JLabel subTitle = new JLabel(" Elementary Math Tutor "); 
     con.add(subTitle); 
     subTitle.setBounds(150, 1, 200, 70); 
     rand1 = 1 + (int) (Math.random() * 9); 
     rand2 = 1 + (int) (Math.random() * rand1); 
     while (rand1 % rand2 != 0) { 
      rand2 = 1 + (int) (Math.random() * rand1); 
     } 
     jbtadd1 = new JButton("" + rand1); 
     jbtsub1 = new JButton("" + rand1); 
     jbtmul1 = new JButton("" + rand1); 
     jbtdiv1 = new JButton("" + rand1); 
     con.add(jbtadd1); 
     con.add(jbtsub1); 
     con.add(jbtmul1); 
     con.add(jbtdiv1); 
     jbtadd1.setBounds(50, 50, 50, 50); 
     jbtsub1.setBounds(50, 120, 50, 50); 
     jbtmul1.setBounds(50, 190, 50, 50); 
     jbtdiv1.setBounds(50, 260, 50, 50); 
     JLabel plus = new JLabel(plusIcon); 
     JLabel minus = new JLabel(minusIcon); 
     JLabel mul = new JLabel(mulIcon); 
     JLabel div = new JLabel(divIcon); 
     con.add(plus); 
     con.add(minus); 
     con.add(mul); 
     con.add(div); 
     plus.setBounds(100, 50, 150, 50); 
     minus.setBounds(80, 120, 200, 50); 
     mul.setBounds(60, 190, 250, 50); 
     div.setBounds(40, 260, 300, 50); 
     jbtadd2 = new JButton("" + rand2); 
     jbtsub2 = new JButton("" + rand2); 
     jbtmul2 = new JButton("" + rand2); 
     jbtdiv2 = new JButton("" + rand2); 
     con.add(jbtadd2); 
     con.add(jbtsub2); 
     con.add(jbtmul2); 
     con.add(jbtdiv2); 
     jbtadd2.setBounds(250, 50, 50, 50); 
     jbtsub2.setBounds(250, 120, 50, 50); 
     jbtmul2.setBounds(250, 190, 50, 50); 
     jbtdiv2.setBounds(250, 260, 50, 50); 
     JLabel equal1 = new JLabel(equalIcon); 
     JLabel equal2 = new JLabel(equalIcon); 
     JLabel equal3 = new JLabel(equalIcon); 
     JLabel equal4 = new JLabel(equalIcon); 
     con.add(equal1); 
     con.add(equal2); 
     con.add(equal3); 
     con.add(equal4); 
     equal1.setBounds(330, 40, 50, 50); 
     equal2.setBounds(330, 110, 50, 50); 
     equal3.setBounds(330, 180, 50, 50); 
     equal4.setBounds(330, 250, 50, 50); 
     addAnswer = new JTextField(); 
     subAnswer = new JTextField(); 
     mulAnswer = new JTextField(); 
     divAnswer = new JTextField(); 
     con.add(addAnswer); 
     con.add(subAnswer); 
     con.add(mulAnswer); 
     con.add(divAnswer); 

     addAnswer.setBounds(410, 50, 50, 50); 
     subAnswer.setBounds(410, 120, 50, 50); 
     mulAnswer.setBounds(410, 190, 50, 50); 
     divAnswer.setBounds(410, 260, 50, 50); 
     JButton Check = new JButton("CHECK"); 
     JButton Next = new JButton("NEXT"); 
     JButton Exit = new JButton("EXIT"); 
     con.add(Check); 
     con.add(Next); 
     con.add(Exit); 
     Check.setBounds(235, 330, 75, 30); 
     Next.setBounds(320, 330, 65, 30); 
     Exit.setBounds(395, 330, 65, 30); 



} 

回答

0

首先,您的下一個動作偵聽器會拋出NullPointerException,因爲4個操作數變量未啓動。請您Calculatorx構造函數的開頭添加以下代碼:

operand1 = new JLabel(); 
operand2 = new JLabel(); 
operand3 = new JLabel(); 
operand4 = new JLabel(); 

爲了解決您的重疊圖標的問題加上下面這一行的檢查措施監聽器的actionPerformed方法開頭:

Check.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      con.repaint();    
      .... 

也可能不是需要以下2行:

con.add(operand4); 
operand4.setBounds(520, 260, 50, 50); 

作爲一般的建議,您應該總是使用對象小寫字母名稱。只有類名以大寫字母(以及常量)開頭

+0

con.repaint()是什麼意思;? – user3767918 2014-09-11 15:23:03

+0

它告訴容器** con **重新繪製自己。在actionPeformed方法中的其他代碼從容器中「清除」之前顯示的圖像之前執行它,並且不會有更多重疊。 – 2014-09-11 15:57:31