[問題] 新手的發問 編譯階段出了問題
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
→
04/26 20:59, , 1F
04/26 20:59, 1F
※ 編輯: FUFrench 來自: 118.160.90.63 (04/26 21:16)
推
04/26 21:29, , 2F
04/26 21:29, 2F
→
04/26 21:30, , 3F
04/26 21:30, 3F
→
04/26 21:33, , 4F
04/26 21:33, 4F
→
04/26 21:34, , 5F
04/26 21:34, 5F
→
04/26 21:37, , 6F
04/26 21:37, 6F
→
04/26 21:39, , 7F
04/26 21:39, 7F
→
04/26 21:42, , 8F
04/26 21:42, 8F