所以我試圖爲一個接口創建兩個按鈕,我嘗試實現ActionListener來啓用一個按鈕來打印一個字符串,但它給了我一個錯誤,說「actionlistener wasn' t在類BorderDemo中實現「在RTP GUI中實現Actionlistener
我做了什麼錯了?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
class BorderDemo
implements ActionListener
{
public static void main (String[] args)
{
JFrame F = new JFrame("Buttons");
F.addWindowListener
(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});
F.setSize(544,416);
JPanel pane = (JPanel) F.getContentPane();
pane.add(new Picture(),BorderLayout.CENTER);
pane.add(new JButton("Start"),BorderLayout.WEST);
pane.addActionListener(this);
pane.add(new JButton("Stop"),BorderLayout.EAST);
F.setVisible(true);
F.setResizable(false);
}
}
class Picture extends JComponent
{
public Picture()
{
repaint();
}
public void paint (Graphics g)
{
g.setColor(Color.yellow);
g.fillOval(getWidth()/4,getHeight()/4,
getWidth()/2,getHeight()/3);
g.setColor(Color.black);
g.fillOval(getWidth()/2,getHeight()/4,
getWidth()/17,getHeight()/3);
g.setColor(Color.black);
g.fillOval(getWidth()/3,getHeight()/4,
getWidth()/17,getHeight()/3);
g.setColor(Color.white);
g.fillOval(getWidth()/5,getHeight()/5,
getWidth()/5,getHeight()/7);
g.setColor(Color.white);
g.fillOval(getWidth()/3,getHeight()/8,
getWidth()/5,getHeight()/7);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Item clicked: "+e.getActionCommand());
}
}
1運行正常基礎上貼出的代碼示例)用於代碼塊一致性和邏輯縮進。代碼的縮進旨在幫助人們理解程序流程。 2)請學習常見的[Java命名約定](http://java.sun.com/docs/books/jls/second_edition/html/names.doc。html#73307)(特別是用於名稱的情況)類別,方法和屬性名稱並一致使用。 –