我已经反转了,但出现编译错误 PLZ 帮助需要在今天晚上 10:00 之前得到答案,因为这是由于 tonigt
我在执行的反向按钮操作时收到编译错误:找不到符号单词 - 如何解决
以下是源代码:
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(",", "");
}
}
}
类ReverseListener
不知道lblOutstring
是什么。你需要以某种方式给它这些信息。
您的words
变量在函数main()
范围内声明。编译器不知道类ReverseListener
这个变量。将words
声明为类范围变量或将其传递给ReverseListener