CountingNumbers時不是抽象和java.awt.event.ActionListener不重寫抽象方法的actionPerformed(java.awt.event.ActionEvent中)試圖建立一個可點擊的按鈕,但拋出的錯誤編譯
是錯誤我明白了。
我的代碼:
//Create an application that counts the number of each letter in a given word/phrase
//have a box that has a text box a button and output box
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CountingNumbers extends JFrame implements ActionListener
{
public void count()
{
JFrame frame = new JFrame("Counting Letters Application");
JTextField textEntry = new JTextField("enter your word/phrase here", 1);
JButton count = new JButton("Count Letters");
count.addActionListener(this);
frame.setPreferredSize(new Dimension(400, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color teal = new Color(0, 128, 128);
frame.getContentPane().setBackground(teal);
frame.getContentPane().add(textEntry, BorderLayout.NORTH);
frame.getContentPane().add(count, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e, JFrame frame)
{
JTextField output = new JTextField("output box", 4);
frame.getContentPane().add(output, BorderLayout.WEST);
output.setText("button was clicked");
}
}
我不知道爲什麼會這樣,我是相當新的java和從我校一類剛剛學會。
@Cody:查看有關輸出的建議更改JTextField – 2011-04-09 14:17:55
很好的答案!我也建議使jframe和jtextfields最終。 – extraneon 2011-04-09 14:27:26
@extraneon:謝謝!我第二次提出你的建議。我自己,我會使輸出組件成爲JLabel或不可編輯的JTextField。 – 2011-04-09 14:40:25