我需要关于我的交易或无交易Java游戏的帮助
我想做一个简单的交易或不交易的游戏。我还没有真正进步,但希望我能完成它。到目前为止,我只制作了一个带有JButtons的JFrame,当您单击按钮时,amount就会出现。我接下来要做的是,当您单击至少3个按钮时,将出现一个对话框。我不知道这是否可能,但如果可能,请帮助我。我在Gui和事件处理方面只有一些经验 以下是我目前的代码:我需要关于我的交易或无交易Java游戏的帮助,java,swing,user-interface,Java,Swing,User Interface,我想做一个简单的交易或不交易的游戏。我还没有真正进步,但希望我能完成它。到目前为止,我只制作了一个带有JButtons的JFrame,当您单击按钮时,amount就会出现。我接下来要做的是,当您单击至少3个按钮时,将出现一个对话框。我不知道这是否可能,但如果可能,请帮助我。我在Gui和事件处理方面只有一些经验 以下是我目前的代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.
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已执行的公共无效操作(操作事件a){
}您必须在
操作执行
方法中有一个计数器,然后显示一个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
}
}
显然,你可以添加更多的优雅,但这只是一般的想法。在您的特定用例中,我可能会将所有可能的按钮添加到列表中
,以便您可以验证ActionEvent
的来源。e、 g
private final List<JButton> buttons = new ArrayList<JButton>();
public DealOrNoDeal {
// your normal initialization
buttons.add(button1);
buttons.add(button2);
// ... and so on
}
你在寻找一个需要点击三次按钮才能激活的魔法事件吗?即使这样的事情真的存在,我也不知道我会花时间去寻找它。就用柜台吧。单击“增量”。我们三点了吗?单击“增量”。我们到了吗?单击“增量”。繁荣我们三点了!做一些特别的事情,然后将计数器设置为零。考虑使用数组而不是<代码> ButoO1,<代码> ButoN2,<代码> ButoNo3,…这将是更有趣的维护!如果要跟踪所有单击,请使用单独的计数器或在计数器上使用Java mod(%)运算符。@user3215637当然。:)
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
}
}