Java 所选变量不';乒乓球比赛-秋千

Java 所选变量不';乒乓球比赛-秋千,java,swing,pong,Java,Swing,Pong,我正试着用挥杆做一个乒乓球游戏。我有一个菜单屏幕,你可以改变你的喜好,如音乐,游戏速度,你可以选择多人游戏选项等。但当我做出选择时,它们的值不会改变。我有我的主菜单课和计算机课 例如:我想改变难度,这意味着改变人工智能的速度,但我没能做到。我期待着一个答案,也许很简单,但我看不到 这是我的代码: public class MenuPanel { ScoreBoard sc = new ScoreBoard(); private JFrame mainFrame; priv

我正试着用挥杆做一个乒乓球游戏。我有一个菜单屏幕,你可以改变你的喜好,如音乐,游戏速度,你可以选择多人游戏选项等。但当我做出选择时,它们的值不会改变。我有我的主菜单课和计算机课

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

这是我的代码:

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());        
        }   
    }
}
我的电脑。课堂:

公共类计算机
{   
私人游戏场;
私家车y=Pong.WINDOW_高度/2;
私有int yCh=0;
私人整数=10;
私人整数向下=10;
私有整数宽度=20;
私人室内高度=90;
布尔值=false;
公共计算机(){
}
公共空间更新(球b){
如果(y+高度>舷窗高度)
{
y=舷窗高度-高度;
}
else if(y<0)
{
y=0;
}
如果(!isTwoPlayer){
如果(b.getY()=0)
{
yCh=上升;
}

如果(b.getY()>y&&y+height我不知道整个程序是如何构造的。但是在这段代码中:

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

        optionsFrame.dispose();
        Pong pong = new Pong();
创建
Pong
类的新实例,并设置新创建实例的值


太长了,读不下去了。这个例子可能不在其他地方使用。您可能需要使用已经使用的<代码> Pong < /C> >,或者让其他类使用新创建的实例。

欢迎来到TL;DR;请发布。
doneButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

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