java编程swing应用程序

时间:2015-04-11 18:33:24

标签: java swing

    import java.awt.BorderLayout;
            import java.applet.Applet;
            import java.awt.GridLayout;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;
            import java.util.ArrayList;
            import javax.swing.*;
            import java.awt.FlowLayout;
            import java.awt.event.*;
            import java.awt.*;

            public class MemoryGame implements ActionListener 
            {
                Label mostra;           
                public int delay = 1000; //1000 milliseconds
                public void init() 
                {   
                    //add(mostra = new Label("     "+0));
                }

                public void Contador()
                {
                    ActionListener counter = new ActionListener() 
                    {
                        public void actionPerformed(ActionEvent e) 
                        {
                            tempo++;
                            TempoScore.setText("Tempo: " + tempo);
                        }
                    };
                    new Timer(delay, counter).start();
                }

                public void updateHitMiss() 
                {
                    HitScore.setText("Acertou: " + Hit);
                    MissScore.setText("Falhou: " + Miss);
                    PontosScore.setText("Pontos: " + Pontos);
                }

                private JFrame window = new JFrame("Jogo da Memoria");
                private static final int WINDOW_WIDTH = 500; // pixels
                private static final int WINDOW_HEIGHT = 500; // pixels
                private JButton exitBtn, baralharBtn, solveBtn, restartBtn;
                ImageIcon ButtonIcon = createImageIcon("card1.png");
                private JButton[] gameBtn = new JButton[16];
                private ArrayList<Integer> gameList = new ArrayList<Integer>();
                private int Hit, Miss, Pontos;
                public int tempo = 0;   
                private int counter = 0;
                private int[] btnID = new int[2];
                private int[] btnValue = new int[2];
                private JLabel HitScore, MissScore,TempoScore, PontosScore;
                private JPanel gamePnl = new JPanel();
                private JPanel buttonPnl = new JPanel();
                private JPanel scorePnl = new JPanel();

                protected static ImageIcon createImageIcon(String path) 
                {
                    java.net.URL imgURL = MemoryGame.class.getResource(path);
                    if (imgURL != null) 
                    {
                        return new ImageIcon(imgURL);
                    } 
                    else 
                    {
                        System.err.println("Couldn't find file: " + path);
                        return null;
                    }
                }

                public MemoryGame()
                {
                    createGUI();
                    createJPanels();
                    setArrayListText();
                    window.setTitle("Jogo da Memoria");
                    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
                    window.setVisible(true);
                    Contador();
                }

                public void createGUI()
                {
                    for (int i = 0; i < gameBtn.length; i++)
                    {
                        gameBtn[i] = new JButton(ButtonIcon);
                        gameBtn[i].addActionListener(this);
                    }
                    HitScore = new JLabel("Acertou: " + Hit);
                    MissScore = new JLabel("Falhou: " + Miss);
                    TempoScore = new JLabel("Tempo: " + tempo);
                    PontosScore = new JLabel("Pontos: " + Pontos);
                    exitBtn = new JButton("Sair");
                    exitBtn.addActionListener(this);
                    baralharBtn = new JButton("Baralhar");
                    baralharBtn.addActionListener(this);
                    solveBtn = new JButton("Resolver");
                    solveBtn.addActionListener(this);
                    restartBtn = new JButton("Recomecar");
                    restartBtn.addActionListener(this);
                }

                public void createJPanels()
                {
                    gamePnl.setLayout(new GridLayout(4, 4));
                    for (int i = 0; i < gameBtn.length; i++)
                    {
                        gamePnl.add(gameBtn[i]);
                    }
                    buttonPnl.add(baralharBtn);
                    buttonPnl.add(exitBtn);
                    buttonPnl.add(solveBtn);
                    buttonPnl.add(restartBtn);
                    buttonPnl.setLayout(new GridLayout(1, 0));
                    scorePnl.add(HitScore);
                    scorePnl.add(MissScore);
                    scorePnl.add(TempoScore);
                    scorePnl.add(PontosScore);
                    scorePnl.setLayout(new GridLayout(1, 0));
                    window.add(scorePnl, BorderLayout.NORTH);
                    window.add(gamePnl, BorderLayout.CENTER);
                    window.add(buttonPnl, BorderLayout.SOUTH);
                }

                public void setArrayListText()
                {
                    for (int i = 0; i < 2; i++)
                    {
                        for (int ii = 1; ii < (gameBtn.length / 2) + 1; ii++)
                        {
                            gameList.add(ii);
                        }
                    }
                }

                public boolean sameValues()
                {
                    if (btnValue[0] == btnValue[1])
                    {
                        return true;
                    }
                    return false;
                }

                public void actionPerformed(ActionEvent e)
                {
                        if (exitBtn == e.getSource())
                        {
                            System.exit(0);
                        }

                        if (baralharBtn == e.getSource())
                        {
                            //
                        }

                        if (solveBtn == e.getSource())
                        {
                            for (int i = 0; i < gameBtn.length; i++)
                            {
                                gameBtn[i].setText("" + gameList.get(i));
                                gameBtn[btnID[0]].setEnabled(false);
                                gameBtn[btnID[1]].setEnabled(false);
                            }
                        }

                        for (int i = 0; i < gameBtn.length; i++)
                        {
                            if (gameBtn[i] == e.getSource())
                            {
                                gameBtn[i].setText("" + gameList.get(i));
                                gameBtn[i].setEnabled(false);
                                counter++;
                                if (counter == 3)
                                {
                                    if (sameValues())
                                    {
                                        gameBtn[btnID[0]].setEnabled(false);
                                        gameBtn[btnID[1]].setEnabled(false);
                                        gameBtn[btnID[0]].setVisible(false);
                                        gameBtn[btnID[1]].setVisible(false);
                                        Hit = Hit +1;
                                        Pontos = Pontos + 25;
                                    }
                                    else
                                    {
                                        gameBtn[btnID[0]].setEnabled(true);
                                        gameBtn[btnID[0]].setText("");
                                        gameBtn[btnID[1]].setEnabled(true);
                                        gameBtn[btnID[1]].setText("");
                                        Miss = Miss +1;
                                        Pontos = Pontos - 5;                       
                                    }
                                    counter = 1; //permite 2(3) clikes de cada vez
                                }
                                /*if (Pontos <= 0)
                                {
                                    Pontos=0;
                                } */
                                if (counter == 1) // se carregar 1º botão
                                {
                                    btnID[0] = i;
                                    btnValue[0] = gameList.get(i);
                                }
                                if (counter == 2) // se carregar 2º botão
                                {
                                    btnID[1] = i;
                                    btnValue[1] = gameList.get(i);
                                }
                            }
                        }

                        if (restartBtn == e.getSource())
                        { 
                            Hit=0;
                            Miss=0;
                            tempo=-1;
                            Pontos=0;
                            for (int i = 0; i < gameBtn.length; i++)  /* what i want to implement(restart button) goes here, this is incomplete because it  only clean numbers and "transforms" into regular JButton, the last two clicks
on the 4x4 grid (btnID[0] and btnID[1]), but i want to clean all the visible
 numbers in the grid 4x4, and want to transform all grid 4x4 into default (or
  regular color, white and blue) JButton, the grid numbers stay in same order 
    or position. */
                            {

                                gameBtn[btnID[0]].setEnabled(true); 
                                gameBtn[btnID[0]].setText(""); 
                                gameBtn[btnID[1]].setEnabled(true); 
                                gameBtn[btnID[1]].setText("");  
                            }

                            /*
                            restartBtn.addActionListener(new ActionListener() 
                            { 
                                JFrame.dispatchEvent(new WindowEvent(JFrame, WindowEvent.WINDOW_CLOSING));  // this line is getting errors (MemoryGame.java:233: error: <identifier> expected
                                JFrame.dispatchEvent(new WindowEvent(JFrame, WindowEvent.WINDOW_CLOSING));
             illegal start of type,')' expected, ';' expected all in the same line )
                                private JFrame window = new JFrame("Jogo da Memoria");
                                // something not right, i think frame is replaced by JFrame but i dont know, be 
                            }); */
                        }  
                    updateHitMiss();
                }

                public static void main(String[] args)
                {      
                    new MemoryGame();
                }
            }

        </code>

您好, 我想添加一个重启按钮(鼠标点击),我正在运行notepadd ++,当我点击重启按钮(recomeçar)时,它应该重启这个记忆游戏,所有计数器设置为零等,清理网格4x4,所有数字都是当我点击重新启动按钮时它们全部消失(但它们在相同的位置或顺序)因此只显示常规的JButtons,游戏应该像第一次打开应用程序时的起点那样,所有数字都不可见。 (重启按钮的功能等于退出应用程序,然后重新启动)。

重新启动第215到239行中的按钮代码,这些行之间的注释会报告问题。

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解你的问题,但我认为这会有所帮助:

使用以下命令将ActionListener添加到按钮: button.addActionListener(new ActionListener() { });button是您的JButton的名称。

在您的ActionListener中,调用frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));frame是您的JFrame的名称。

然后,运行用于初始化游戏的所有方法(包括打开JFrame的方法)。

评论是否需要澄清或更多帮助。