2012-04-30 11 views
0

Java GUI形式のゲームマスタを再作成しようとしています。私の問題は、ActionListenerが私のNewPanelクラスにアクセスすることができないことに由来します。どんな助けも素晴らしく、本当にこれに執着しています。プログラミング上の問題操作リスナー-FillRect

import javax.swing.*; 

    import java.awt.BorderLayout; 
    import java.awt.Color; 
    import java.awt.Dimension; 
    import java.awt.Graphics; 
    import java.awt.GridLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 

    public class Mastermind extends JFrame { 
     public Mastermind(){ 
      setLayout(new BorderLayout()); 
      JPanel p1 = new JPanel(); 
      JPanel p2 = new JPanel(); 
      add(p1, BorderLayout.WEST); 
      add(p2, BorderLayout.EAST); 

      /* PANEL 1 LAYOUT STUFF*/ 
      JPanel p4 = new JPanel(); 
      JPanel p5 = new JPanel(); 

      JButton red = new JButton("RED"); 
      JButton blue = new JButton("BLUE"); 

      JLabel HTP = new JLabel("How To Play"); 
      JLabel htp =new JLabel("<html>Player 2 will have 10<br> "+ 
       "attempts to correctly<br> guess Player 1's<br> "+ 
       "Password.The<br> password will consist<br> "+ 
       "of a 4 length sequence<br> "+ 
       "of 4 different colors.<br> "+ 
       "Colors may be used<br> "+ 
       "more than once or<br> " 
       "not at all.</html>"); 
      htp.getForeground(); 
      JLabel results = new JLabel("Guesses:"); 

      p1.setLayout(new BorderLayout()); 
      p4.setLayout(new BorderLayout()); 
      p5.setLayout(new GridLayout(1,2)); 

      p1.add(p4, BorderLayout.NORTH); 
      p1.add(p5, BorderLayout.SOUTH); 
      p1.add(results, BorderLayout.CENTER); 

      p4.add(HTP, BorderLayout.NORTH); 
      p4.add(htp,BorderLayout.SOUTH); 

      p5.add(red); 
      p5.add(blue); 
      red.setPreferredSize(new Dimension(80,20)); 

      /* END PANEL 1 LAYOUT STUFF*/ 

      /* PANEL 2 LAYOUT STUFF*/ 


      JPanel p3 = new JPanel(); 

      JButton green = new JButton("GREEN"); 
      JButton yellow = new JButton("YELLOW"); 

      p2.setLayout(new BorderLayout()); 
      p3.setLayout(new GridLayout(1,2)); 


      p2.add(p3, BorderLayout.SOUTH); 


      p2.add(new NewPanel(), BorderLayout.CENTER); 
      p3.add(green); 
      p3.add(yellow); 



      /* END PANEL 2 LAYOUT STUFF*/ 

      /* ACTION LISTENER EVENTS */   
      red.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 
        fillRect(Color.red); 
       } 
      }); 

      blue.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 

       } 
      }); 

      green.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 

       } 
      }); 

      yellow.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 

       } 
      }); 
} 

/* END ACTION LISTENER EVENTS */ 

public static void main(String[] args) { 
      Mastermind frame = new Mastermind(); 

      frame.setTitle("Mastermind"); 
      frame.setSize(334, 500); 
      frame.setLocationRelativeTo(null); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setVisible(true); 
} 

} 
class NewPanel extends JPanel { 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     int x,y; 

     for(y = 10; y<= 330; y+=35){ 
      for(x =10; x<=120; x+=35){ 
       g.drawRect(x, y, 25, 25); 
      } 
     } 
    } 
} 

答えて

0

