java在事件发生之前单击2个按钮

时间:2018-03-04 01:15:32

标签: java

我是Java的新手,我想创建一个小骰子游戏。对于游戏,你掷两个骰子,然后根据两个骰子的加法或减法的值单击1-9的按钮。这可以很好地工作除了,当我滚动6& 6我希望能够点击0(对于减法 - 哪个有效)或1& 2在一起(增加12)。我只能为单值按钮点击而不是1& 2.关于如何点击1& 2如果我滚动6和6? (以下代码仅适用于按钮0,1,2)感谢您提供任何帮助。

package dice;
import java.awt.*;
import java.awt.event.*;
import static java.lang.Math.abs;
import javax.swing.JOptionPane;

public class Dice extends Frame implements WindowListener,ActionListener {
    TextField text = new TextField(20);
    TextField dice1 = new TextField(10);
    TextField dice2 = new TextField(10);
    TextField plusvalue = new TextField(10);
    TextField minusvalue = new TextField(10);
    Button b0;
    Button b1;
    Button b2;
    Button nomatch;
    Button rolldice;
private int numClicks = 0;
int dice1value;
int dice2value;
int SIDES = 6;
int plus;
int minus;
public void roll() {
    //roll dice
    dice1value = (int) (Math.random() * SIDES) + 1;
    dice2value = (int) (Math.random() * SIDES) + 1;
    plus = dice1value + dice2value;
    minus = abs(dice1value - dice2value);
    dice1.setText("Dice 1 = "+ Integer.toString(dice1value));      
    dice2.setText("Dice 2 = "+Integer.toString(dice2value)); 
    plusvalue.setText("Addition = "+Integer.toString(plus));      
    minusvalue.setText("Subtract = "+Integer.toString(minus));     
   }   
    public static void main(String[] args) {
            Dice myWindow = new Dice("My Dice Game");
            myWindow.setSize(400,400);
            myWindow.setVisible(true);

    }

    public Dice(String title) {

            super(title);
            setLayout(new FlowLayout());
            addWindowListener(this);
            rolldice = new Button("Roll Dice");
            b0 = new Button("0");
            b1 = new Button("1");
            b2 = new Button("2");
            nomatch = new Button("No Match");
            add(b0);
            add(b1);
            add(b2);
            add(rolldice);
            add(text);
            add(dice1);
            add(dice2);
            add(plusvalue);
            add(minusvalue);
            add(nomatch);
            b0.addActionListener(this);
            b1.addActionListener(this);
            b2.addActionListener(this);
            nomatch.addActionListener(this);
            rolldice.addActionListener(this);
    }

   public void actionPerformed(ActionEvent e) {

 if (e.getSource()==b0)
     {
         if(plus==0||minus==0)
         {  numClicks++;
            text.setText("Button Clicked " + numClicks + " times");
            b0.setEnabled(false);
     } else { JOptionPane.showMessageDialog(null, "You can't do that", "Error",
              JOptionPane.ERROR_MESSAGE);}}
 else if (e.getSource()==b1)
     {  if(plus==1||minus==1)
         {  numClicks++;
            text.setText("Button Clicked " + numClicks + " times");
            b1.setEnabled(false);
     } else { JOptionPane.showMessageDialog(null, "You can't do that", "Error",
              JOptionPane.ERROR_MESSAGE);}}
 else if (e.getSource()==b2)
     {
         if(plus==2||minus==2)
         {  numClicks++;
            text.setText("Button Clicked " + numClicks + " times");
            b2.setEnabled(false);
     } else { JOptionPane.showMessageDialog(null, "You can't do that", "Error",
              JOptionPane.ERROR_MESSAGE);}}
 else if (e.getSource()==nomatch)
     {
            numClicks++;
            text.setText("Button Clicked " + numClicks + " times");
                     }
 else if (e.getSource()==rolldice)
     {roll();


     } 

   }

    public void windowClosing(WindowEvent e) {
            dispose();
            System.exit(0);
    }

    public void windowOpened(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
} 

1 个答案:

答案 0 :(得分:0)

最简单的方法是

    if (e.getSource() == b1) {
    if (plus == 1 || minus == 1 || plus == 12) {
        numClicks++;
        text.setText("Button Clicked " + numClicks + " times");
        b1.setEnabled(false);
    } else {
        JOptionPane.showMessageDialog(null, "You can't do that", "Error",
        JOptionPane.ERROR_MESSAGE);
    }
}  if (e.getSource() == b2) {
    if (plus == 2 || minus == 2 || plus == 12) {
        numClicks++;
        text.setText("Button Clicked " + numClicks + " times");
        b2.setEnabled(false);
    } else {
        JOptionPane.showMessageDialog(null, "You can't do that", "Error",
        JOptionPane.ERROR_MESSAGE);
    }
}

否则你需要引入第三个变量来存储2,你需要在if条件中检查变量的值。