java - 选定的变量在乒乓球游戏中不会改变 - Swing

标签 java swing pong

我正在尝试使用Swing制作乒乓球游戏。我有一个菜单屏幕,您可以在其中更改您的偏好,例如音乐、游戏速度,并且您可以选择多人游戏选项等。但是当我做出选择时,它们的值不会改变。我有主菜单课和计算机课。

例如:我想改变难度,这意味着改变人工智能的速度,但我无法做到。我期待着答案,也许这很简单,但我看不到。

这是我的代码:

public class MenuPanel
{
    ScoreBoard sc = new ScoreBoard();
    private JFrame mainFrame;
    private JFrame optionsFrame;
    private JPanel menuPanel;
    private JPanel optionsPanel;
    private JFrame gameEndFrame;
    private JPanel gameEndPanel;
    JCheckBox twoPlayer;

    // creating the swing components and containers,
    // there are two frames to swap between them, 
    // we tried to use cardLayout but it didn't work for us. 

    public MenuPanel() {
        mainFrame = new JFrame("Welcome To Pong Game");
        mainFrame.setSize(700,700);
        mainFrame.setLayout(new CardLayout());
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        menuPanel = new JPanel(null);
        menuPanel.setSize(700,700);
        menuPanel.setVisible(true);
        mainFrame.add(menuPanel);

        optionsFrame = new JFrame("Settings");
        optionsFrame.setSize(700, 700);
        optionsFrame.setLayout(new CardLayout());
        optionsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel optionsPanel = new JPanel(null) ;
        optionsPanel.setSize(700,700);
        optionsPanel.setVisible(true);
        optionsFrame.add(optionsPanel);

        // mainPanel components
        ImageIcon ic1 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\Actions-player-play-icon.png");
        ImageIcon ic2 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\Settings-L-icon.png");
        ImageIcon ic3 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\cup-icon.png");
        ImageIcon ic4 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\Button-Close-icon.png");
        ImageIcon ic5 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\ice-2025937_960_720.jpg");
        ImageIcon ic6 = new ImageIcon("C:\\Users\\Onur17\\Downloads\\tenis1.png");

        JLabel mainLabel = new JLabel();
        Font font = new Font("Papyrus", Font.BOLD,26);
        mainLabel.setFont(font);
        mainLabel.setForeground(Color.RED);
        mainLabel.setText("PONG GAME");

        JButton startButton = new JButton("Start Game");
        JButton optionsButton = new JButton("Options");
        JButton leaderButton = new JButton("Leaderboard");
        JButton exitButton = new JButton("Exit");

        JButton icb1 = new JButton(ic1);
        JButton icb2 = new JButton(ic2);
        JButton icb3 = new JButton(ic3);
        JButton icb4 = new JButton(ic4);
        JButton icb6 = new JButton(ic6);

        // at first, we tried to keep our buttons and labels in panels 
        // but then we didn't add an image to label as the background
        // so we created a JLabel to set its image as the background, we may remove it later.

        JLabel mn = new JLabel(ic5);
        mn.setBackground(Color.BLUE);
        mn.setBounds(1, 1, 700, 700);

        Font font3 = new Font("Calibri", Font.PLAIN, 20);
        twoPlayer = new JCheckBox();
        twoPlayer.setFont(font3);
        twoPlayer.setForeground(Color.DARK_GRAY);
        twoPlayer.setText("       MultiPlayer");
        twoPlayer.setBounds(200, 350, 220, 40);

        menuPanel.add(mn);
        mn.add(mainLabel);
        mn.add(startButton);
        mn.add(optionsButton);
        mn.add(leaderButton);
        mn.add(exitButton);
        mn.add(icb1);
        mn.add(icb2);
        mn.add(icb3);
        mn.add(icb4);
        mn.add(icb6);
        mn.add(twoPlayer);



        mainFrame.setVisible(true); 

        // the components added by their coordinates to make them look formal

        mainLabel.setBounds(210, 100, 220, 40);
        startButton.setBounds(200, 150, 220, 40);
        optionsButton.setBounds(200, 200, 220, 40);
        leaderButton.setBounds(200, 250, 220, 40);
        exitButton.setBounds(200, 300, 220, 40);

        icb1.setBounds(150, 150, 40, 40);
        icb2.setBounds(150, 200, 40, 40);
        icb3.setBounds(150, 250, 40, 40);
        icb4.setBounds(150, 300, 40, 40);
        icb6.setBounds(150,350,40,40);

        startButton.setBorder(BorderFactory.createRaisedBevelBorder());
        optionsButton.setBorder(BorderFactory.createRaisedBevelBorder());
        leaderButton.setBorder(BorderFactory.createRaisedBevelBorder());
        exitButton.setBorder(BorderFactory.createRaisedBevelBorder());

        // optionsPanel components
        JButton doneButton = new JButton("Done");
        doneButton.setBounds(150,330,220,40);
        doneButton.setBorder(BorderFactory.createRaisedBevelBorder());

        Font font1 = new Font("Calibri", Font.PLAIN,24);
        Font font2 = new Font("Calibri", Font.BOLD,19);
        JLabel st = new JLabel();
        st.setFont(font1);
        st.setForeground(Color.DARK_GRAY);
        st.setText("-OPTIONS-");

        JLabel difficulty = new JLabel("Difficulty: ");
        difficulty.setFont(font2);
        difficulty.setForeground(Color.DARK_GRAY);

        JLabel music = new JLabel("Sound: ");
        music.setFont(font2);
        music.setForeground(Color.DARK_GRAY);

        JLabel gSpeed = new JLabel("Game Speed: ");
        gSpeed.setFont(font2);
        gSpeed.setForeground(Color.DARK_GRAY);

        JLabel screen = new JLabel(ic5);
        screen.setBackground(Color.BLUE);
        screen.setBounds(1, 1, 700, 700);

        JRadioButton rb1 = new JRadioButton("Easy");
        JRadioButton rb2 = new JRadioButton("Normal");
        JRadioButton rb3 = new JRadioButton("Hard");
        JRadioButton rb4 = new JRadioButton("On");
        JRadioButton rb5 = new JRadioButton("Off");
        JRadioButton rb6 = new JRadioButton("x");
        JRadioButton rb7 = new JRadioButton("2x");
        JRadioButton rb8 = new JRadioButton("3x");

        ButtonGroup bg1 = new ButtonGroup();
        ButtonGroup bg2 = new ButtonGroup();
        ButtonGroup bg3 = new ButtonGroup();
        bg1.add(rb1);
        bg1.add(rb2);
        bg1.add(rb3);
        bg2.add(rb4);
        bg2.add(rb5);
        bg3.add(rb6);
        bg3.add(rb7);
        bg3.add(rb8);

        optionsPanel.add(screen);
        screen.add(difficulty);
        screen.add(st);
        screen.add(gSpeed);
        screen.add(rb1);
        screen.add(rb2);
        screen.add(rb3);
        screen.add(rb4);
        screen.add(rb5);
        screen.add(rb6);
        screen.add(rb7);
        screen.add(rb8);
        screen.add(music);
        screen.add(doneButton);

        st.setBounds(200,100,220,40);
        difficulty.setBounds(100,150,90,40);
        music.setBounds(100,200,90,40);
        gSpeed.setBounds(100, 250, 120, 40);

        rb1.setBounds(200,150,60,40);
        rb2.setBounds(260,150,80,40);
        rb3.setBounds(340, 150, 60, 40);
        rb4.setBounds(200, 200, 50, 40);
        rb5.setBounds(250,200,50,40);
        rb6.setBounds(220, 250, 50, 40);
        rb7.setBounds(270, 250, 50, 40);
        rb8.setBounds(320, 250, 50, 40);

        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {        
                Pong pong = new Pong();
                sound("Marimba-music");

                mainFrame.dispose();

                if(twoPlayer.isSelected()) {
                    pong.c.isTwoPlayer = true;
                }
                else {
                    pong.c.isTwoPlayer = false;
                }
            }       
        });

        optionsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mainFrame.setVisible(false);
                optionsFrame.setVisible(true);
            }
        });

        doneButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                optionsFrame.dispose();
                Pong pong = new Pong();

                if(rb1.isSelected())
                {
                    pong.c.setUp(-10);;
                    pong.c.setDown(10);
                }
                else if(rb2.isSelected())
                {
                    pong.c.setUp(-11);
                    pong.c.setDown(11);
                }
                else if(rb3.isSelected())
                {
                    pong.c.setUp(-30);
                    pong.c.setDown(30);
                }

                if(rb4.isSelected())
                {
                    sound("Marimba-music");
                }
                else if(rb5.isSelected())
                {
                    soundStop("Marimba-music");
                }

                if(rb6.isSelected())
                {
                    GamePanel g = new GamePanel();
                    g.x = 50;
                }
                else if(rb7.isSelected())
                {
                    GamePanel g = new GamePanel();
                    g.x = 75;
                }
                else if(rb8.isSelected())
                {
                    GamePanel g = new GamePanel();
                    g.x = 100;
                    System.out.println(g.x);
                }           

            }
        }); 

        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
               mainFrame.dispose();
            }
        });
    }

    public void sound(String nm)
    {
        AudioStream BGM;

        try {
            InputStream test = new FileInputStream("./" +nm+".wav");
            BGM = new AudioStream(test);

            AudioPlayer.player.start(BGM);      
        }
        catch(IOException error) {
            JOptionPane.showMessageDialog(null, error.getMessage());
        }
    }

    public void soundStop(String nm)
    {
        AudioStream BGM;

        try {
            InputStream test = new FileInputStream("./" +nm+".wav");
            BGM = new AudioStream(test);

            AudioPlayer.player.stop(BGM);
        }
        catch(IOException error) {
            JOptionPane.showMessageDialog(null, error.getMessage());        
        }   
    }
}

