-1

我已经扭转了但得到一个编译错误 PLZ HELP 需要在今天晚上 10:00 之前得到答案,因为这是由于今晚

我在执行反向按钮操作时遇到编译错误:找不到符号词 - 如何解决该问题

这是源代码:

public class ReverseTester extends JFrame
{
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 200;

private JTextField txtInstring;
private JLabel lblOutstring;
private JButton btnReverse;
private JButton btnExit;
private JPanel pnl2x3;

public ReverseTester()
{
  txtInstring = new JTextField("");
  lblOutstring = new JLabel("Reversed LinkedList: ");

  btnReverse = new JButton("Reverse");
  ActionListener my_ReverseListener = new ReverseListener();
  btnReverse.addActionListener(my_ReverseListener);

  btnExit = new JButton("Exit");
  ActionListener my_ExitListener = new ExitButtonListener();
  btnExit.addActionListener(my_ExitListener);

  pnl2x3 = new JPanel();
  pnl2x3.setLayout (new GridLayout(2,1));
  pnl2x3.add(txtInstring);
  pnl2x3.add(btnReverse);
  pnl2x3.add(btnExit);
  pnl2x3.add(lblOutstring);
  add(pnl2x3, BorderLayout.CENTER);
  setSize(FRAME_WIDTH, FRAME_HEIGHT);
}

public static void main(String[] args)   
{
  LinkedList<String> words = new LinkedList<String>();
  words.addLast("before...");
  words.addLast("gone");
  words.addLast("has");
  words.addLast("one");
  words.addLast("no");
  words.addLast("where");
  words.addLast("go");
  words.addLast("boldly");
  words.addLast("To");
  words.addLast("frontier.");
  words.addLast("final");
  words.addLast("the");
  words.addLast("is");
  words.addLast("Space");

  String instring = String.format("%s",words);
  ListUtil.reverse(words);
  JFrame frame = new ReverseTester();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
}

class ExitButtonListener implements ActionListener
{
  public void actionPerformed(ActionEvent e)
  {
    dispose();
  }
}
class ReverseListener implements ActionListener
{
  public void actionPerformed(ActionEvent e)
  {
    String outstring = String.format("%s",words);
    outstring = outstring.replace(",", "");

    lblOutstring.setText(outstring);
  }
}
}


public class ListUtil
{
  public static void reverse(LinkedList<String> myList)
  {
    LinkedList<String> temp = new LinkedList<String>();
    for (String element : myList)
    {
      //String removed = myList.removeFirst();
      temp.addFirst(element);
      String outstring_rev = String.format("%s",temp);
      outstring_rev = outstring_rev.replace(",", "");
    }
  }

}
4

2 回答 2

0

班级ReverseListener不知道lblOutstring是什么。你需要以某种方式给它这个信息。

于 2013-11-04T20:05:00.370 回答
0

您的变量在函数words范围内声明。main()编译器不知道ReverseListener类中的这个变量。声明words为类范围变量或将其传递给ReverseListener

于 2013-11-04T20:07:33.883 回答