我的ButtonListener拨号程序出错

时间:2012-10-24 00:13:50

标签: java swing awt actionlistener mixing

我只想弄清楚为什么会出现这些错误。我不确定为什么它会说varibles没有找到或为什么它不会让我加或减int。

以下是错误:

         javac Dial2.java
 Dial2.java:53: cannot find symbol
symbol  : class arrl
location: class DialFrame
b.addActionListener(new arrl());
                        ^
Dial2.java:56: b is already defined in DialFrame(java.lang.String)
Button b = new Button("<");
       ^
Dial2.java:57: cannot find symbol
symbol  : class arrl
location: class DialFrame
b.addActionListener(new arrl());
                        ^
Dial2.java:127: cannot find symbol
symbol  : variable pastNumbeCount
location: class DialFrame.ArrowListener
    arrowIndex = (arrowIndex + pastNumbeCount - 1) % pastNumberCount;
                               ^
Dial2.java:127: operator + cannot be applied to int,pastNumbeCount
    arrowIndex = (arrowIndex + pastNumbeCount - 1) % pastNumberCount;
                  ^
Dial2.java:127: operator - cannot be applied to <nulltype>,int
    arrowIndex = (arrowIndex + pastNumbeCount - 1) % pastNumberCount;
                                              ^
Dial2.java:128: cannot find symbol
symbol  : variable arrowindex
location: class DialFrame.ArrowListener
    phoneNumber.setText(pastNumbers[arrowindex - 1]);
                                    ^
Dial2.java:133: cannot find symbol
symbol  : variable pastNumberindex
location: class DialFrame.ArrowListener
    phoneNumber.setText(pastNumberindex);
                        ^
8 errors

这是一个简单的电话拨号器的代码:

cat Dial2.java
import java.awt.*;
import java.awt.event.*;

// Driver class
public class Dial2 {

public static void main(String[] args) {
Frame frame = new DialFrame("Dial");
frame.pack();
frame.setVisible(true);
}
}

// Frame class
class DialFrame extends Frame {
private TextField phoneNumber = new TextField();
private String pastNumbers[] = new String[10];
private int pastNumberCount = 0;
private int pastNumberIndex = 0;
private int arrowIndex = 0;

// Constructor
public DialFrame(String title) {
// Set title
super(title);

// set all pastNumbers to empty string
for (int x = 0; x < pastNumbers.length; x++)
  pastNumbers[x] = "";

// Make text field non-editable and put at top of frame
phoneNumber.setEditable(false);
add("North", phoneNumber);

// Create panel to hold buttons and set its layout
Panel buttonPanel = new Panel();
buttonPanel.setLayout(new GridLayout(4, 3, 10, 10));

DigitListener dl = new DigitListener();
ArrowListener arrl = new ArrowListener();

// Create first nine buttons, attach a listener to each,
// and add buttons to buttonPanel
for (int i = 1; i <= 9; i++) {
  Button b = new Button(i + "");
  b.addActionListener(dl);
  buttonPanel.add(b);
}

// **1** Add your two new buttons to your panel, along with an
//       an arrow listener for both
Button b = new Button(">");
b.addActionListener(new arrl());

b = new Button("0");
Button b = new Button("<");
b.addActionListener(new arrl());
buttonPanel.add(b);



// Create a panel to hold buttonPanel; put it at the
// center of the frame
Panel centerPanel = new Panel();
centerPanel.add(buttonPanel);
add("Center", centerPanel);

// Create a panel to hold the "Dial" button; put it at
// the bottom of the frame
Panel bottomPanel = new Panel();
b = new Button("Dial");
b.addActionListener(new DialListener());
bottomPanel.add(b);
add("South", bottomPanel);

// Attach window listener
addWindowListener(new WindowCloser());
}

private void addPastNumber(String num) {
boolean numberAlreadyAdded = false;
for (int x=0; x < pastNumbers.length; x++) {
  if (pastNumbers[x].equals(num)) {
    numberAlreadyAdded = true;  break;
  }
}
if (!numberAlreadyAdded) {
  pastNumbers[pastNumberIndex] = num;
  pastNumberIndex++;
  if (pastNumberCount < 10) pastNumberCount++;
  if (pastNumberIndex == pastNumbers.length) pastNumberIndex = 0;
}
arrowIndex = pastNumberIndex;
}  

 // Listener for all buttons
class DigitListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
  String buttonLabel = evt.getActionCommand();
  // append the digit on the newly pressed key
  phoneNumber.setText(phoneNumber.getText() + buttonLabel);
}
}

// Listener for all buttons
class DialListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
  addPastNumber(phoneNumber.getText());
  phoneNumber.setText("Dialing...");
  // pause for two seconds, or 2000 millisecond  
  try {
    Thread.sleep(2000);
  } catch (InterruptedException e) {}
  phoneNumber.setText("");
}
}

// Listener for "arrow" buttons
class ArrowListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {

  // ** 2 **
String buttonLabel = evt.getActionCommand();

if (buttonLabel == "<")
{
    arrowIndex = (arrowIndex + pastNumbeCount - 1) % pastNumberCount;
    phoneNumber.setText(pastNumbers[arrowindex - 1]);
}
else
{
    arrowIndex = (arrowIndex + 1) % pastNumberCount;
    phoneNumber.setText(pastNumberindex);
}








}
}

 // Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
  System.exit(0);
}
}
}

任何帮助或信息将不胜感激。

说明:如果out outNumbers数组中的当前索引为9,则如果“&gt;”将显示存储在索引0处的数字键是按下的。如果当前索引为0,则如果“&lt;”将显示存储在索引9处的数字。键是按下的。

要进行此设置,我需要从我的按钮中检索标签。如果是“&lt;”键,将我的箭头索引设置为:     (arrowIndex + pastNumberCount - 1)%pastNumberCount; 并将我过去的数字数组中此索引处的过去数字添加到我的phoneNumber文本字段。

否则,我需要将我的数组索引设置为:(arrowIndex + 1)%pastNumberCount,并将此索引处的过去数字添加到文本字段中。

1 个答案:

答案 0 :(得分:1)

使用新的ArrowListener()而不是新的arrl(); (arrl不是一个类,它是一个对象) 使用pastNumberCount而不是pastNumbeCount (r缺失) 使用arrowIndex而不是arrowindex (我应该是大写) 使用pastNumberIndex而不是pastNumberindex (我应该是大写)

你的节目中有很多错别字。

此致 佳日