ボタンに応じて色を変更するボックスがあります。私はあなたがここからそれを取りたいと思っていませんが、これは良いスタートになるはずです。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class Mastermind extends JFrame implements ActionListener{ 
    NewPanel p1,p2,p3,p4,p5; 
    public Mastermind(){ 
     super(); 
     setLayout(new BorderLayout()); 
     p1 = new NewPanel(); 
     p2 = new NewPanel(); 
     add(p1, BorderLayout.WEST); 
     add(p2, BorderLayout.EAST); 

     /* PANEL 1 LAYOUT STUFF*/ 
     p4 = new NewPanel(); 
     p5 = new NewPanel(); 

     JButton red = new JButton("RED"); 
     JButton blue = new JButton("BLUE"); 

     JLabel HTP = new JLabel("How To Play"); 
     JLabel htp =new JLabel("<html>Player 2 will have 10<br> "+ 
      "attempts to correctly<br> guess Player 1's<br> "+ 
      "Password.The<br> password will consist<br> "+ 
      "of a 4 length sequence<br> "+ 
      "of 4 different colors.<br> "+ 
      "Colors may be used<br> "+ 
      "more than once or<br> " + 
      "not at all.</html>"); 
     htp.getForeground(); 
     JLabel results = new JLabel("Guesses:"); 

     p1.setLayout(new BorderLayout()); 
     p4.setLayout(new BorderLayout()); 
     p5.setLayout(new GridLayout(1,2)); 

     p1.add(p4, BorderLayout.NORTH); 
     p1.add(p5, BorderLayout.SOUTH); 
     p1.add(results, BorderLayout.CENTER); 

     p4.add(HTP, BorderLayout.NORTH); 
     p4.add(htp,BorderLayout.SOUTH); 

     p5.add(red); 
     p5.add(blue); 
     red.setPreferredSize(new Dimension(80,20)); 

     /* END PANEL 1 LAYOUT STUFF*/ 

     /* PANEL 2 LAYOUT STUFF*/ 


     p3 = new NewPanel(); 

     JButton green = new JButton("GREEN"); 
     JButton yellow = new JButton("YELLOW"); 

     p2.setLayout(new BorderLayout()); 
     p3.setLayout(new GridLayout(1,2)); 


     p2.add(p3, BorderLayout.SOUTH); 


     p2.add(new NewPanel(), BorderLayout.CENTER); 
     p3.add(green); 
     p3.add(yellow); 

     red.addActionListener(this); 
     blue.addActionListener(this); 
     yellow.addActionListener(this); 
     green.addActionListener(this);  

    } 
    public void actionPerformed(ActionEvent e) { 
     System.out.println(e.getActionCommand()); 
     if (e.getActionCommand().equals("RED")){ 
      p1.setRectColor(Color.red); 
     }else if (e.getActionCommand().equals("BLUE")){ 
      p1.setRectColor(Color.blue); 
     } 
     else if (e.getActionCommand().equals("GREEN")){ 
      p1.setRectColor(Color.green); 
     } 
     else if (e.getActionCommand().equals("YELLOW")){ 
      p1.setRectColor(Color.yellow); 
     } 
     p1.updateUI(); 
     p2.updateUI(); 
     p3.updateUI(); 
     p4.updateUI(); 
     p5.updateUI(); 
    } 
} 



import java.awt.Color; 
import java.awt.Graphics; 

import javax.swing.JPanel; 

class NewPanel extends JPanel { 
    private Color c; 
    protected void paintComponent(Graphics g) { 
     super.paintComponents(g); 
     g.setColor(this.c); 
     int x,y; 

     for(y = 10; y<= 330; y+=35){ 
      for(x =10; x<=120; x+=35){ 
       g.drawRect(x, y, 25, 25); 
      } 
     } 
    } 
    public void setRectColor(Color c){ 
     this.c=c; 
    } 
} 

import javax.swing.*;  

public class testRect{ 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     Mastermind frame = new Mastermind(); 

     frame.setTitle("Mastermind"); 
     frame.setSize(334, 500); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 
+0

したがって、すべてのJPanelsをnewPanelクラスに置き換える必要がありますか? – Matt

+1

この宿題はありますか? – eabraham

+0

これは宿題ではありませんが、私は別のフォーラムからそのアイデアを得ました。私はguiに新しいと試して楽しいプロジェクトのように聞こえました – Matt

関連する問題