setText()不更新标签

时间:2013-11-02 21:13:12

标签: java swing user-interface jlabel

我在互联网上试图解决我的问题。 whoWon.setText() 部分未更新 JLabel

应该更新 JLabel 的部分位于评论<下的 PlayAgain.java 文件中/ p>

//获得获胜者

我尝试了 重绘() postInvalidate() ,但仍无效。必须有我遗漏的东西。

以下是代码:

TicTacToe.java =&gt;

package ticTacToe;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TicTacToe extends JFrame implements ActionListener {

    private JPanel contentPane;
    boolean winner = false;
    boolean draw = false;
    byte count = 0;
    String btnLabel;


    PlayAgain playAgain = new PlayAgain();

    JButton btn1 = new JButton("");
    JButton btn2 = new JButton("");
    JButton btn3 = new JButton("");
    JButton btn4 = new JButton("");
    JButton btn5 = new JButton("");
    JButton btn6 = new JButton("");
    JButton btn7 = new JButton("");
    JButton btn8 = new JButton("");
    JButton btn9 = new JButton("");

    static TicTacToe frame = new TicTacToe();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TicTacToe() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 616, 637);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btn1.setBounds(0, 0, 200, 200);
        btn1.addActionListener(this);
        contentPane.add(btn1);


        btn2.setBounds(200, 0, 200, 200);
        btn2.addActionListener(this);
        contentPane.add(btn2);


        btn3.setBounds(400, 0, 200, 200);
        btn3.addActionListener(this);
        contentPane.add(btn3);


        btn4.setBounds(0, 200, 200, 200);
        btn4.addActionListener(this);
        contentPane.add(btn4);


        btn5.setBounds(200, 200, 200, 200);
        btn5.addActionListener(this);
        contentPane.add(btn5);


        btn6.setBounds(400, 200, 200, 200);
        btn6.addActionListener(this);
        contentPane.add(btn6);


        btn7.setBounds(0, 400, 200, 200);
        btn7.addActionListener(this);
        contentPane.add(btn7);


        btn8.setBounds(200, 400, 200, 200);
        btn8.addActionListener(this);
        contentPane.add(btn8);


        btn9.setBounds(400, 400, 200, 200);
        btn9.addActionListener(this);
        contentPane.add(btn9);
    }


//Main Code
    @Override
    public void actionPerformed(ActionEvent e) {


        //Play label (Player 1 = X, Player 2 = O)
        if(count==0 || count==2 || count==4 || count==6 || count==8){
            btnLabel = "X";
        }else btnLabel = "O";

        count++;

        //Clicking buttons

        if(e.getSource()==btn1){
            btn1.setText(btnLabel);
            btn1.setEnabled(false);
            btn1.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn2){
            btn2.setText(btnLabel);
            btn2.setEnabled(false);
            btn2.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn3){
            btn3.setText(btnLabel);
            btn3.setEnabled(false);
            btn3.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn4){
            btn4.setText(btnLabel);
            btn4.setEnabled(false);
            btn4.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn5){
            btn5.setText(btnLabel);
            btn5.setEnabled(false);
            btn5.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn6){
            btn6.setText(btnLabel);
            btn6.setEnabled(false);
            btn6.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn7){
            btn7.setText(btnLabel);
            btn7.setEnabled(false);
            btn7.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn8){
            btn8.setText(btnLabel);
            btn8.setEnabled(false);
            btn8.setBackground(Color.BLACK);
        }
        else if(e.getSource()==btn9){
            btn9.setText(btnLabel);
            btn9.setEnabled(false);
            btn9.setBackground(Color.BLACK);
        }


        //Win Conditions

        //Horizontal
                if(btn1.getText().equals(btn2.getText())&&btn2.getText().equals(btn3.getText())&!btn1.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    else if(btn4.getText().equals(btn5.getText())&&btn5.getText().equals(btn6.getText())&!btn4.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    else if(btn7.getText().equals(btn8.getText())&&btn8.getText().equals(btn9.getText())&!btn7.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    //Vertical
    else if(btn1.getText().equals(btn4.getText())&&btn4.getText().equals(btn7.getText())&!btn1.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    else if(btn2.getText().equals(btn5.getText())&&btn5.getText().equals(btn8.getText())&!btn2.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    else if(btn3.getText().equals(btn6.getText())&&btn6.getText().equals(btn9.getText())&!btn3.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    //Diagonal
    else if(btn1.getText().equals(btn5.getText())&&btn5.getText().equals(btn9.getText())&!btn1.getText().equals("")){ 
        winner = true; 
        afterGame(btnLabel);
    }

    else if(btn3.getText().equals(btn5.getText())&&btn5.getText().equals(btn7.getText())&!btn3.getText().equals("")) {
        winner = true;
        afterGame(btnLabel);
    }

        //Draw
        else if(count==9&winner==false)System.out.println("Game is draw");



    }

    public void afterGame(String btnName){
        playAgain.getWinner(btnName);
        playAgain = new PlayAgain();
        playAgain.setVisible(true);

        frame.dispose();


    }

}

PlayAgain.java =&gt;

package ticTacToe;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;

public class PlayAgain extends JFrame {

    private JPanel contentPane;
    JLabel whoWon = new JLabel();
    static PlayAgain frame = new PlayAgain();
    String winner;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public PlayAgain() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 153);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        whoWon.setText(" ");
        whoWon.setFont(new Font("Tahoma", Font.BOLD, 18));
        whoWon.setBounds(10, 11, 414, 48);
        contentPane.add(whoWon);

        JButton playAgain = new JButton("Play Again!");
        playAgain.setBounds(10, 70, 200, 34);
        contentPane.add(playAgain);

        JButton exit = new JButton("Quit!");
        exit.setBounds(220, 70, 204, 34);
        contentPane.add(exit);
    }

    //Get Winner
    public void getWinner(String player){       
        System.out.println("getWinner()");
        System.out.println("btnName: " + player);

        if(player.equals("X")){
            System.out.println("Player 1 won");
            whoWon.setText("Player 1 has won!");
            repaint();
            }
        else {
            System.out.println("Player 2 won");
            whoWon.setText("Player 2 has won!");
            repaint();
        }
    }


}

编辑: System.out.println() 可以帮助我按照程序的步骤进行操作

3 个答案:

答案 0 :(得分:0)

您不能使用==检查字符串相等性。使用equals

if (player.equals("X"))

btn1.getText()==btn2.getText()也是错误的平等检查......

答案 1 :(得分:0)

在标签更新之前在某处做这件事,看看是否有帮助。如果标签没有更新,可能是因为它不可编辑/启用。

whoWon.setEditable(true);

答案 2 :(得分:0)

我认为您的问题出在方法 afterGame 之后。您为一个实例调用 getWinner 方法,然后在下一行中创建一个空值的新方法,因为您的标签不会更改文本。您会看到一个新的对象实例。