看板 java 關於我們 聯絡資訊
TwoButtons.java:49: TwoButtons.ColorListener is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event .ActionListener class ColorListener implements ActionListener 照課本解答KEY出來的程式 編譯卻碰到了這個過不去的問題.... 翻書很久找不到解決方法 只好來這邊求高人指點了 orz 程式碼如下 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TwoButtons { JFrame frame; JLabel label; public static void main (String[] args) { TwoButtons gui = new TwoButtons(); gui.go(); } public void go() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton labelButton = new JButton("Change Label"); labelButton.addActionListener( new LabelListener() ); JButton colorButton = new JButton("Change Circle"); colorButton.addActionListener( new ColorListener() ); label = new JLabel("I`m a label"); MyDrawPanel drawPanel = new MyDrawPanel(); frame.getContentPane().add(BorderLayout.SOUTH, colorButton); frame.getContentPane().add(BorderLayout.CENTER, drawPanel); frame.getContentPane().add(BorderLayout.EAST, labelButton); frame.getContentPane().add(BorderLayout.WEST, label); frame.setSize(300, 300); frame.setVisible(true); } class LabelListener implements ActionListener { public void actionPerformed(ActionEvent event) { label.setText("Ouch!"); } } class ColorListener implements ActionListener { public void actionPreformed(ActionEvent event) { frame.repaint(); } } } class MyDrawPanel extends JPanel { public void paintComponent (Graphics g) { g.setColor(Color.orange); g.fillRect(20,50,100,100); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.160.90.63
tkcn:不是書印錯就是你打錯,所以請把程式排版好貼上來。 04/26 20:59
※ 編輯: FUFrench 來自: 118.160.90.63 (04/26 21:16)
asilzheng:actionPreformed actionPerformed 字打錯 04/26 21:29
asilzheng:在ColorListener這個class裡面 錯誤訊息裡有寫了 04/26 21:30
FUFrench:好丟人 為這種錯誤搞了半天 我ㄧ直以為 繼承出了問題... 04/26 21:33
FUFrench:以後我會多加注意 謝謝各位指正 : ) 04/26 21:34
uziel:你的程式裡面沒有實作 ColorListener 這個類別 04/26 21:37
uziel:抱歉,看錯 04/26 21:39
tkcn:這個不丟人啦,我在這個 method 上也錯過好幾次 :p 04/26 21:42