繁体   English   中英

JOptionPane消息显示为空

[英]JOptionPane message showing null

我们正在一个项目上尝试在按下按钮并满足某些条件时在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);
     }
}

在您的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 ...

问题是您正在创建Arrays类的两个单独的实例。 actionPerformed可以在actionPerformed方法中使用,也可以在Math类的构造函数中使用。

您当前拥有的这段代码:

Arrays array = new Arrays();
Math math = new Math();
math.cardvalue = 2;
array.clicktracker++;

JOptionPane.showMessageDialog(null,array.result);

将显示您在actionPerformed方法中创建的Arrays对象的结果-由于此对象的结果初始化为null且从不设置,因此该结果为null。

在其他答案和评论中已经提到了这一点,并将通过产生null来解决它,但是这种方法现在将始终产生相同的结果,因为您仍然始终在actionPerformed方法中创建Arrays类的新实例。

更好的方法是将结果的逻辑从Math类的构造方法分离到另一个方法中,并在actionPerformed方法外部创建Math类的此实例。 然后在您的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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM