警告:前面有很多代码。

所以我有一个任务,我觉得我已经很接近要开始工作了。它编译并显示按钮。

问题:当我按下按钮时,没有数字出来。

这是我的指示:
设计并实现一个显示数字键的程序-
手机上可能出现的键盘。在键盘按钮上方,
显示一个标签,其中显示了被拾取的数字。到
键盘按钮的右侧,包括另一个按钮以清除
显示。使用边框布局来管理整个演示文稿,
以及用于管理键盘按钮的网格布局。放置边框
围绕键盘按钮进行可视化分组,并带有边框
在显示器周围。

悬崖:基本上是制作一个GUI,让您可以像电话一样打入数字。
这是我的代码的样子。我有一个驱动程序文件,以及一个大多数实际程序在其中的文件:

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


public class Phonekeypadpanel extends JPanel
{

private JLabel tnum;
private JPanel pad, clr,title;
private JButton one,two,three,four,five,six,seven,eight,nine,zero,clear,aster,pound;
private String num;



   public Phonekeypadpanel ()
   {
   //KEYPAD YO

   JPanel pad = new JPanel(new GridLayout(4,3));
   setBackground (Color.red);
   setLayout (new BorderLayout());

   one=new JButton ("1");
   two=new JButton ("2");
   three=new JButton ("3");
   four=new JButton ("4");
   five=new JButton ("5");
   six=new JButton ("6");
   seven=new JButton ("7");
   eight=new JButton ("8");
   nine=new JButton ("9");
   aster=new JButton ("*");
   zero=new JButton ("0");
   pound=new JButton ("#");

   pad.add(one);
   pad.add(two);
   pad.add(three);
   pad.add(four);
   pad.add(five);
   pad.add(six);
   pad.add(seven);
   pad.add(eight);
   pad.add(nine);
   pad.add(aster);
   pad.add(zero);
   pad.add(pound);

   one.addActionListener(new ButtonListener());
   two.addActionListener(new ButtonListener());
   three.addActionListener(new ButtonListener());
   four.addActionListener(new ButtonListener());
   five.addActionListener(new ButtonListener());
   six.addActionListener(new ButtonListener());
   seven.addActionListener(new ButtonListener());
   eight.addActionListener(new ButtonListener());
   nine.addActionListener(new ButtonListener());
   aster.addActionListener(new ButtonListener());
   zero.addActionListener(new ButtonListener());
   pound.addActionListener(new ButtonListener());

   //And here is the clear button.

   JPanel clr = new JPanel();
   clear= new JButton("CLR");
   clr.add(clear);
   clear.addActionListener(new ButtonListener());

  //The buttons get added to a border layout here.

   add(pad,BorderLayout.CENTER);
   add(clr,BorderLayout.EAST);

   //display code where pushed buttons will be seen.
   num = "";

   tnum = new JLabel();
   tnum.setText(num);
   add(tnum,BorderLayout.NORTH);

   }

  // Here we have the code for what to do when buttons are pressed.

   private class ButtonListener implements ActionListener
  {

   public void actionPerformed(ActionEvent event)
  {

          if (event.getSource()==("1"))

            {

             num = "1" + num;
             tnum.setText("Telephone number: " + num);

            }else

          if(event.getSource()==("2"))

            {

             num = "2" + num;
             tnum.setText("Telephone number: " + num);

            }else

          if (event.getSource()==("3"))
            {

              num = "3" + num;
             tnum.setText("Telephone number: " + num);

            }else

          if (event.getSource()==("4"))
           {

              num = "4" + num;
             tnum.setText("Telephone number: " + num);

           }else

          if (event.getSource()==("5"))
           {

              num = "5" + num;
             tnum.setText("Telephone number: " + num);
           }else

          if (event.getSource()==("6"))
          {

             num = "6" + num;
             tnum.setText("Telephone number: " + num);

          }else

          if (event.getSource()==("7"))
          {

             num = "7" + num;
             tnum.setText("Telephone number: " + num);;

          }else

          if (event.getSource()==("8"))
          {

             num = "8" + num;
             tnum.setText("Telephone number: " + num);

          }else

          if (event.getSource()==("9"))
          {

             num = "9" + num;
             tnum.setText("Telephone number: " + num);

          }else

          if (event.getSource()==("*"))
          {

             num = "*" + num;
             tnum.setText("Telephone number: " + num);

          }else

          if (event.getSource()==("0"))
          {

             num = "0" + num;
             tnum.setText("Telephone number: " + num);

          }else


         if (event.getSource()==("#"))
         {

              num = "#" + num;
             tnum.setText("Telephone number: " + num);

          }
          else
        if (event.getSource()==("CLR"))
         {
                 num = "";
             tnum.setText("Telephone number: " + num);

         }

   }

}
}


您并不需要真正看一下,但这是我的驱动程序文件:

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


public class Phonekeypad
{


   public static void main(String[] args)
   {
   JFrame frame = new JFrame("Phone Keypad");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.getContentPane().add(new Phonekeypadpanel());
   frame.pack();
   frame.setVisible(true);
   }
}

最佳答案

在类tnum和构造函数中都声明了变量numPhonekeypadpanel。当您尝试为它们分配值时,最终只会更改驻留在构造函数中的本地副本,而不会更改类变量。将行String num = "";更改为num = "";,并将JLabel tnum = new JLabel();更改为tnum = new JLabel();

最后,将每个"one""two"等替换为"1""2"等。因为将System.out.println(event.getActionCommand());放在actionPerformed的顶部时,您会看到输出是标签的标签。按钮。

10-02 04:28