我在Deal或No Deal Java游戏中需要帮助

时间:2014-04-04 14:20:55

标签: java swing user-interface

我正在努力做一个简单的交易或没有交易游戏。我还没有真正进步,但希望我能完成它。到目前为止,我只使用JButton创建了一个JFrame,当您单击按钮时,会出现ammount。我接下来要做的是当你点击至少3个按钮时,会出现一个对话框。我不知道这是否可能,但如果是的话,请帮助我。我只对Gui和事件处理有一些经验。

到目前为止,这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class Dealornodeal2 implements ActionListener{

private JFrame frame = new JFrame("DEAL OR NO DEAL");
private JButton button1 = new JButton(" 1 ");
private JButton button2 = new JButton(" 2 ");
private JButton button3 = new JButton(" 3 ");
private JButton button4 = new JButton(" 4 ");
private JButton button5 = new JButton(" 5 ");
private JButton button6 = new JButton(" 6 ");
private JButton button7 = new JButton(" 7 ");
private JButton button8 = new JButton(" 8 ");
private JButton button9 = new JButton(" 9 ");
private JButton button10 = new JButton(" 10 ");
private JButton button11 = new JButton(" 11 ");
private JButton button12 = new JButton(" 12 ");
private String amm[]={"25,000","50,000","75,000","100,000"}; 
int number;
private boolean clicked = false;


//constructor for deal or no deal
public Dealornodeal2(){

    frame.setSize(500,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLayout(new GridLayout(3,3));
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.add(button4);
    frame.add(button5);
    frame.add(button6);
    frame.add(button7);
    frame.add(button8);
    frame.add(button9);
    frame.add(button10);
    frame.add(button11);
    frame.add(button12);

    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    button7.addActionListener(this);
    button8.addActionListener(this);
    button9.addActionListener(this);
    button10.addActionListener(this);
    button11.addActionListener(this);
    button12.addActionListener(this);

}

//的ActionListener
    public void actionPerformed(ActionEvent a){

    if(a.getSource() == button1){
        button1.setText(amm[(int)(Math.random() * amm.length)]);
        button1.setEnabled(false);


    }
    else if(a.getSource() == button2){
        button2.setText(amm[(int)(Math.random() * amm.length)]);
        button2.setEnabled(false);

    }
    else if(a.getSource() == button3){
        button3.setText(amm[(int)(Math.random() * amm.length)]);
        button3.setEnabled(false);

    }
    else if(a.getSource() == button4){
        button4.setText(amm[(int)(Math.random() * amm.length)]);
        button4.setEnabled(false);

    }
    else if(a.getSource() == button5){
        button5.setText(amm[(int)(Math.random() * amm.length)]);
        button5.setEnabled(false);

    }
    else if(a.getSource() == button6){
        button6.setText(amm[(int)(Math.random() * amm.length)]);
        button6.setEnabled(false);

    }
    else if(a.getSource() == button7){
        button7.setText(amm[(int)(Math.random() * amm.length)]);
        button7.setEnabled(false);

    }
    else if(a.getSource() == button8){
        button8.setText(amm[(int)(Math.random() * amm.length)]);
        button8.setEnabled(false);

    }
    else if(a.getSource() == button9){
        button9.setText(amm[(int)(Math.random() * amm.length)]);
        button9.setEnabled(false);

    }
    else if(a.getSource() == button10){
        button10.setText(amm[(int)(Math.random() * amm.length)]);
        button10.setEnabled(false);

    }
    else if(a.getSource() == button11){
        button11.setText(amm[(int)(Math.random() * amm.length)]);
        button11.setEnabled(false);

    }
    else if(a.getSource() == button12){
        button12.setText(amm[(int)(Math.random() * amm.length)]);
        button12.setEnabled(false);

    }



}

}

1 个答案:

答案 0 :(得分:1)

您必须在actionPerformed方法中设置一个计数器,然后显示Swing弹出窗口。它背后没有任何魔力。

Swing课程中:

private int buttonsPressed = 0;

public void actionPerformed(ActionEvent e) {
    buttonsPressed++;
    if(buttonsPressed >= 3) {
        JOptionPane.showMessageDialog(null, "You clicked three buttons", "App Title", JOptionPane.INFORMATION_MESSAGE);
        buttonsPressed = 0; //Assuming you want to reset the functionality
    }
}

你可以添加更明显的优雅,但这是一般的想法。在您的特定用例中,我可能会将所有可能的按钮添加到List,以便您可以验证ActionEvent的来源。如,

private final List<JButton> buttons = new ArrayList<JButton>();

public DealOrNoDeal {
    // your normal initialization
    buttons.add(button1);
    buttons.add(button2);
    // ... and so on
}

...然后你可以验证输入。

public void actionPerformed(ActionEvent e) {
    if(buttons.contains(e.getSource()) {
        //do stuff
    }
}