使用变量来决定一个类 Java 中多个类的数组长度



我在记忆游戏中为每个级别(简单 4*4、中等 6*4、困难 6*6)制作了 3 个类。它们由相同的代码组成,但是它们具有不同数量的数组长度和其他变量。我想通过将三个类组合成一个类来提高代码效率,您有什么建议吗?如果有帮助,我已经粘贴了下面的三个不同的类:)

来自 3 个不同类的数组和变量:

JButton[] button = new JButton[16];
JButton[] button = new JButton[24];
JButton[] button = new JButton[36];
int[] StoreCards = new int[16];
int[] StoreCards = new int[24];
int[] StoreCards = new int[36];
static int[] card = new int[9];
static int[] card = new int[13];
static int[] card = new int[19];

简易级别:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
class EasyWindow extends JFrame implements ActionListener, MouseListener {
    JLabel Score = new JLabel("Score: - ");
    JLabel Welcome = new JLabel("Welcome " + StartWindow.user + "!");
    ImageIcon Back = new ImageIcon("mback.png");
    ImageIcon musicicon = new ImageIcon("musicicon.png");
    ImageIcon themeicon = new ImageIcon("themeicon.png");
    ImageIcon difficultyicon = new ImageIcon("difficulty.png");
    ImageIcon pointsicon = new ImageIcon("pointsicon.png");
    ImageIcon studentsicon = new ImageIcon("studentsicon.png");
    JButton AnOtherLevel = new JButton(
            "Click here if you want to change level.");
    JButton Quit = new JButton("Quit Game!");
    JButton[] button = new JButton[16];
    JMenuBar menuBar = new JMenuBar();
    JMenu Settings = new JMenu("Settings");
    JMenu Theme = new JMenu("Theme");
    JMenu Rules = new JMenu("Rules");
    JMenu Creators = new JMenu("Creators");
    JMenuItem Music = new JMenuItem("Music", musicicon);
    JMenuItem Celebrities = new JMenuItem("Celebrities", themeicon);
    JMenuItem Cities = new JMenuItem("Cities", themeicon);
    JMenuItem Memes = new JMenuItem("Memes", themeicon);
    JMenuItem Difficulty = new JMenuItem("Difficulty", difficultyicon);
    JMenuItem Points = new JMenuItem("Points", pointsicon);
    JMenuItem Ava = new JMenuItem("Ava Baghchesara", studentsicon);
    JMenuItem Michelle = new JMenuItem("Michelle Bill", studentsicon);
    int[] StoreCards = new int[16];
    static int[] cardChecker = new int[2];
    static int[] card = new int[9];
    int[] Button = new int[2];
    static int flipped = 0;
    static int score = 0;
    String imageType = ".png";
    String back = ".png";
    JPanel Top = new JPanel(new GridLayout(1, 1, 5, 15));
    JPanel Center = new JPanel(new GridLayout(4, 4, 5, 5));
    JPanel Bottom = new JPanel(new GridLayout(1, 2, 0, 0));
    JPanel Right = new JPanel(new GridLayout(2, 2, 0, 0));
    JPanel Left = new JPanel(new GridLayout(1, 1, 0, 0));
    static Container contentArea;
    public EasyWindow() {
        super("User: " + StartWindow.user + " || Easy Level");
        setSize(600, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        setLayout(new BorderLayout());
        setVisible(true);
        AnOtherLevel.addActionListener(this);
        Quit.addActionListener(this);
        AnOtherLevel.addMouseListener(this);
        Quit.addMouseListener(this);
        AnOtherLevel.setBackground(Color.white);
        AnOtherLevel.setForeground(Color.BLACK);
        Quit.setBackground(Color.white);
        Quit.setForeground(Color.BLACK);
        add(Top, BorderLayout.NORTH);
        add(Left, BorderLayout.WEST);
        add(Center, BorderLayout.CENTER);
        add(Right, BorderLayout.EAST);
        add(Bottom, BorderLayout.SOUTH);
        Welcome.setFont(new Font("Serif", Font.PLAIN, 30));
        Welcome.setHorizontalAlignment(SwingConstants.CENTER);
        Welcome.setVerticalAlignment(SwingConstants.CENTER);
        Top.add(Welcome);
        Top.setBackground(Color.white);
        Center.setBackground(Color.white);
        Right.setBackground(Color.white);
        Right.add(Score);
        Bottom.add(AnOtherLevel);
        Bottom.add(Quit);
        Bottom.setBackground(Color.white);
        for (int n = 0; n <= button.length - 1; n++) {
            button[n] = new JButton();
            Center.add(button[n]);
            button[n].addActionListener(this);
            button[n].setBackground(Color.white);
        }
        contentArea = getContentPane();
        contentArea.add("North", Top);
        contentArea.add("Center", Center);
        contentArea.add("South", Bottom);
        menuBar.add(Settings);
        menuBar.add(Rules);
        menuBar.add(Creators);
        setJMenuBar(menuBar);
        Music.addActionListener(this);
        Theme.addActionListener(this);
        Celebrities.addActionListener(this);
        Cities.addActionListener(this);
        Memes.addActionListener(this);
        Difficulty.addActionListener(this);
        Points.addActionListener(this);
        Ava.addActionListener(this);
        Michelle.addActionListener(this);
        Settings.add(Music);
        Settings.add(Theme);
        Theme.add(Celebrities);
        Theme.add(Cities);
        Theme.add(Memes);
        Rules.add(Difficulty);
        Rules.add(Points);
        Creators.add(Ava);
        Creators.add(Michelle);
        Game();
        flipped = 3;
        Reset();
        setContentPane(contentArea);
        contentArea.setBackground(Color.white);
    }
    public void Game() {
        int number = 0;
        int x = 0;
        ImageIcon image[] = new ImageIcon[15];
        while (x < 16) {
            number = (int) RandomNumbers.GetRandomNumber(8);
            image[number] = new ImageIcon(number + imageType);
            if (card[number] < 2) {
                card[number]++;
                StoreCards[x] = number;
                System.out.println(number + " Number" + "card nr " + x);
                x++;
            }
        }
    }
    public void Reset() {
        if (flipped > 2) {
            flipped = 0;
            for (int n = 0; n <= button.length - 1; n++) {
                button[n].setIcon(Back);
            }
        }
    }
    public void Check(int number) {
        if (cardChecker[0] == cardChecker[1]) {
            score = score + 2;
            Score.setText("Score: " + score);
            DisableButtons();
        } else {
            System.out.println("jj");
        }
        if (score == 16) {
            setVisible(false);
            new EndWindow1();
        }
    }
    public void Card1and2(int number, int button) {
        if (flipped == 0) {
            cardChecker[0] = number;
            Button[0] = button;
        }
        if (flipped == 1) {
            cardChecker[1] = number;
            Button[1] = button;
            if (StoreCards[cardChecker[0]] == StoreCards[cardChecker[1]]) {
                if (Button[0] != Button[1])
                    Check(number);
            }
        }
    }
    public void DisableButtons() {
        for (int n = 0; n <= button.length; n++) {
            if (Button[0] == n || Button[1] == n) {
                button[n].setVisible(false);
            }
        }
    }
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == AnOtherLevel) {
            setVisible(false);
            new AnOtherWindow();
        }
        if (event.getSource() == Quit) {
            System.exit(0);
        }
        for (int n = 0; n <= button.length - 1; n++) {
            if (event.getSource() == button[n]) {
                int number = StoreCards[n];
                button[n].setIcon(new ImageIcon(number + imageType));
                Card1and2(number, n);
                flipped++;
                Reset();
            }
        }
        if (event.getSource() == Celebrities) {
            Back = new ImageIcon("ceback.png");
            imageType = "c.png";
        }
        if (event.getSource() == Cities) {
            Back = new ImageIcon("ciback.png");
            imageType = ".jpg";
        }
        if (event.getSource() == Memes) {
            Back = new ImageIcon("mback.png");
            imageType = ".png";
        }
    }
    public void mouseEntered(MouseEvent event) {
        if (event.getSource() == AnOtherLevel) {
            AnOtherLevel.setBackground(Color.lightGray);
            AnOtherLevel.setForeground(Color.BLACK);
        }
        if (event.getSource() == Quit) {
            Quit.setBackground(Color.lightGray);
            Quit.setForeground(Color.BLACK);
        }
    }
    public void mouseClicked(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
        AnOtherLevel.setBackground(Color.white);
        AnOtherLevel.setForeground(Color.BLACK);
        Quit.setBackground(Color.white);
        Quit.setForeground(Color.BLACK);
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
}
public class EasyLevelWindow {
    public static void main(String[] args) {
        EasyWindow win = new EasyWindow();
    }

中级:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
class MediumWindow extends JFrame implements ActionListener, MouseListener {
    JLabel Score = new JLabel("Score: - ");
    JLabel Welcome = new JLabel("Welcome " + StartWindow.user + "!");
    ImageIcon Back = new ImageIcon("mback.png");
    ImageIcon musicicon = new ImageIcon("musicicon.png");
    ImageIcon themeicon = new ImageIcon("themeicon.png");
    ImageIcon difficultyicon = new ImageIcon("difficulty.png");
    ImageIcon pointsicon = new ImageIcon("pointsicon.png");
    ImageIcon studentsicon = new ImageIcon("studentsicon.png");
    JButton AnOtherLevel = new JButton(
            "Click here if you want to change level.");
    JButton Quit = new JButton("Quit Game!");
    JButton[] button = new JButton[24];
    JMenuBar menuBar = new JMenuBar();
    JMenu Settings = new JMenu("Settings");
    JMenu Theme = new JMenu("Theme");
    JMenu Rules = new JMenu("Rules");
    JMenu Creators = new JMenu("Creators");
    JMenuItem Music = new JMenuItem("Music", musicicon);
    JMenuItem Celebrities = new JMenuItem("Celebrities", themeicon);
    JMenuItem Cities = new JMenuItem("Cities", themeicon);
    JMenuItem Memes = new JMenuItem("Memes", themeicon);
    JMenuItem Difficulty = new JMenuItem("Difficulty", difficultyicon);
    JMenuItem Points = new JMenuItem("Points", pointsicon);
    JMenuItem Ava = new JMenuItem("Ava Baghchesara", studentsicon);
    JMenuItem Michelle = new JMenuItem("Michelle Bill", studentsicon);
    int[] StoreCards = new int[24];
    static int[] cardChecker = new int[2];
    static int[] card = new int[13];
    int[] Button = new int[2];
    static int flipped = 0;
    static int score = 0;
    String imageType = ".png";
    String back = ".png";
    JPanel Top = new JPanel(new GridLayout(2, 1, 5, 15));
    JPanel Center = new JPanel(new GridLayout(6, 4, 5, 5));
    JPanel Bottom = new JPanel(new GridLayout(1, 1, 0, 0));
    JPanel Right = new JPanel(new GridLayout(2, 2, 0, 0));
    JPanel Left = new JPanel(new GridLayout(1, 1, 0, 0));
    static Container contentArea;
    public MediumWindow() {
        super("User: " + StartWindow.user + " || Medium Level");
        setSize(600, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        setLayout(new BorderLayout());
        setVisible(true);
        AnOtherLevel.addActionListener(this);
        Quit.addActionListener(this);
        AnOtherLevel.addMouseListener(this);
        Quit.addMouseListener(this);
        AnOtherLevel.setBackground(Color.white);
        AnOtherLevel.setForeground(Color.BLACK);
        Quit.setBackground(Color.white);
        Quit.setForeground(Color.BLACK);
        add(Top, BorderLayout.NORTH);
        add(Left, BorderLayout.WEST);
        add(Center, BorderLayout.CENTER);
        add(Right, BorderLayout.EAST);
        add(Bottom, BorderLayout.SOUTH);
        Welcome.setFont(new Font("Serif", Font.PLAIN, 30));
        Welcome.setHorizontalAlignment(SwingConstants.CENTER);
        Welcome.setVerticalAlignment(SwingConstants.CENTER);
        Top.add(Welcome);
        Top.setBackground(Color.white);
        Center.setBackground(Color.white);
        Right.setBackground(Color.white);
        Right.add(Score);
        Bottom.add(AnOtherLevel);
        Bottom.add(Quit);
        Bottom.setBackground(Color.white);
        for (int n = 0; n <= button.length - 1; n++) {
            button[n] = new JButton();
            Center.add(button[n]);
            button[n].addActionListener(this);
            button[n].setBackground(Color.white);
        }
        contentArea = getContentPane();
        contentArea.add("North", Top);
        contentArea.add("Center", Center);
        contentArea.add("South", Bottom);
        menuBar.add(Settings);
        menuBar.add(Rules);
        menuBar.add(Creators);
        setJMenuBar(menuBar);
        Music.addActionListener(this);
        Theme.addActionListener(this);
        Celebrities.addActionListener(this);
        Cities.addActionListener(this);
        Memes.addActionListener(this);
        Difficulty.addActionListener(this);
        Points.addActionListener(this);
        Ava.addActionListener(this);
        Michelle.addActionListener(this);
        Settings.add(Music);
        Settings.add(Theme);
        Theme.add(Celebrities);
        Theme.add(Cities);
        Theme.add(Memes);
        Rules.add(Difficulty);
        Rules.add(Points);
        Creators.add(Ava);
        Creators.add(Michelle);
        Game();
        flipped = 3;
        Reset();
        setContentPane(contentArea);
        contentArea.setBackground(Color.white);
    }
    public void Game() {
        int number = 0;
        int x = 0;
        ImageIcon image[] = new ImageIcon[23];
        while (x < 24) {
            number = (int) RandomNumbers.GetRandomNumber(12);
            image[number] = new ImageIcon(number + imageType);
            if (card[number] < 2) {
                card[number]++;
                StoreCards[x] = number;
                System.out.println(number + " Number" + "card nr " + x);
                x++;
            }
        }
    }
    public void Reset() {
        if (flipped > 2) {
            flipped = 0;
            for (int n = 0; n <= button.length - 1; n++) {
                button[n].setIcon(Back);
            }
        }
    }
    public void Check(int number) {
        if (cardChecker[0] == cardChecker[1]) {
            score = score + 2;
            Score.setText("Score: " + score);
            DisableButtons();
        } else {
            System.out.println("jj");
        }
        if (score == 24) {
            setVisible(false);
            new EndWindow1();
        }
    }
    public void Card1and2(int number, int button) {
        if (flipped == 0) {
            cardChecker[0] = number;
            Button[0] = button;
        }
        if (flipped == 1) {
            cardChecker[1] = number;
            Button[1] = button;
            if (StoreCards[cardChecker[0]] == StoreCards[cardChecker[1]]) {
                if (Button[0] != Button[1])
                    Check(number);
            }
        }
    }
    public void DisableButtons() {
        for (int n = 0; n <= button.length; n++) {
            if (Button[0] == n || Button[1] == n) {
                button[n].setVisible(false);
            }
        }
    }
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == AnOtherLevel) {
            setVisible(false);
            new AnOtherWindow();
        }
        if (event.getSource() == Quit) {
            System.exit(0);
        }
        for (int n = 0; n <= button.length - 1; n++) {
            if (event.getSource() == button[n]) {
                int number = StoreCards[n];
                button[n].setIcon(new ImageIcon(number + imageType));
                Card1and2(number, n);
                flipped++;
                Reset();
            }
        }
        if (event.getSource() == Celebrities) {
            Back = new ImageIcon("ceback.png");
            imageType = "c.png";
        }
        if (event.getSource() == Cities) {
            Back = new ImageIcon("ciback.png");
            imageType = ".jpg";
        }
        if (event.getSource() == Memes) {
            Back = new ImageIcon("mback.png");
            imageType = ".png";
        }
    }
    public void mouseEntered(MouseEvent event) {
        if (event.getSource() == AnOtherLevel) {
            AnOtherLevel.setBackground(Color.lightGray);
            AnOtherLevel.setForeground(Color.BLACK);
        }
        if (event.getSource() == Quit) {
            Quit.setBackground(Color.lightGray);
            Quit.setForeground(Color.BLACK);
        }
    }
    public void mouseClicked(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
        AnOtherLevel.setBackground(Color.white);
        AnOtherLevel.setForeground(Color.BLACK);
        Quit.setBackground(Color.white);
        Quit.setForeground(Color.BLACK);
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
}
public class MediumLevelWindow {
    public static void main(String[] args) {
        MediumWindow win = new MediumWindow();
    }
}

硬级别:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    class HardWindow extends JFrame implements ActionListener, MouseListener {
        JLabel Score = new JLabel("Score: - ");
        JLabel Welcome = new JLabel("Welcome " + StartWindow.user + "!");
        ImageIcon Back = new ImageIcon("mback.png");
        ImageIcon musicicon = new ImageIcon("musicicon.png");
        ImageIcon themeicon = new ImageIcon("themeicon.png");
        ImageIcon difficultyicon = new ImageIcon("difficulty.png");
        ImageIcon pointsicon = new ImageIcon("pointsicon.png");
        ImageIcon studentsicon = new ImageIcon("studentsicon.png");
        JButton AnOtherLevel = new JButton(
                "Click here if you want to change level.");
        JButton Quit = new JButton("Quit Game!");
        JButton[] button = new JButton[36];
        JMenuBar menuBar = new JMenuBar();
        JMenu Settings = new JMenu("Settings");
        JMenu Theme = new JMenu("Theme");
        JMenu Rules = new JMenu("Rules");
        JMenu Creators = new JMenu("Creators");
        JMenuItem Music = new JMenuItem("Music", musicicon);
        JMenuItem Celebrities = new JMenuItem("Celebrities", themeicon);
        JMenuItem Cities = new JMenuItem("Cities", themeicon);
        JMenuItem Memes = new JMenuItem("Memes", themeicon);
        JMenuItem Difficulty = new JMenuItem("Difficulty", difficultyicon);
        JMenuItem Points = new JMenuItem("Points", pointsicon);
        JMenuItem Ava = new JMenuItem("Ava Baghchesara", studentsicon);
        JMenuItem Michelle = new JMenuItem("Michelle Bill", studentsicon);
        int[] StoreCards = new int[36];
        static int[] cardChecker = new int[2];
        static int[] card = new int[19];
        int[] Button = new int[2];
        static int flipped = 0;
        static int score = 0;
        String imageType = ".png";
        String back = ".png";
        JPanel Top = new JPanel(new GridLayout(2, 1, 5, 15));
        JPanel Center = new JPanel(new GridLayout(6, 6, 5, 5));
        JPanel Bottom = new JPanel(new GridLayout(1, 1, 0, 0));
        JPanel Right = new JPanel(new GridLayout(2, 2, 0, 0));
        JPanel Left = new JPanel(new GridLayout(1, 1, 0, 0));
        static Container contentArea;
        public HardWindow() {
            super("User: " + StartWindow.user + " || Hard Level");
            setSize(780, 730);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(true);
            setLayout(new BorderLayout());
            setVisible(true);
            AnOtherLevel.addActionListener(this);
            Quit.addActionListener(this);
            AnOtherLevel.addMouseListener(this);
            Quit.addMouseListener(this);
            AnOtherLevel.setBackground(Color.white);
            AnOtherLevel.setForeground(Color.BLACK);
            Quit.setBackground(Color.white);
            Quit.setForeground(Color.BLACK);
            add(Top, BorderLayout.NORTH);
            add(Left, BorderLayout.WEST);
            add(Center, BorderLayout.CENTER);
            add(Right, BorderLayout.EAST);
            add(Bottom, BorderLayout.SOUTH);
            Welcome.setFont(new Font("Serif", Font.PLAIN, 30));
            Welcome.setHorizontalAlignment(SwingConstants.CENTER);
            Welcome.setVerticalAlignment(SwingConstants.CENTER);
            Top.add(Welcome);
            Top.setBackground(Color.white);
            Center.setBackground(Color.white);
            Right.setBackground(Color.white);
            Right.add(Score);
            Bottom.add(AnOtherLevel);
            Bottom.add(Quit);
            Bottom.setBackground(Color.white);
            for (int n = 0; n <= button.length - 1; n++) {
                button[n] = new JButton();
                Center.add(button[n]);
                button[n].addActionListener(this);
                button[n].setBackground(Color.white);
            }
            contentArea = getContentPane();
            contentArea.add("North", Top);
            contentArea.add("Center", Center);
            contentArea.add("South", Bottom);
            menuBar.add(Settings);
            menuBar.add(Rules);
            menuBar.add(Creators);
            setJMenuBar(menuBar);
            Music.addActionListener(this);
            Theme.addActionListener(this);
            Celebrities.addActionListener(this);
            Cities.addActionListener(this);
            Memes.addActionListener(this);
            Difficulty.addActionListener(this);
            Points.addActionListener(this);
            Ava.addActionListener(this);
            Michelle.addActionListener(this);
            Settings.add(Music);
            Settings.add(Theme);
            Theme.add(Celebrities);
            Theme.add(Cities);
            Theme.add(Memes);
            Rules.add(Difficulty);
            Rules.add(Points);
            Creators.add(Ava);
            Creators.add(Michelle);
            Game();
            flipped = 3;
            Reset();
            setContentPane(contentArea);
            contentArea.setBackground(Color.white);
        }
        public void Game() {
            int number = 0;
            int x = 0;
            ImageIcon image[] = new ImageIcon[35];
            while (x < 36) {
                number = (int) RandomNumbers.GetRandomNumber(18);
                image[number] = new ImageIcon(number + imageType);
                if (card[number] < 2) {
                    card[number]++;
                    StoreCards[x] = number;
                    System.out.println(number + " Number" + "card nr " + x);
                    x++;
                }
            }
        }
        public void Reset() {
            if (flipped > 2) {
                flipped = 0;
                for (int n = 0; n <= button.length - 1; n++) {
                    button[n].setIcon(Back);
                }
            }
        }
        public void Check(int number) {
            if (cardChecker[0] == cardChecker[1]) {
                score = score + 2;
                Score.setText("Score: " + score);
                DisableButtons();
            } else {
                System.out.println("jj");
            }
            if (score == 36) {
                setVisible(false);
                new EndWindow1();
            }
        }
        public void Card1and2(int number, int button) {
            if (flipped == 0) {
                cardChecker[0] = number;
                Button[0] = button;
            }
            if (flipped == 1) {
                cardChecker[1] = number;
                Button[1] = button;
                if (StoreCards[cardChecker[0]] == StoreCards[cardChecker[1]]) {
                    if (Button[0] != Button[1])
                        Check(number);
                }
            }
        }
        public void DisableButtons() {
            for (int n = 0; n <= button.length; n++) {
                if (Button[0] == n || Button[1] == n) {
                    button[n].setVisible(false);
                }
            }
        }
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == AnOtherLevel) {
                setVisible(false);
                new AnOtherWindow();
            }
            if (event.getSource() == Quit) {
                System.exit(0);
            }
            for (int n = 0; n <= button.length - 1; n++) {
                if (event.getSource() == button[n]) {
                    int number = StoreCards[n];
                    button[n].setIcon(new ImageIcon(number + imageType));
                    Card1and2(number, n);
                    flipped++;
                    Reset();
                }
            }
            if (event.getSource() == Celebrities) {
                Back = new ImageIcon("ceback.png");
                imageType = "c.png";
            }
            if (event.getSource() == Cities) {
                Back = new ImageIcon("ciback.png");
                imageType = ".jpg";
            }
            if (event.getSource() == Memes) {
                Back = new ImageIcon("mback.png");
                imageType = ".png";
            }
        }
        public void mouseEntered(MouseEvent event) {
            if (event.getSource() == AnOtherLevel) {
                AnOtherLevel.setBackground(Color.lightGray);
                AnOtherLevel.setForeground(Color.BLACK);
            }
            if (event.getSource() == Quit) {
                Quit.setBackground(Color.lightGray);
                Quit.setForeground(Color.BLACK);
            }
        }
        public void mouseClicked(MouseEvent e) {
        }
        public void mouseExited(MouseEvent e) {
            AnOtherLevel.setBackground(Color.white);
            AnOtherLevel.setForeground(Color.BLACK);
            Quit.setBackground(Color.white);
            Quit.setForeground(Color.BLACK);
        }
        public void mousePressed(MouseEvent e) {
        }
        public void mouseReleased(MouseEvent e) {
        }
    }
    public class HardLevelWindow {
        public static void main(String[] args) {
            HardWindow win = new HardWindow();

}
}

您可以创建一个抽象的Parent类,例如:BaseGame。诸如此类:

    public abstract class BaseGame {
        private abstract int[] getStoreCards();
        private abstract JButton[] getButtons();
        private abstract int[] getCards();
        ... your other code goes here
    }

然后,您可以在其中创建实现这些方法的子类,例如:

public class EasyGame {
    @Override
    private int[] getStoreCards() {
        return new int[16];
    }
    @Override
    private int[] getCards() {
        return new int[9];
    }
    @Override
    private JButton[] getButtons() {
        return new JButton[16];
    }
}

因此,在代码中通常启动数组的任何地方,请改用这些方法。

int[] Button = new int[2];

成为

getButtons();

你可能想使用一个ArrayList,如下所示:

ArrayList<JButton> buttons = new ArrayList<>();
//...
for (int n = 0; n < boardSize; n++) {
    JButton button = new JButton();
    buttons.add(button); // add to the end of the expandable list
    Center.add(button);
    button.addActionListener(this);
    button.setBackground(Color.white);
}

其中boardSize是指定大小的构造函数的参数。

最新更新