User interface 我不知道';我不知道为什么我的JOptionPane是';我不在这里展示
出于某种原因,当我单击“没收”按钮时,JOptionPane没有显示在ActionListner中,这是我的类,没有涉及其他类。我尝试在其他地方使用optionPane,比如构造器,它可以工作,但是如果单击“没收”按钮,我需要显示它User interface 我不知道';我不知道为什么我的JOptionPane是';我不在这里展示,user-interface,jbutton,actionlistener,joptionpane,User Interface,Jbutton,Actionlistener,Joptionpane,出于某种原因,当我单击“没收”按钮时,JOptionPane没有显示在ActionListner中,这是我的类,没有涉及其他类。我尝试在其他地方使用optionPane,比如构造器,它可以工作,但是如果单击“没收”按钮,我需要显示它 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class GUI extends JFrame implements A
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;
}
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.io.*;
公共类GUI扩展JFrame实现ActionListener
{
JButton tttSquare;
布顿没收;
JLabel-whosTurn;
JLabel-turnImage;
JOptionPane optionPane=新的JOptionPane();
布尔转向=真;
字符串redOrBlack=“draw”;
公共图形用户界面()
{
容器contentPane=getContentPane();
设置大小(750450);
setTitle(“Tictatcoe”);
setLocationRelativeTo(空);
contentPane.setLayout(null);
setVisible(真);
setDefaultCloseOperation(关闭时退出);
contentPane.setBackground(颜色:浅灰色);
int xCord=0;
int-yCord=0;
//构建tic-tac趾板
对于(int rowCount=0;rowCount<3;rowCount++)
{
xCord=0;
对于(int squareCount=0;squareCount<3;squareCount++)
{
tttSquare=newjbutton();
TTT方形立根地面(颜色:白色);
tttSquare.addActionListener(此);
contentPane.add(tttSquare);
TTT方形立根(xCord+60,yCord+55100100);
xCord+=105;
}
yCord+=105;
}
whosurn=新的JLabel(“X的回合”);
contentPane.add(whosurn);
whosTurn设置位置(525225);
whosTurn.setSize(100200);
没收=新的JButton();
内容窗格。添加(没收);
没收。setText(“没收?”);
没收。设置位置(500165);
没收。设置大小(100100);
feund.setBackground(Color.red);//编辑正在运行的侦听器
}
//主要方法
公共静态void main(字符串参数[])
{
GUI g=新GUI();
}
@凌驾
已执行的公共无效操作(操作事件e)
{
JButton clicked=(JButton)e.getSource();
如果(单击==没收)
{
optionPane.showMessageDialog(“获胜者!”);
}
否则,如果(转身)
{
单击。挫折背景(颜色。红色);
放弃。挫折背景(颜色:黑色);
redOrBlack=“黑色”;
转向=假;
}
其他的
{
单击。挫折背景(颜色。黑色);
放弃。挫折背景(颜色。红色);
redOrBlack=“红色”;
转身=真;
}
}
}
找到了我的解决方案!在回顾了我使用JButton的其他项目之后,我发现我忘记了使用JButton.addActionListener(这个)
希望这仍然可以为有问题的人提供参考 注意:我尝试将JOptionPane调用更改为System.exit(0),但这也不起作用,可能是按钮本身不起作用。