所以我做了tic-tac-toe GUI,我发现当鼠标点击(检查paint()
事件代码)时,mouseClicked()
方法被调用两次。我不知道 - 这是怎么回事,我应该怎么做才能避免这个问题。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class tic_tac_toe_game extends JFrame implements MouseListener{
JPanel contentPane;
JLabel box_1_1,box_1_2,box_1_3,box_2_1,box_2_2,box_2_3,box_3_1,box_3_2,box_3_3; //x, y coordinate
static int turn = 1; // 1 is p1 turn 2 is p2 turn
static boolean win_state = false;
static boolean break_up = false;
static boolean cant_turn[][] = new boolean[3][3];
public tic_tac_toe_game() {
contentPane = new JPanel();
contentPane.setLayout(null);
box_1_1 = new JLabel("T");
box_1_2= new JLabel("T");
box_1_3 = new JLabel("T");
box_2_1= new JLabel("T");
box_2_2 = new JLabel("T");
box_2_3= new JLabel("T");
box_3_1 = new JLabel("T");
box_3_2= new JLabel("T");
box_3_3 = new JLabel("T");
//modify it in paint()
//contentPane *NO* layout mnager
setContentPane(contentPane);
setSize(600,600);
setVisible(true);
setTitle("Tic tac toe game");
setLayout(null);
}
public void paint(Graphics g)
{
g.drawLine(0, getHeight()/3*1, getWidth(), getHeight()/3); // 1, 2 lines haha (hor)
g.drawLine(0, getHeight()/3*2, getWidth(), getHeight()/3*2); // (vertical)
g.drawLine(getWidth()/3*1, 0, getWidth()/3*1, getHeight()); // 1, 2 lines haha (hor)
g.drawLine(getWidth()/3*2, 0, getWidth()/3*2, getHeight()); //(vertical);
setup_UI();
//box_1_1.set
}
public void setup_UI()
{
box_1_1.setFont(new Font("Serif",Font.BOLD,50));
box_1_1.setBounds(getWidth()/3/3*1, getHeight()/3/3*1, 40, 40);
box_1_1.addMouseListener(this);
contentPane.add(box_1_1);
box_1_2.setFont(new Font("Serif",Font.BOLD,50));
box_1_2.setBounds(getWidth()/3/3*1, getHeight()/3/3*4, 40, 40);
box_1_2.addMouseListener(this);
contentPane.add(box_1_2);
box_1_3.setFont(new Font("Serif",Font.BOLD,50));
box_1_3.setBounds(getWidth()/3/3*1, getHeight()/3/3*7, 40, 40);
box_1_3.addMouseListener(this);
contentPane.add(box_1_3);
box_2_1.setFont(new Font("Serif",Font.BOLD,50));
box_2_1.setBounds(getWidth()/3/3*4, getHeight()/3/3*1, 40, 40);
box_2_1.addMouseListener(this);
contentPane.add(box_2_1);
box_2_2.setFont(new Font("Serif",Font.BOLD,50));
box_2_2.setBounds(getWidth()/3/3*4, getHeight()/3/3*4, 40, 40);
box_2_2.addMouseListener(this);
contentPane.add(box_2_2);
box_2_3.setFont(new Font("Serif",Font.BOLD,50));
box_2_3.setBounds(getWidth()/3/3*4, getHeight()/3/3*7, 40, 40);
box_2_3.addMouseListener(this);
contentPane.add(box_2_3);
box_3_1.setFont(new Font("Serif",Font.BOLD,50));
box_3_1.setBounds(getWidth()/3/3*7, getHeight()/3/3*1, 40, 40);
box_3_1.addMouseListener(this);
contentPane.add(box_3_1);
box_3_2.setFont(new Font("Serif",Font.BOLD,50));
box_3_2.setBounds(getWidth()/3/3*7, getHeight()/3/3*4, 40, 40);
box_3_2.addMouseListener(this);
contentPane.add(box_3_2);
box_3_3.setFont(new Font("Serif",Font.BOLD,50));
box_3_3.setBounds(getWidth()/3/3*7, getHeight()/3/3*7, 40, 40);
box_3_3.addMouseListener(this);
contentPane.add(box_3_3);
}
public static void main(String[] args)
{
tic_tac_toe_game tic = new tic_tac_toe_game();
/*while(tic.win_state== false)
{
tic.whos_turn();
//tic.rule(); // de termine if player win or not
}*/
}
public void rule()
{
// if(box_1_1.equals('O') == true)
// System.out.append("Yes");
}
public void whos_turn()
{
if(turn == 1 && break_up == false)
{
JOptionPane.showMessageDialog(null, "P1's turn ! ");
break_up = true;
}
else if(turn == 2 && break_up == false)
{
JOptionPane.showMessageDialog(null, "P2's turn ! ");
break_up = true;
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.print("I'm clicked"); //this one called twice
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
您的paint
方法只应绘制,而不是执行任何额外操作,如调用setup_UI
。改为在构造函数中调用setup_UI
。
请注意,您的程序不是线程安全的,您不应该使用初始线程来创建和操作swing组件。阅读https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html