有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java编程故障操作listenersFillRect

我正在尝试用JavaGUI格式重新创建游戏策划。我的问题源于actionlistener无法访问我的NewPanel类。任何帮助都会很好,我真的被困在这上面了

    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);
            }
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    我有可以根据按钮改变颜色的盒子。我不知道你想从这里开始,但这应该是一个好的开始

    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);
        }
    }