2017-03-13 15 views
0

したがって、関数setPaintFlag()のスコープ内に外部のボタンリスナーがあり、呼び出す必要がありますが、Javaではランタイムエラーが発生しています。さらに、私の描画パネルが表示されません。誰も私の間違いを見つけられますか?プログラムの目的は、ランダムな大きさと色でランダムな位置に円または正方形のいずれかを描くことです。プログラムがコンパイルされ、ウィンドウが表示されますが、ボタンだけが表示されますが、起動しません。目に見えるエラー私はsetPaintFlag()上にカーソルを移動The method setPaintFlag(int) is undefined for the type PaintLab.buttonListenerボタンリスナーを使用してJava内部クラス関数を呼び出す

ボタンを押した後に、より詳細なエラーメッセージが

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

public class PaintLab extends JPanel { 
    private JButton circle, square; 
    private JPanel drawingPanel; 

    public PaintLab() { 
     circle = new JButton("Draw Circle"); 
     circle.addActionListener(new ButtonListener()); 
     square = new JButton("Draw Square"); 
     square.addActionListener(new ButtonListener()); 
     JPanel buttonPanel = new JPanel(); 
     JPanel drawingPanel = new DrawingPanel(400, 400); 
     add(drawingPanel); 
     BoxLayout bX = new BoxLayout(buttonPanel, BoxLayout.X_AXIS); 

     buttonPanel.setLayout(bX); 
     buttonPanel.add(circle); 
     buttonPanel.add(square); 

     add(buttonPanel); 

    } 

    private class DrawingPanel extends JPanel { // outer 
     private boolean paintFlag = false; 
     private int theMode = -1; 
     private int width, height; 

     private DrawingPanel(int width, int height) { // inner 
      this.width = width; 
      this.height = height; 
     } 

     public void setPaintFlag(int current) { // inner 
      paintFlag = true; 
      theMode = current; 
     } 

     public void paintComponent(Graphics g) { // inner 
      super.paintComponent(g); 
      Graphics2D g2 = (Graphics2D) g; 
      int r = (int) (Math.random() * 255) + 1; 
      int gn = (int) (Math.random() * 255) + 1; 
      int b = (int) (Math.random() * 255) + 1; 
      g2.setColor(new Color(r, gn, b)); 
      int w = (int) (Math.random() * width) + 1; 
      int h = (int) (Math.random() * height) + 1; 
      int x = (int) (Math.random() * width) + 1; 
      int y = (int) (Math.random() * height) + 1; 

      if (theMode == 0) { 
       g2.drawOval(x, y, w, h); 
       g2.fillOval(x, y, w, h); 
      } else if (theMode == 1) { 
       g2.drawRect(x, y, w, h); 
       g2.fillRect(x, y, w, h); 
      } 
      g2.dispose(); 
     } 

    } 

    private class ButtonListener implements ActionListener { 

     public void actionPerformed(ActionEvent event) { 

      if (event.getSource() == circle) { 
       setPaintFlag(0); 
       drawingPanel.repaint(); 
      } else if (event.getSource() == square) { 
       setPaintFlag(1); 
       drawingPanel.repaint(); 
      } 
     } 
    } 

} 

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: The method setPaintFlag(int) is undefined for the type PaintLab.ButtonListener The method setPaintFlag(int) is undefined for the type PaintLab.ButtonListenerであり、これがメインです。あなたはsetPaintFlag(int current)フォームButtonListenerを呼ぶが、それはDrawingPanelに定義されているため

Circle = new JButton("Draw Circle") 
Circle.addActionListener(new buttonListener()) 

エラーメッセージは次のとおりです。彼らは何のアクションリスナーを持っていないので

public class paintLabMain { 

    public static void main(String[] args) { 
     JFrame frame=new JFrame("Paint Lab"); 
     frame.setPreferredSize(new Dimension (300,600)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new PaintLab()); 
     frame.pack(); 
     frame.setVisible(true); 


    } 

} 
+0

1. [MCVE]を投稿してください。 2.あなたは 'buttonListener'を呼び出しますか? 3.どのようなエラーが出ますか? – c0der

+0

申し訳ありません私は私のメインを追加することを忘れていました。プログラムがコンパイルされ、ウィンドウが表示されますが、ボタンだけが表示されますが、起動しません。 –

+0

アクションリスナーがないため、ボタンは起動しません – c0der

答えて

3

ボタンは発生しません。

編集: は(私はそれの部品を取り外し、スケルトンを残したので、コードをコンパイルすることはできません)コメントを参照してください。

public class PaintLab extends JPanel { 
    private JButton circle, square; 
    private DrawingPanel drawingPanel;//declare drawingPanel as field 

    public PaintLab() { 
     circle = new JButton("Draw Circle"); 
     circle.addActionListener(new ButtonListener()); 
     square = new JButton("Draw Square"); 
     square.addActionListener(new ButtonListener()); 
     JPanel buttonPanel = new JPanel(); 
     /*JPanel already declared as field*/ 
     drawingPanel = new DrawingPanel(400, 400); 
     add(drawingPanel); 
     BoxLayout bX = new BoxLayout(buttonPanel, BoxLayout.X_AXIS); 

     buttonPanel.setLayout(bX); 
     buttonPanel.add(circle); 
     buttonPanel.add(square); 

     add(buttonPanel); 
    } 

    private class DrawingPanel extends JPanel { // outer 

      //code removed 
     } 

     public void paintComponent(Graphics g) { // inner 
      //code removed  
     } 

     //add setters 
     void setPaintFlag(boolean paintFlag) { 
      this.paintFlag = paintFlag; 
     } 

     void setTheMode(int theMode) { 
      this.theMode = theMode; 
     } 
    } 

    class ButtonListener implements ActionListener { 

     public void actionPerformed(ActionEvent event) { 

      if (event.getSource() == circle) { 
       drawingPanel.setPaintFlag(true); 
       drawingPanel.repaint(); 
      } else if (event.getSource() == square) { 
       drawingPanel.setPaintFlag(false); 
       drawingPanel.repaint(); 
      } 
     } 
    } 
} 
+0

変更を加えたとき、 'setPaintFlag'はまだエラーをスローしていますが、ボタンは現在起動しています。ありがとうございます –

+0

ヘルプを非常に感謝し、あなたはすべてのエラーを解決し、プログラムが動作しています。 –

+0

コードはコンパイルされません。メソッドのメソッドを持つことはできません – MadProgrammer

関連する問題