java - 按钮计数器未更新

标签 java button

尝试编写一个简单的程序来显示通过按钮输入的电话号码。我设置了一个计数器,每次按下按钮时都会在第三个和第六个数字输入后自动插入“-”。我的计数器似乎没有在按下按钮时更新。我不明白为什么。这可能很简单,但我将非常感谢任何帮助。

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

最佳答案

您需要为此 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 += "-";

关于java - 按钮计数器未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23413160/

相关文章:

jQuery Button.click() 事件被触发两次

button - 如何在 J2ME 应用程序的窗体上创建按钮?

java - Spring WebFlux WebClient : receive part of response

java - 如何创建一个更新查询来更改带有 sysdate 的字段值?

java - 如何在android中像facebook一样在屏幕底部显示三个按钮?

java - 使用 jenkins 构建的 Maven 没有从本地 .m2 存储库中获取 jar

java - FragmentManager 子级和 findViewById NullPointer

css - YouTube按钮高度

使用 DOM 和 appendChild 的函数中的 JavaScript 数组

android - 一个 OnClickHandler 用于多个按钮