我不知道为什么我的JOptionPane没有在这里显示

时间:2014-10-28 03:01:55

标签: user-interface jbutton actionlistener joptionpane

出于某种原因,当我点击forfeit按钮时,JOptionPane没有显示在ActionListner中,这里是我的类,没有涉及其他类。我尝试在其他地方使用optionPane,比如构造函数,它可以工作,但如果单击“forfeit”按钮,我需要显示它。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

    public class GUI extends JFrame implements ActionListener
    {
        JButton tttSquare;
        JButton forfeit;

        JLabel whosTurn;
        JLabel turnImage;

        JOptionPane optionPane = new JOptionPane();

        boolean turn = true;
        String redOrBlack = "draw";

        public GUI()
        {
            Container contentPane = getContentPane();
            setSize(750, 450);
            setTitle("TicTacToe");
            setLocationRelativeTo(null);
            contentPane.setLayout(null);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            contentPane.setBackground(Color.lightGray);

            int xCord = 0;
            int yCord = 0;
            // build tic tac toe board
            for(int rowCount = 0; rowCount < 3; rowCount++)
            {
                xCord = 0;
                for(int squareCount = 0; squareCount < 3; squareCount++)
                {
                    tttSquare = new JButton();
                    tttSquare.setBackground(Color.white);
                    tttSquare.addActionListener(this);
                    contentPane.add(tttSquare);
                    tttSquare.setBounds(xCord + 60, yCord + 55, 100, 100);
                    xCord += 105;
                }
                yCord += 105;
            }
            whosTurn = new JLabel("X's TURN");
            contentPane.add(whosTurn);
            whosTurn.setLocation(525, 225);
            whosTurn.setSize(100, 200);

            forfeit = new JButton();
            contentPane.add(forfeit);
            forfeit.setText("Forfeit?");
            forfeit.setLocation(500 ,165);
            forfeit.setSize(100, 100);
            forfeit.setBackground(Color.red);       // edit in action listener                                                                           
        }

        //main method
        public static void main(String args[])
        {
            GUI g = new GUI();
        } 

        @Override
        public void actionPerformed(ActionEvent e) 
        {
            JButton clicked = (JButton) e.getSource();
            if(clicked == forfeit)
            {   
                optionPane.showMessageDialog("Winner!");
            }
            else if(turn)
            {
                clicked.setBackground(Color.red);
                forfeit.setBackground(Color.black);
                redOrBlack = "black";
                turn = false;
            }
            else
            {
                clicked.setBackground(Color.black);
                forfeit.setBackground(Color.red);
                redOrBlack = "red";
                turn = true;
            }  
        }

    }

1 个答案:

答案 0 :(得分:0)

找到我的解决方案!在回顾了我使用JButtons的其他项目之后,我发现我忘了使用JButton.addActionListener(this);

希望这仍然是有问题的人的参考!