HALP! 警告:前面有很多代码。
所以我有一个任务,我觉得我已经非常接近让它工作了。它编译并显示按钮。
问题:当我按下按钮时,没有数字出现。
这是我的指示: 设计并实现一个显示数字键的程序 - 可能出现在手机上的打击垫。在键盘按钮上方, 显示一个标签,显示拾取后的数字。到了 右键盘按钮,包括另一个按钮清除 显示。使用边框布局来管理整体演示, 以及用于管理键盘按钮的网格布局。放一个边框 键盘按钮周围可视化和边框分组 在显示器周围。
Cliffs:基本上制作一个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);
}
}
答案 0 :(得分:2)
应该是
private String num;
不
private string num;
答案 1 :(得分:2)
变量tnum
和num
在类Phonekeypadpanel
和构造函数中都被声明。当您尝试为它们赋值时,最终只会更改驻留在构造函数中的本地副本,而不是类变量。将行String num = "";
更改为num = "";
并将JLabel tnum = new JLabel();
更改为tnum = new JLabel();
。
最后,将"one"
,"two"
等替换为"1"
,"2"
等,因为当您将System.out.println(event.getActionCommand());
放在actionPerformed
的顶部时{1}}你会看到输出是按钮的标签。
答案 2 :(得分:1)
您尝试使用e.getActionCommand()
,但没有e
,它应该是event
。
else if (event.getActionComand().equals("three")){
...
}
event
是巡回ActionEvent
变量,而不是e
。这就是can't find symbol
的原因。你刚刚错过了“三个”
和
Jlabel display = new JLabel ("Phone Number ");
没有这样的课程Jlabel
。应为JLabel
编辑:显示文字
if (event.getActionCommand().eqauls("three")){
num += "3";
display.setText(num);
}