1

これらのエラーが発生する理由を理解しようとしています。変数が見つからないというメッセージが表示される理由や、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 pastNumbers 配列内の現在のインデックスが 9 の場合、>" キーを押すと、インデックス 0 に格納されている数値が表示されます。現在のインデックスが 0 の場合、「<」キーを押すと、インデックス 9 に格納されている数値が表示されます。

これを設定するには、ボタンからラベルを取得する必要があります。「<」キーの場合は、矢印のインデックスを次のように設定します: (arrowIndex + pastNumberCount - 1) % pastNumberCount; 過去の番号の配列のこのインデックスにある過去の番号を phoneNumber テキスト フィールドに追加します。

それ以外の場合は、配列のインデックスを : (arrowIndex + 1) % pastNumberCount に設定し、このインデックスの過去の数字をテキスト フィールドに追加する必要があります。

4

1 に答える 1

1

new arrl(); の代わりに new ArrowListener() を使用してください。(arrl はクラスではなく、オブジェクトです) pastNumbeCount の代わりに pastNumberCount を使用します(r がありません) arrowindex の代わりに arrowIndex を使用します (i は大文字にする必要があります) pastNumberindex の代わりに pastNumberIndex を使用します(i は大文字にする必要があります)

あなたのプログラムには多くのタイプミスがあります。

よろしく、 ヤシュ

于 2012-10-24T11:33:46.330 に答える