2013-04-14 43 views
0

所以我試圖爲一個接口創建兩個按鈕,我嘗試實現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()); 
} 
} 
+2

1運行正常基礎上貼出的代碼示例)用於代碼塊一致性和邏輯縮進。代碼的縮進旨在幫助人們理解程序流程。 2)請學習常見的[Java命名約定](http://java.sun.com/docs/books/jls/second_edition/html/names.doc。html#73307)(特別是用於名稱的情況)類別,方法和屬性名稱並一致使用。 –

回答

1

看起來actionPerformed加到Picture類,而不是BorderDemo。所以如果你把它移動到BorderDemo它應該解決上述錯誤。

原因的錯誤是BorderDemo被聲明爲實現ActionListener接口的事實:

class BorderDemo implements ActionListener 

但是,它並沒有實現它。添加在ActionListener定義actionPerformed方法,即:

@Override 
public void actionPerformed(ActionEvent arg0) { 
} 

看看Implementing an Interface教程。另見How to Write an Action Listener

一些小意見:

  1. 你可能會想動作偵聽器添加到按鈕,而不是JPanelJPanel不接受動作監聽器。

  2. 不是添加窗口監聽到框架上滑蓋關閉,可以使用退出該應用程序的:

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  3. 而不是設置框架大小與setSize(544, 416);Picture實施getPrefferedSize()和框架上調用pack();

  4. Picture類中,請勿覆蓋paint(),覆蓋paintComponent()。此外,請不要忘記在您的實施中致電super.paintComponent()。見Performing Custom Painting

  5. 請務必在上創建UI組件使用invokeLater()。見Initial Threads

  6. 正如上面評論中提到的,代碼可讀性非常重要。有關詳細信息和示例,請參閱Code Conventions for the Java Programming Language

編輯:在RTP 1.7

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 


class BorderDemo { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowUI(); 
      } 
     }); 
    } 

    public static void createAndShowUI() { 
     JFrame frame = new JFrame("Buttons"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JButton startButton = new JButton("Start"); 
     startButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("Start clicked"); 
      } 
     }); 
     JButton stopButton = new JButton("Stop"); 
     stopButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("Stop clicked"); 
      } 
     }); 

     Container contentPane = frame.getContentPane(); 
     contentPane.add(new Picture(), BorderLayout.CENTER); 
     contentPane.add(startButton, BorderLayout.WEST); 
     contentPane.add(stopButton, BorderLayout.EAST); 

     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setResizable(false); 
    } 

    static class Picture extends JComponent { 
     public Picture() { 
     } 

     public Dimension getPreferredSize() { 
      return new Dimension(544, 416); 
     } 

     public void paintComponent(Graphics g) { 
      super.paintComponent(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); 
     } 
    } 
} 
+0

感謝您的回覆,但我還有其他疑問:您準備好的程序中提供了哪些代碼? (版本1.7) –

+0

@LongVuTa RTP綁定到特定版本的Java? – tenorsax

+0

@LongVuTa似乎RTP 1.7使用Java 1.4,所以你應該能夠實現所有的建議。查看編輯示例。 – tenorsax