我们正在研究一个项目,当按下某个按钮并满足某些条件时,该项目会在JOptionPane中显示一条消息。但是,无论何时激活代码并按下按钮,JOptionPane都会显示没有消息。以下是创建GUI的代码
package BlackJack;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class BlckJckUI {
public static void main(String[] args) {
JFrame GUI = new JFrame("Blackjack Advisor");
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setSize(1000,900);
GUI.setVisible(true);
JButton two = new JButton(Two);
two.setSize(300, 100);
two.setLocation(100, 200);
two.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
Arrays array = new Arrays();
Math math = new Math();
math.cardvalue = 2;
array.clicktracker++;
JOptionPane.showMessageDialog(null,array.result);
}
});
GUI.add(two);
这是解决逻辑问题的代码。
package BlackJack;
public class Math {
public int cardvalue;
public Math()
{
Arrays array = new Arrays();
if (array.clicktracker == 1)
{
array.dealer = cardvalue;
array.result = "Please select the first card you have :)";
}
else if (array.clicktracker == 2)
{
array.playerhand.add(cardvalue);
array.result = "Please select the second card you have :)";
}
else if (array.clicktracker >= 3)
{
array.playerhand.add(cardvalue);
if (array.playerhandtotal <= 8)
{
// array.result = result statement
array.result = "You should just hit until you're safe. If the dealer 6 or below,\n"
+ " the chances are that he'll bust and if not, remain low above 17.\n"
+ " As long as you can pull a 17 or higher, you should be safe. Pick \n"
+ "another card or reset.";
这是创建数组及与之关联的变量的代码。
package BlackJack;
import java.util.ArrayList;
public class Arrays{
public String result = null;
ArrayList<Integer> playerhand = new ArrayList<Integer>();
public int dealer = 0;
public int clicktracker = 0;
public int playerhandtotal = 0;
{
for (int element: playerhand)
{
playerhandtotal = element + playerhandtotal;
}
System.out.println(result);
System.out.println(dealer);
System.out.println(clicktracker);
}
}
答案 0 :(得分:2)
在Math
构造函数中,您要更改的array.result
类型的结果与您尝试显示的结果不同。
我会考虑将Arrays
实例传递给Math
构造函数,以便您可以从那里修改结果。确保不要重新分配实例。
public void actionPerformed(ActionEvent e)
{
Arrays array = new Arrays();
Math math = new Math(array);
math.cardvalue = 2;
array.clicktracker++;
JOptionPane.showMessageDialog(null,array.result);
}
...
public Math(Arrays array)
{
if (array.clicktracker == 1)
{
// And so on ...
答案 1 :(得分:0)
问题是您要创建Arrays
类的两个单独实例。进入actionPerformed
方法,也位于Math
类的构造函数中。
您目前拥有此代码:
Arrays array = new Arrays();
Math math = new Math();
math.cardvalue = 2;
array.clicktracker++;
JOptionPane.showMessageDialog(null,array.result);
将显示您在Arrays
方法中创建的actionPerformed
对象的结果 - 该对象为null,因为此对象的结果初始化为null且从未设置。
在其他答案和评论中已经提到了这一点,并且会通过生成null来解决这个问题,但是这种方法现在总会产生相同的结果,因为您仍然总是在{Arrays
类中创建新的actionPerformed
类实例。 1}}方法。
更好的方法是将结果的逻辑与Math
类的构造函数分离为另一种方法,并在Math
方法之外创建actionPerformed
类的此实例。然后在actionPerformed
方法中调用您的方法,该方法将为您的结果提供逻辑。
在用户界面中:
Math math = new Math();
two.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
math.cardvalue = 2;
math.array.clicktracker++;
math.calcResult();
JOptionPane.showMessageDialog(null,math.array.result);
}
});
在数学中:
public class Math {
public int cardvalue;
public Arrays array;
public Math()
{
array = new Arrays();
}
public void calcResult(){
if (array.clicktracker == 1)
{
//...rest of your logic
}
}