import java.awt.*; //Container, GridLayout, *, or etc...
import javax.swing.*; //JFrame, JLabel, *, or etc...
import java.awt.event.*;
public class NumerologyEC extends JFrame
{
private static final int Width = 400;
private static final int Height = 100;
private JLabel wordJL;
private JTextField wordTF;
private JButton calculateJB, exitJB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
public NumerologyEC()
{
setTitle ("Numerology Extra Credit");
wordJL = new JLabel ("Enter a word: ", SwingConstants.RIGHT);
wordTF = new JTextField(10);
calculateJB = new JButton ("Calculate");
cbHandler = new CalculateButtonHandler();
calculateJB.addActionListener (cbHandler);
exitJB = new JButton ("Exit");
ebHandler = new ExitButtonHandler();
exitJB.addActionListener (ebHandler);
Container pane = getContentPane();
pane.setLayout (new GridLayout (2, 2));
pane.add(wordJL);
pane.add(wordTF);
pane.add(calculateJB);
pane.add(exitJB);
setSize(Width, Height);
setVisible (true);
setDefaultCloseOperation (EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed (ActionListener e)
{
String word;
word = String.parseString (wordTF.getText());
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerfromed (ActionEvent e)
{
System.exit (0);
}
}
public static void main (String[] args)
{
NumerologyEC rectObject = new NumerologyEC();
}
}
我一直在“私有类CalculateButtonHandler实现ACtionListener”上出现错误
我缺少什么?
答案 0 :(得分:1)
ExitButtonHandler
中的方法是错误的:
public void actionPerfromed (ActionEvent e)
{
System.exit (0);
}
正确的拼写是:
actionPerformed(ActionEvent e)
{
System.exit(0);
}
第二:CalculateButtonHandler中的方法是错误的,正确的方法是
public void actionPerfromed (ActionEvent e)
{
}
不是
public void actionPerfromed (ActionListener e)
{
}
也是代码
word = String.parseString (wordTF.getText());
错误,String
类没有parseString()
方法。我想你想从输入TextArea获取输入并将其转换为String,为什么不使用word=wordTF.getText()
,因为wordTF.getText()
是一个String。