我正在上课的轮盘赌游戏,不知道出了什么问题。它会编译,但是当我运行它时,面板就不会显示出来。如果你们中的一个人可以查看我的代码,那就太好了。而且我知道我可以使用循环来做很多这样的事情但是现在我只是想让它运转起来。 感谢
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Layout extends JFrame implements ActionListener{
public static String title = "Roulette Game";
public static Dimension size = new Dimension(610,150);
public static JPanel panel = new JPanel();
public static JButton spin = new JButton("Spin");
public static int TotalChecked;
public JLabel MoneyLabel = new JLabel("You have $ " + AmountOfMoney);
public JLabel empty = new JLabel (" ");
public static int AmountOfMoney = 36;
public static int Random;
public static int MoneyEarned;
//Check whether checkboxes are checked
public static boolean b1;
public static boolean b2;
public static boolean b3;
public static boolean b4;
public static boolean b5;
public static boolean b6;
public static boolean b7;
public static boolean b8;
public static boolean b9;
public static boolean b10;
public static boolean b11;
public static boolean b12;
public static boolean b13;
public static boolean b14;
public static boolean b15;
public static boolean b16;
public static boolean b17;
public static boolean b18;
public static boolean b19;
public static boolean b20;
public static boolean b21;
public static boolean b22;
public static boolean b23;
public static boolean b24;
public static boolean b25;
public static boolean b26;
public static boolean b27;
public static boolean b28;
public static boolean b29;
public static boolean b30;
public static boolean b31;
public static boolean b32;
public static boolean b33;
public static boolean b34;
public static boolean b35;
public static boolean b36;
//Checkboxes
JCheckBox a1 = new JCheckBox("1 ");
JCheckBox a2 = new JCheckBox("2 ");
JCheckBox a3 = new JCheckBox("3 ");
JCheckBox a4 = new JCheckBox("4 ");
JCheckBox a5 = new JCheckBox("5 ");
JCheckBox a6 = new JCheckBox("6 ");
JCheckBox a7 = new JCheckBox("7 ");
JCheckBox a8 = new JCheckBox("8 ");
JCheckBox a9 = new JCheckBox("9 ");
JCheckBox a10 = new JCheckBox("10 ");
JCheckBox a11 = new JCheckBox("11 ");
JCheckBox a12 = new JCheckBox("12 ");
JCheckBox a13 = new JCheckBox("13 ");
JCheckBox a14 = new JCheckBox("14 ");
JCheckBox a15 = new JCheckBox("15 ");
JCheckBox a16 = new JCheckBox("16 ");
JCheckBox a17 = new JCheckBox("17 ");
JCheckBox a18 = new JCheckBox("18 ");
JCheckBox a19 = new JCheckBox("19 ");
JCheckBox a20 = new JCheckBox("20 ");
JCheckBox a21 = new JCheckBox("21 ");
JCheckBox a22 = new JCheckBox("22 ");
JCheckBox a23 = new JCheckBox("23 ");
JCheckBox a24 = new JCheckBox("24 ");
JCheckBox a25 = new JCheckBox("25 ");
JCheckBox a26 = new JCheckBox("26 ");
JCheckBox a27 = new JCheckBox("27 ");
JCheckBox a28 = new JCheckBox("28 ");
JCheckBox a29 = new JCheckBox("29 ");
JCheckBox a30 = new JCheckBox("30 ");
JCheckBox a31 = new JCheckBox("31 ");
JCheckBox a32 = new JCheckBox("32 ");
JCheckBox a33 = new JCheckBox("33 ");
JCheckBox a34 = new JCheckBox("34 ");
JCheckBox a35 = new JCheckBox("35 ");
JCheckBox a36 = new JCheckBox("36 ");
public static void main(String args[]) {
System.out.println("Game started successfully");
Layout layout = new Layout();
}
public Layout(){
super(title);
setSize(size);
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(panel);
spin.addActionListener(this);
panel.add(a1);
panel.add(a2);
panel.add(a3);
panel.add(a4);
panel.add(a5);
panel.add(a6);
panel.add(a7);
panel.add(a8);
panel.add(a9);
panel.add(a10);
panel.add(a11);
panel.add(a12);
panel.add(a13);
panel.add(a14);
panel.add(a15);
panel.add(a16);
panel.add(a17);
panel.add(a18);
panel.add(a19);
panel.add(a20);
panel.add(a21);
panel.add(a22);
panel.add(a23);
panel.add(a24);
panel.add(a25);
panel.add(a26);
panel.add(a27);
panel.add(a28);
panel.add(a29);
panel.add(a30);
panel.add(a31);
panel.add(a32);
panel.add(a33);
panel.add(a34);
panel.add(a35);
panel.add(a36);
panel.add(spin);
panel.add(empty);
panel.add(MoneyLabel);
}
@Override
public void actionPerformed(ActionEvent event){
if (event.getSource() == spin) {
MoneyEarned = 0;
Random = (int)(Math.random() * ((36) + 1));
if(TotalChecked == 0){
JOptionPane.showMessageDialog (this, "Please check at least 1 box", "Warning", JOptionPane.WARNING_MESSAGE);
}
System.out.println("test button has been clicked");
if(a1.isSelected()){
b1 = true;
}else{
b1 = false;
}
if(a1.isSelected()){ b1 = true; }else{ b1 = false; }
if(a2.isSelected()){ b2 = true; }else{ b2 = false; }
if(a3.isSelected()){ b3 = true; }else{ b3 = false; }
if(a4.isSelected()){ b4 = true; }else{ b4 = false; }
if(a5.isSelected()){ b5 = true; }else{ b5 = false; }
if(a6.isSelected()){ b6 = true; }else{ b6 = false; }
if(a7.isSelected()){ b7 = true; }else{ b7 = false; }
if(a8.isSelected()){ b8 = true; }else{ b8 = false; }
if(a9.isSelected()){ b9 = true; }else{ b9 = false; }
if(a10.isSelected()){ b10 = true; }else{ b10 = false; }
if(a11.isSelected()){ b11 = true; }else{ b11 = false; }
if(a12.isSelected()){ b12 = true; }else{ b12 = false; }
if(a13.isSelected()){ b13 = true; }else{ b13 = false; }
if(a14.isSelected()){ b14 = true; }else{ b14 = false; }
if(a15.isSelected()){ b15 = true; }else{ b15 = false; }
if(a16.isSelected()){ b16 = true; }else{ b16 = false; }
if(a17.isSelected()){ b17 = true; }else{ b17 = false; }
if(a18.isSelected()){ b18 = true; }else{ b18 = false; }
if(a19.isSelected()){ b19 = true; }else{ b19 = false; }
if(a20.isSelected()){ b20 = true; }else{ b20 = false; }
if(a21.isSelected()){ b21 = true; }else{ b21 = false; }
if(a22.isSelected()){ b22 = true; }else{ b22 = false; }
if(a23.isSelected()){ b23 = true; }else{ b23 = false; }
if(a24.isSelected()){ b24 = true; }else{ b24 = false; }
if(a25.isSelected()){ b25 = true; }else{ b25 = false; }
if(a26.isSelected()){ b26 = true; }else{ b26 = false; }
if(a27.isSelected()){ b27 = true; }else{ b27 = false; }
if(a28.isSelected()){ b28 = true; }else{ b28 = false; }
if(a29.isSelected()){ b29 = true; }else{ b29 = false; }
if(a30.isSelected()){ b30 = true; }else{ b30 = false; }
if(a31.isSelected()){ b31 = true; }else{ b31 = false; }
if(a32.isSelected()){ b32 = true; }else{ b32 = false; }
if(a33.isSelected()){ b33 = true; }else{ b33 = false; }
if(a34.isSelected()){ b34 = true; }else{ b34 = false; }
if(a35.isSelected()){ b35 = true; }else{ b35 = false; }
if(a36.isSelected()){ b36 = true; }else{ b36 = false; }
if(b1 == true) { TotalChecked++; }
if(b2 == true) { TotalChecked++; }
if(b3 == true) { TotalChecked++; }
if(b4 == true) { TotalChecked++; }
if(b5 == true) { TotalChecked++; }
if(b6 == true) { TotalChecked++; }
if(b7 == true) { TotalChecked++; }
if(b8 == true) { TotalChecked++; }
if(b9 == true) { TotalChecked++; }
if(b10 == true) { TotalChecked++; }
if(b11 == true) { TotalChecked++; }
if(b12 == true) { TotalChecked++; }
if(b13 == true) { TotalChecked++; }
if(b14 == true) { TotalChecked++; }
if(b15 == true) { TotalChecked++; }
if(b16 == true) { TotalChecked++; }
if(b17 == true) { TotalChecked++; }
if(b18 == true) { TotalChecked++; }
if(b19 == true) { TotalChecked++; }
if(b20 == true) { TotalChecked++; }
if(b21 == true) { TotalChecked++; }
if(b22 == true) { TotalChecked++; }
if(b23 == true) { TotalChecked++; }
if(b24 == true) { TotalChecked++; }
if(b25 == true) { TotalChecked++; }
if(b26 == true) { TotalChecked++; }
if(b27 == true) { TotalChecked++; }
if(b28 == true) { TotalChecked++; }
if(b29 == true) { TotalChecked++; }
if(b30 == true) { TotalChecked++; }
if(b31 == true) { TotalChecked++; }
if(b32 == true) { TotalChecked++; }
if(b33 == true) { TotalChecked++; }
if(b34 == true) { TotalChecked++; }
if(b35 == true) { TotalChecked++; }
if(b36 == true) { TotalChecked++; }
if(Random == 1){ if(b1 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 2){ if(b2 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 3){ if(b3 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 4){ if(b4 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 5){ if(b5 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 6){ if(b6 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 7){ if(b7 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 8){ if(b8 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 9){ if(b9 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 10){ if(b10 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 11){ if(b11 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 12){ if(b12 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 13){ if(b13 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 14){ if(b14 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 15){ if(b15 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 16){ if(b16 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 17){ if(b17 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 18){ if(b18 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 19){ if(b19 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 20){ if(b20 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 21){ if(b21 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 22){ if(b22 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 23){ if(b23 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 24){ if(b24 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 25){ if(b25 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 26){ if(b26 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 27){ if(b27 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 28){ if(b28 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 29){ if(b29 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 30){ if(b30 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 31){ if(b31 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 32){ if(b32 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 33){ if(b33 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 34){ if(b34 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 35){ if(b35 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if(Random == 36){ if(b36 == true){ AmountOfMoney = AmountOfMoney - TotalChecked + 36; MoneyEarned = 36 - TotalChecked ; JOptionPane.showMessageDialog(this,"Congratulations you won $" + (36-TotalChecked)); } }
if (MoneyEarned == 0) {
JOptionPane.showMessageDialog(this,"You have lost $" + (TotalChecked));
}
MoneyLabel.setText("You have $ " + AmountOfMoney);
}
}
}
答案 0 :(得分:1)
在建立用户界面后,最后致电setVisible
public Layout(){
super(title);
// Use pack, it's safer, but do this JUST before you call setVisible...
setSize(size);
setResizable(false);
// Move me...
//setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(panel);
spin.addActionListener(this);
//...
panel.add(spin);
panel.add(empty);
panel.add(MoneyLabel);
// To here
setVisible(true);
}