Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中的if语句中使用Jbutton_Java_Swing_If Statement_Jbutton_Actionlistener - Fatal编程技术网

如何在java中的if语句中使用Jbutton

如何在java中的if语句中使用Jbutton,java,swing,if-statement,jbutton,actionlistener,Java,Swing,If Statement,Jbutton,Actionlistener,我正在尝试使用Jbutton操作侦听器clicks for将按下的变量从0设置为1、2或3。如果此号码已切换,则文本字段上的卡片套件将被切换。我的问题不是将按下的变量设置为新的数字,而是将其保持为0 我的主文件 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates *

我正在尝试使用Jbutton操作侦听器clicks for将按下的变量从0设置为1、2或3。如果此号码已切换,则文本字段上的卡片套件将被切换。我的问题不是将按下的变量设置为新的数字,而是将其保持为0

我的主文件

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cardapp;

/**
 *
 * @author Angela
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CardApp extends JFrame implements ActionListener {

    private JButton oneButton, 
                    twoButton, 
                    threeButton;  
    private int width = 25;
    private int height = 15;
    private JTextField TextField = new JTextField(3); 
    Hand handObject = new Hand();
    private int pressedNumber;


    public CardApp() {

        JPanel buttonPanel = new JPanel(new GridLayout(1, 3));        



        oneButton = new JButton("1");
        twoButton = new JButton("2");
        threeButton = new JButton("3");


        // Listen for events on each button
        oneButton.addActionListener(this);
        twoButton.addActionListener(this);
        threeButton.addActionListener(this);

        // Add each to the panel of buttons
        buttonPanel.add(oneButton); 
        buttonPanel.add(twoButton); 
        buttonPanel.add(threeButton); 
       // Add everything to a main panel attached to the content pane
        JPanel mainPanel = new JPanel(new BorderLayout());
        getContentPane().add(mainPanel);
        mainPanel.add(TextField, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        setTitle("Sabacc Example by Angela Rucci");
        setSize(375, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        handObject.Discards(pressedNumber);
        TextField.setText(handObject.ListOfCards());

    }


    public void actionPerformed(ActionEvent e) {

                if (e.getSource() == oneButton){
                        pressedNumber = 1;
                        handObject.Discards(pressedNumber);
                        }
                if (e.getSource() == twoButton){
                        pressedNumber = 2;
                        handObject.Discards(pressedNumber);
                        }
                if (e.getSource() == threeButton){
                        pressedNumber = 3;
                        handObject.Discards(pressedNumber);
                        }

                TextField.setText(handObject.ListOfCards());



    }


public static void main(String[] args) {
    CardApp c = new CardApp();
}

}
我的文件,它将生成一个新的西装按钮按下

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cardapp;
import java.util.Random;
//import javax.swing.JButton;
import javax.swing.JOptionPane;
//import javax.swing.*;
//import java.awt.*;
//import java.awt.event.*;

/**
 *
 * @author Angela
 */

public class Hand {
String [] Suits = {"C", "H", "S", "D"};
String [] probability = {"C","H","R","S","D"};
Random randomInt = new Random ();
String RandomSuit;
String RandomShuffle;
String ThreeSuits; 
String LeftSuit;
String MiddleSuit;
String RightSuit;
int pressed;


       public int Discards(int pressedNumber){
                            Randomizer();
              return pressed;

             }



       public void Randomizer (){

           int RandomSuitNumber = randomInt.nextInt(4);//this is generator a random number

           //------------------Decide what hand to randomize --------------------------//
           if (pressed==1){
                  LeftSuit= Suits[RandomSuitNumber];

                  }

              if (pressed==2){
                 MiddleSuit=Suits[RandomSuitNumber];

                 }

              if (pressed==3){
                  RightSuit=Suits[RandomSuitNumber];}

          //----------------20% chance of new random set------------------------------------//
            int ProabilityRandomNum = randomInt.nextInt(5);
            RandomShuffle= probability[ProabilityRandomNum];


          //------------If proability array equals R then change all of the suits----------//  
            if (RandomShuffle.equals("R") || pressed== 0){

                JOptionPane.showMessageDialog(null, "Randomized Hand!");

                int leftNumber = randomInt.nextInt(4);
                int middleNumber = randomInt.nextInt(4);
                int rightNumber = randomInt.nextInt(4);

                LeftSuit= Suits[leftNumber];
                MiddleSuit= Suits[middleNumber];
                RightSuit= Suits[rightNumber];}

            ThreeSuits = (LeftSuit + MiddleSuit + RightSuit); 
       }


       public String ListOfCards (){
               return ThreeSuits;
             }



       public void GameOver(){
              if (LeftSuit == MiddleSuit && MiddleSuit == RightSuit && RightSuit== LeftSuit){
                  JOptionPane.showMessageDialog(null, "WINNER!!");
                 }
             }
}
您将pressedNumber的值从CardApp传递到Hand,但从未实际使用它

public int Discards(int pressedNumber) {
    Randomizer();
    return pressed;
}
你是不是想做一些像

public int Discards(int pressedNumber) {
    pressed = pressedNumber;
    Randomizer();
    return pressed;
}

现在,不可否认,我几乎没有睡眠,所以我很容易困惑,但我真的不知道你的问题是什么。您是否询问pressedNumber的值是否大于单击的按钮的值?它不应更改?你说要将其保持为0,但如果你只是想将其保持为0,为什么还要更改该值?不,我不想将其保持为0,我希望在单击其中一个按钮时更改该值。你将pressedNumber传递给HandDiscards,但从未实际使用它?你从未设置Hand.pressed;你想把作业放在丢弃方法中吗?是的,但我正在尝试使用它,尽管欢迎来到我的俱乐部:P@AngelaRucci这是一个很大的俱乐部。