【问题标题】:I need help on my Deal or no Deal Java game我需要有关我的 Deal or no Deal Java 游戏的帮助
【发布时间】:2014-05-16 22:06:18
【问题描述】:

我正在尝试进行简单的交易或无交易游戏。我还没有真正进步,但希望我能完成它。到目前为止,我只制作了一个带有 JButtons 的 JFrame,当您单击按钮时,会出现数量。我接下来要做的是,当您单击至少 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);

}

//动作监听器
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);

    }



}

}

【问题讨论】:

  • 您是否正在寻找一个需要单击三个按钮才能激活的魔术事件?即使确实存在这种东西,我也不知道我会花时间找到它。只需使用计数器。 单击增量。我们三点了吗? 单击增量。我们到了吗? 单击增量。繁荣!我们三点!做一些特别的事情,然后将我们的计数器重置为零。
  • 考虑使用数组代替button1button2button3,...这样维护起来会更有趣!
  • 如果您想跟踪所有点击,请使用单独的计数器或使用计数器上的 Java mod (%) 运算符。

标签: java swing user-interface


【解决方案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
    }
}

【讨论】:

  • @user3215637 当然可以。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-28
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 2013-08-03
  • 2021-12-09
相关资源
最近更新 更多