2014-05-01 133 views
1

試圖編寫一個簡單的程序來顯示用按鈕鍵入的電話號碼。每按一次按鈕,我都會設置一個計數器,以在第3和第6個數字輸入後自動插入「 - 」。我的計數器似乎沒有按鈕按下更新。我看不出爲什麼。這可能很簡單,但我非常感謝任何幫助。按鈕計數器不增加

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

public class BorderPanel extends JPanel 
{ 

    //variables 
    int counter = 0; 
    int total; 
    Object source = new Object(); 
    String display = ""; 
    String s; 

     //buttons 
    JButton b1 = new JButton("1"); 
    JButton b2 = new JButton("2"); 
    JButton b3 = new JButton("3"); 
    JButton b4 = new JButton("4"); 
    JButton b5 = new JButton("5"); 
    JButton b6 = new JButton("6"); 
    JButton b7 = new JButton("7"); 
    JButton b8 = new JButton("8"); 
    JButton b9 = new JButton("9"); 
    JButton b0 = new JButton("0"); 
    JButton bstar = new JButton("*"); 
    JButton blb = new JButton("#"); 

    JButton resetB = new JButton("RESET"); 

     //layout managers 
    BorderLayout layoutB = new BorderLayout(); 
    GridLayout layoutG = new GridLayout(4,3); 

     //panels 
    JPanel bigP = new JPanel(layoutB); 
    JPanel numberP = new JPanel(layoutG); 

     //JLabel 
    JLabel displayL = new JLabel(); 
    JLabel counterL = new JLabel(); 

     //listener 
    ButtonListener buttonListener = new ButtonListener(); 

public BorderPanel() 
{ 
    /****************START**********************/ 

    displayL.setText(display); 

    numberP.add(b1); 
    b1.addActionListener(buttonListener); 
    numberP.add(b2); 
    b2.addActionListener(buttonListener); 
    numberP.add(b3); 
    b3.addActionListener(buttonListener); 
    numberP.add(b4); 
    b4.addActionListener(buttonListener); 
    numberP.add(b5); 
    b5.addActionListener(buttonListener); 
    numberP.add(b6); 
    b6.addActionListener(buttonListener); 
    numberP.add(b7); 
    b7.addActionListener(buttonListener); 
    numberP.add(b8); 
    b8.addActionListener(buttonListener); 
    numberP.add(b9); 
    b9.addActionListener(buttonListener); 
    numberP.add(bstar); 
    bstar.addActionListener(buttonListener); 
    numberP.add(b0); 
    b0.addActionListener(buttonListener); 
    numberP.add(blb); 
    blb.addActionListener(buttonListener); 
    resetB.addActionListener(buttonListener); 

    bigP.add(displayL, layoutB.SOUTH); 
    bigP.add(resetB, layoutB.EAST); 
    bigP.add(numberP, layoutB.CENTER); 
    add(counterL); 
    add(bigP); 
} 

private class ButtonListener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent event) 
    { 
     counter ++; 

     if (total == 3) 
      display += "-"; 

     if (total == 6) 
      display += "-"; 

     source = event.getSource(); 

     if (source == b1) 
      display += "1"; 

     if (source == b2) 
      display += "2"; 

     if (source == b3) 
      display += "3"; 

     if (source == b4) 
      display += "4"; 

     if (source == b5) 
      display += "5"; 

     if (source == b6) 
      display += "6"; 

     if (source == b7) 
      display += "7"; 

     if (source == b8) 
      display += "8"; 

     if (source == b9) 
      display += "9"; 

     if (source == b0) 
      display += "0"; 

     if (source == bstar) 
      display += "*"; 

     if (source == blb) 
      display += "#"; 

     if (source == resetB) 
      display = ""; 
      counter = 0; 

     displayL.setText(display); 
     counterL.setText("" + counter); 
    } 
} 
} 

回答

0

您需要的if語句,否則counter = 0總是執行包括括號這一點。如果你沒有對控制結構使用大括號,它只包含結構範圍內的下一個語句。

所以,你的代碼

if (source == resetB) 
     display = ""; 
     counter = 0; 

實際上相當於

if (source == resetB) 
    { 
     display = ""; 
    } 

    counter = 0; 

您需要括在大括號這兩個語句。這就是爲什麼你應該習慣使用支架來控制結構(if/else/for/while等),除非你確定它不會造成任何混淆。即使那樣,你也會冒這樣的風險。

if (source == resetB) 
    { 
     display = ""; 
     counter = 0; 
    } 

此外,要設置total但從來沒有使用它,這樣雖然會增加counter它不會顯示連字符。

您需要的total兩種用法更改爲counter和數量添加到顯示後,其移動,否則你將得到00-000-00000而不是000-000-0000

所以它看起來像:

if (source == b1) 
     display += "1"; 

    //...etc 

    if (source == blb) 
     display += "#"; 

    if (source == resetB) 
    { 
     display = ""; 
     counter = 0; 
    } 

    if (counter == 3) 
     display += "-"; 

    if (counter == 6) 
     display += "-";