我和我的朋友当前正在编写一个记忆游戏,作为我们第一门编程课程的最终任务。我们想知道是否存在另一种以更好的方式以某种循环形式以更少的行数初始化Jbuttons及其属性的方法。总的来说,我们需要使此代码更高效,任何提示都值得赞赏:)

简易级别课程:

    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.JTextField;
    import javax.swing.JTextPane;
    import javax.swing.SwingConstants;

    import java.util.Timer;
    import java.util.TimerTask;

    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 Button0 = new JButton(Back);
    JButton Button1 = new JButton(Back);
    JButton Button2 = new JButton(Back);
    JButton Button3 = new JButton(Back);
    JButton Button4 = new JButton(Back);
    JButton Button5 = new JButton(Back);
    JButton Button6 = new JButton(Back);
    JButton Button7 = new JButton(Back);
    JButton Button8 = new JButton(Back);
    JButton Button9 = new JButton(Back);
    JButton Button10 = new JButton(Back);
    JButton Button11 = new JButton(Back);
    JButton Button12 = new JButton(Back);
    JButton Button13 = new JButton(Back);
    JButton Button14 = new JButton(Back);
    JButton Button15 = new JButton(Back);

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

    static int[] cardChecker = new int[2];
    static int[] card = new int[9];
    int[] StoreCards = new int[16];
    int[] Button = new int[2];

    static int flipped = 0;
    static int score = 0;
    static int seconds = 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);

        Button0.addActionListener(this);
        Button1.addActionListener(this);
        Button2.addActionListener(this);
        Button3.addActionListener(this);
        Button4.addActionListener(this);
        Button5.addActionListener(this);
        Button6.addActionListener(this);
        Button7.addActionListener(this);
        Button8.addActionListener(this);
        Button9.addActionListener(this);
        Button10.addActionListener(this);
        Button11.addActionListener(this);
        Button12.addActionListener(this);
        Button13.addActionListener(this);
        Button14.addActionListener(this);
        Button15.addActionListener(this);

        AnOtherLevel.setBackground(Color.white);
        AnOtherLevel.setForeground(Color.BLACK);

        Quit.setBackground(Color.white);
        Quit.setForeground(Color.BLACK);

        Button0.setBackground(Color.white);
        Button1.setBackground(Color.white);
        Button2.setBackground(Color.white);
        Button3.setBackground(Color.white);
        Button4.setBackground(Color.white);
        Button5.setBackground(Color.white);
        Button6.setBackground(Color.white);
        Button7.setBackground(Color.white);
        Button8.setBackground(Color.white);
        Button9.setBackground(Color.white);
        Button10.setBackground(Color.white);
        Button11.setBackground(Color.white);
        Button12.setBackground(Color.white);
        Button13.setBackground(Color.white);
        Button14.setBackground(Color.white);
        Button15.setBackground(Color.white);

        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.add(Button0);
        Center.add(Button1);
        Center.add(Button2);
        Center.add(Button3);
        Center.add(Button4);
        Center.add(Button5);
        Center.add(Button6);
        Center.add(Button7);
        Center.add(Button8);
        Center.add(Button9);
        Center.add(Button10);
        Center.add(Button11);
        Center.add(Button12);
        Center.add(Button13);
        Center.add(Button14);
        Center.add(Button15);

        Center.setBackground(Color.white);
        Right.setBackground(Color.white);

        Right.add(Score);
        Right.add(Timer);

        Bottom.add(AnOtherLevel);
        Bottom.add(Quit);
        Bottom.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();

        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;

            Button0.setIcon(Back);
            Button1.setIcon(Back);
            Button2.setIcon(Back);
            Button3.setIcon(Back);
            Button4.setIcon(Back);
            Button5.setIcon(Back);
            Button6.setIcon(Back);
            Button7.setIcon(Back);
            Button8.setIcon(Back);
            Button9.setIcon(Back);
            Button10.setIcon(Back);
            Button11.setIcon(Back);
            Button12.setIcon(Back);
            Button13.setIcon(Back);
            Button14.setIcon(Back);
            Button15.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() {
        if (Button[0] == 0 || Button[1] == 0) {
            Button0.setVisible(false);
        }
        if (Button[0] == 1 || Button[1] == 1) {
            Button1.setVisible(false);
        }
        if (Button[0] == 2 || Button[1] == 2) {
            Button2.setVisible(false);
        }
        if (Button[0] == 3 || Button[1] == 3) {
            Button3.setVisible(false);
        }
        if (Button[0] == 4 || Button[1] == 4) {
            Button4.setVisible(false);
        }
        if (Button[0] == 5 || Button[1] == 5) {
            Button5.setVisible(false);
        }
        if (Button[0] == 6 || Button[1] == 6) {
            Button6.setVisible(false);
        }
        if (Button[0] == 7 || Button[1] == 7) {
            Button7.setVisible(false);
        }
        if (Button[0] == 8 || Button[1] == 8) {
            Button8.setVisible(false);
        }
        if (Button[0] == 9 || Button[1] == 9) {
            Button9.setVisible(false);
        }
        if (Button[0] == 10 || Button[1] == 10) {
            Button10.setVisible(false);
        }
        if (Button[0] == 11 || Button[1] == 11) {
            Button11.setVisible(false);
        }
        if (Button[0] == 12 || Button[1] == 12) {
            Button12.setVisible(false);
        }
        if (Button[0] == 13 || Button[1] == 13) {
            Button13.setVisible(false);
        }
        if (Button[0] == 14 || Button[1] == 14) {
            Button14.setVisible(false);
        }
        if (Button[0] == 15 || Button[1] == 15) {
            Button15.setVisible(false);
        }
    }


    public void actionPerformed(ActionEvent event) {

        if (event.getSource() == AnOtherLevel) {
            setVisible(false);
            new AnOtherWindow();

        }

        if (event.getSource() == Quit) {
            System.exit(0);
        }

        if (event.getSource() == Button0) {

            int number = StoreCards[0];
            Button0.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 0);

            flipped++;
            Reset();

        }
        if (event.getSource() == Button1) {

            int number = StoreCards[1];
            Button1.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 1);

            flipped++;
            Reset();

        }
        if (event.getSource() == Button2) {
            int number = StoreCards[2];
            Button2.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 2);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button3) {
            int number = StoreCards[3];
            Button3.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 3);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button4) {
            int number = StoreCards[4];
            Button4.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 4);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button5) {
            int number = StoreCards[5];
            Button5.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 5);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button6) {
            int number = StoreCards[6];
            Button6.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 6);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button7) {
            int number = StoreCards[7];
            Button7.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 7);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button8) {
            int number = StoreCards[8];
            Button8.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 8);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button9) {
            int number = StoreCards[9];
            Button9.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 9);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button10) {
            int number = StoreCards[10];
            Button10.setIcon(new ImageIcon(number + imageType));
            Card1and2(number, 10);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button11) {
            int number = StoreCards[11];
            Button11.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 11);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button12) {
            int number = StoreCards[12];
            Button12.setIcon(new ImageIcon(number + ".png"));

            Card1and2(number, 12);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button13) {
            int number = StoreCards[13];
            Button13.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 13);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button14) {
            int number = StoreCards[14];
            Button14.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 14);

            flipped++;
            Reset();
        }
        if (event.getSource() == Button15) {
            int number = StoreCards[15];
            Button15.setIcon(new ImageIcon(number + imageType));

            Card1and2(number, 15);

            flipped++;
            Reset();
        }
        if (event.getSource() == Celebrities) {
            imageType = "c.png";
            Back = new ImageIcon("ceback.png");
        }
        if (event.getSource() == Cities) {
            imageType = ".jpg";
            Back = new ImageIcon("ciback.png");
        }

        if (event.getSource() == Memes) {
            imageType = ".png";
            Back = new ImageIcon("mback.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();

    }
}

最佳答案

正如评论中已经提到的,您应该将按钮放入数组中:

JButton[] buttons = new JButton[15];


或清单:

List<JButton> buttons = new ArrayList<JButton>();


然后,您可以使用简单的for循环来创建按钮并设置其属性:

for(int i = 0; i < 15; i++) {
    buttons[i] = new JButton();
    buttons[i].setIcon(...);
    // ...
}


在您提到的注释中,您在循环中收到错误。这是由于您实际上并未创建JButton对象。如上所示定义数组或列表时,只需创建一些包含15个null对象的内存空间。我认为在查看List定义时,这一点会变得更加清晰。在这些对象上,您自然不能调用任何方法。您仍然必须显式地创建那15个JButtons作为for循环的第一步。

关于java - 如何用更少的代码行初始化和使用许多类似的Jbutton?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37532852/

10-14 10:56