我的Computer.class:

public class Computer
{   
    private GamePanel field;

    private int y = Pong.WINDOW_HEIGHT / 2;
    private int yCh = 0;

    private int up = -10;
    private int down = 10;

    private int width = 20;
    private int height = 90;
    boolean isTwoPlayer = false;

    public Computer() {

    }

    public void update(Ball b) {
        if(y + height > Pong.WINDOW_HEIGHT)
        {
            y = Pong.WINDOW_HEIGHT - height;
        }
        else if(y < 0)
        {
            y = 0;
        }

        if(!isTwoPlayer) {
            if(b.getY() < y+height && y >= 0)
            {
                yCh = up;
            }
            else if(b.getY() > y && y + height <= Pong.WINDOW_HEIGHT)
            {
                yCh = down; 
            }
            y = y + yCh;
        } 
        else {
            y = y + yCh;
        } 
    }

    public void setYPosition(int speed) {
        yCh = speed;
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRoundRect(450, y, width, height, 10, 10);
    }

    public int getX() {
        return 450;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getUp() {
        return up;
    }

    public int getDown() {
        return down;
    }

    public void setUp(int up) {
        this.up = up;
    }

    public void setDown(int down) {
        this.down = down;
    }

最佳答案

我不知道你的整个程序是如何构建的。但在这段代码中:

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

        optionsFrame.dispose();
        Pong pong = new Pong();

您创建 Pong 类的新实例并设置新创建的实例的值。

此实例未在其他任何地方使用。您可能需要使用已在使用的 Pong 实例,或者让其他类使用新创建的实例。

关于java - 选定的变量在乒乓球游戏中不会改变 - Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43292543/

相关文章:

java - 使用 TextAction 实现带有 actionPerformed 的 JMenu

java - 表单布局调用 JColorChooser

java - Thread.join 似乎在我的代码中不起作用;我用得对吗?

java - 在 JDBC 中访问 ResultSet 时,是否有一种优雅的方法来区分空值和实际零值?

java - 配置 hibernate.cfg.xml 文件

java - 是否有可能在 O(1) 中得到 m 个字符长度组合的第 k 个元素?

java - IconImage 未显示在 jButton 上

Swift Pong(无 SpriteKit): Detect side of paddle that the ball hit

python - 当球与 Racket 碰撞时会停止,并在乒乓游戏中移动 Racket 后弹开

JavaScript,无法绘制乒乓球中的 Racket 和球。有人可以解释一下为什么吗?