是在Java中编写插槽计算机的代码,并希望将其分成3个文件,但它不起作用



不知道什么不起作用

//slot machine class
package slotMachine;
//import java.util.Arrays;
import java.util.Random;

# public class SlotGame {
    private int winCreadits=0;
    private boolean winState;
    static int slotMidPosition= 0;
    Random rand = new Random(); 

    //wheel contains: "melon", "bell", "7", "lemon", "bar" (5 total icons)
    static String slotBand[] = {"melon", " bell", "    7", "lemon", "  bar"};

        // slotOut is used to store the output values for the slot machine
    static String slotOut[] = new String[]
            {
                    "melon","melon","melon" 
            };
    public SlotGame()
     {
     init();    

     }
     public void checkWin()
     {
    checkConections();
    if(winState)
    {
        if (slotOut[1]=="melon")
        {
            winCreadits=1;
        }
        else if (slotOut[1]=="lemon")
        {
            winCreadits=1;
        }
        else if (slotOut[1]=="bell")
        {
            winCreadits=2;
        }
        else if (slotOut[1]=="bar")
        {
            winCreadits=3;
        }
        else if (slotOut[1]=="7")
        {
            winCreadits=5;
        }
        else 
        {
            winCreadits=0;
        }
    }

     }
     public int getWinAmount()
     {
        return winCreadits;
     }
     public String[] returnWheel()
     {
        return slotOut;
     }
     private void init()
     {
        getSlot( );
     }
     public void getSlot()
     {
    winState=false;
    for (int i=0;i<3;i++)
    {
        slotMidPosition = rand.nextInt(5);
        if (slotMidPosition==0)
        {
            slotOut[0]=slotBand[4];
        }
        else if(slotMidPosition>=0)
        {
            slotOut[0]=slotBand[slotMidPosition-1];
        }
        slotOut[1]=slotBand[slotMidPosition];
        slotOut[2]=slotBand[(slotMidPosition+1)%5];
         }
    checkWin();
     }

     private void outputArray(String[] slotOut)
     {
         for(int i = 0;i<3;i++)
        {

            System.out.print(slotOut[i]);
            System.out.print(" ");

        }
     }
  dont know wats going on at a point


         public void checkConections()
     {
    if(slotOut[0]==slotOut[1]&&slotOut[1]==slotOut[2])
        winState = true;
    else
        winState=false;
     }

     }

控制器类

    package slotMachine;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.util.Arrays;
    import javax.swing.*;
    import edu.cuny.csi.csc330.swing.TTTGameView;


    public class SlotController implements 
    ActionListener, 
    WindowListener{
private boolean ifWork=false;
private SlotView gameView;
private SlotGame gameModel;
private int winAmount;
private String wheel[];
public SlotController() {
    init();
}
public void setGameView(SlotView gameView) {
    this.gameView = gameView;
}
private void init()
{
    gameModel = new SlotGame();
}
@Override
    public void actionPerformed(ActionEvent ae) {
    Object source = ae.getSource();
    ifWork= (source == this.gameView.getSpinButton());
    System.out.println(ifWork);
    System.out.println("Slot actionPerformed: "  +                                              
    source.getClass().getSimpleName());
    // Spin button 
    if(source == this.gameView.getSpinButton() ) 
    {
        winAmount = gameView.getCreadit();
        System.out.println(gameView.getCreadit());
        gameModel.getSlot();
        wheel=gameModel.returnWheel();
        winAmount+= gameModel.getWinAmount();
        System.out.println(Arrays.toString(wheel));
        gameView.setCredit(winAmount);
        gameView.spunWheelDec();
        gameView.importWheel(wheel);
        System.out.println(Arrays.toString(gameView.wheelSet()));
    }

}

@Override
public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
    // TODO Auto-generated method stub
}
public static void main(String[] args) {
    SlotView.present();
    }
}

//gui

slotview
package slotMachine;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.lang.reflect.Array;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.ActionEvent;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import edu.cuny.csi.csc330.swing.TTTController;
import edu.cuny.csi.csc330.swing.TTTGameView;



public class SlotView extends JFrame{
private static Integer DEFAULT_X = 200;
private static Integer DEFAULT_Y = 200;
private static Integer DEFAULT_W = 1000; 
private static Integer DEFAULT_H = 500;

private static String DEFAULT_TITLE = "Spinny Thing Game";

private String[] Wheel= new String[3];
private JButton spinButton;
private ImageIcon imageArr1;
private ImageIcon imageArr2;
private ImageIcon imageArr3;
private ImageIcon image;
private JLabel imageIcone;

private JPanel mainPanel;
private JLabel SlotPos_1;
private JLabel SlotPos_2;
private JLabel SlotPos_3;
private JPanel Slot_1;
private JPanel Slot_2;
private JPanel Slot_3;
private JPanel slotPanel; 
private JPanel CreditPanel;
private JPanel betPanel; 
private JPanel mainOutPut;
private JLabel  currentBet;
private JLabel  currentPoints;
private JLabel  pointsLabel;
private JLabel  betLabel;

private ActionListener actionListener;  
private WindowListener windowListener;

private int credit, bet=1;


public SlotView() {
    guiInit();
}
public SlotView(SlotController listener) {
    actionListener = listener;
    windowListener = listener;
    guiInit();
}
    public SlotView(GraphicsConfiguration gc) {
    super(gc);
    guiInit();
}
public void setCredit(int cred)
{
    credit = cred;
}
public void spunWheelDec()
{
    credit-=bet;
    this.pointsLabel=createStatusLabel(String.format("%d", credit) );
}
public int getCreadit()
{
    return credit;
}
public JLabel getCurentPoints() {
    return this.currentPoints;
}
public JLabel getCurrentBet() {
    return currentBet;
}
public JButton getSpinButton(){
    return spinButton;
}
private void init()
{
    credit=50;
    this.setSize(DEFAULT_W, DEFAULT_H);
    this.getContentPane().setBackground(new Color(100, 149, 237));
    this.setLocation(DEFAULT_X, DEFAULT_X);
    //this.setBounds(100, 100, 1000, 500);
    this.setTitle(DEFAULT_TITLE);
    this.getContentPane().setLayout(null);

}
private void image()
{

      for(int i = 0; i<3;i++) {
        if (Wheel[i]=="melon")
            imageArr1 = new 
        ImageIcon("C:\Users\bn\Pictures\images\Watermelon.png");
        if (Wheel[i]=="bell")
            imageArr1= new 
             ImageIcon("C:\Users\bn\Pictures\images\Bell.png");
        if (Wheel[i]=="7")
            imageArr1= new 
               ImageIcon("C:\Users\bn\Pictures\images\Seven.png");
        if (Wheel[i]=="lemon")
            imageArr1= new 
           ImageIcon("C:\Users\bn\Pictures\images\Cherry.png");
        if (Wheel[i]=="bar")
            imageArr1= new 
               ImageIcon("C:\Users\bn\Pictures\images\Bar.png");
    }
    for(int i = 0; i<3;i++) {
        if (Wheel[i]=="melon")
            imageArr2 = new 
          ImageIcon("C:\Users\bn\Pictures\images\Watermelon.png");
        if (Wheel[i]=="bell")
            imageArr2= new 
                 ImageIcon("C:\Users\bn\Pictures\images\Bell.png");
        if (Wheel[i]=="7")
            imageArr2= new 
             ImageIcon("C:\Users\bn\Pictures\images\Seven.png");
        if (Wheel[i]=="lemon")
            imageArr2= new 
         ImageIcon("C:\Users\bn\Pictures\images\Cherry.png");
        if (Wheel[i]=="bar")
            imageArr2= new 
              ImageIcon("C:\Users\bn\Pictures\images\Bar.png");
    }
    for(int i = 0; i<3;i++) {
        if (Wheel[i]=="melon")
            imageArr3 = new 
          ImageIcon("C:\Users\bn\Pictures\images\Watermelon.png");
        if (Wheel[i]=="bell")
            imageArr3= new 
             ImageIcon("C:\Users\bn\Pictures\images\Bell.png");
        if (Wheel[i]=="7")
            imageArr3= new 
         ImageIcon("C:\Users\bn\Pictures\images\Seven.png");
        if (Wheel[i]=="lemon")
            imageArr3= new 
     ImageIcon("C:\Users\bn\Pictures\images\Cherry.png");
        if (Wheel[i]=="bar")
            imageArr3= new                  
    ImageIcon("C:\Users\bn\Pictures\images\Bar.png");
    }


}
private JLabel createStatusLabel(String title)  {
    JLabel label = new JLabel(title);
    label.setName( title );
    label.setFont( new Font("Dialog", Font.BOLD, 18) );
    label.setPreferredSize(new Dimension(100, 35));
    label.setBackground(Color.YELLOW);
    return label; 
}
public void importWheel(String[] W)
{
    Wheel=W;
}
public void setWheels()
{
    Slot_1.add(SlotPos_1);
    Slot_2.add(SlotPos_2);
    Slot_3.add(SlotPos_3);
}
public String[] wheelSet()
{
    return Wheel;
}

private void guiInit() {
    init();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    currentBet = createStatusLabel("Bet: ");
    currentPoints = createStatusLabel("Credit: ");

    SlotPos_1= new JLabel(imageArr1);   
    SlotPos_1.setBounds(0, 0, 181, 163);
    SlotPos_2= new JLabel(imageArr2);
    SlotPos_2.setBounds(0, 0, 181, 163);

    SlotPos_3=new JLabel(imageArr3);
    SlotPos_3.setBounds(0, 0, 181, 163);

    //set up a main panel for slot wheels
    slotPanel = new JPanel();
    slotPanel.setBackground(new Color(0, 0, 205));
    slotPanel.setBounds(40, 40, 914, 185);
    slotPanel.setLayout(null);
    //set up slot 1
    Slot_1 = new JPanel();
    Slot_1.add(SlotPos_1);
    Slot_1.setBackground(Color.BLACK);
    Slot_1.setBounds(10, 11, 181, 163);
    slotPanel.add(Slot_1);
    Slot_1.setLayout(null);

    //set slot 2
    Slot_2 = new JPanel();
    Slot_2.add(SlotPos_2);
    Slot_2.setBackground(Color.BLACK);
    Slot_2.setBounds(360, 11, 181, 163);
    slotPanel.add(Slot_2);
    Slot_2.setLayout(null);
    Slot_2.setVisible(true);
    //set slot 3
    Slot_3 = new JPanel();
    Slot_3.add(SlotPos_3);
    Slot_3.setBackground(Color.BLACK);
    Slot_3.setBounds(723, 11, 181, 163);
    slotPanel.add(Slot_3);
    Slot_3.setLayout(null);
    Slot_3.setVisible(true);
    // set up for data output and buttons
    mainOutPut = new JPanel();
    mainOutPut.setBackground(new Color(112, 128, 144));
    mainOutPut.setBounds(40, 283, 914, 160);
    mainOutPut.setLayout(null);

    // outputs credit value
    CreditPanel = new JPanel(new GridLayout(1,2), true);
    CreditPanel.setBackground(new Color(112, 128, 144));
    CreditPanel.setBounds(10, 65, 222, 57);
    CreditPanel.add(currentPoints);
    pointsLabel=createStatusLabel(String.format("%d", credit) );
    CreditPanel.add(pointsLabel);
    mainOutPut.add(CreditPanel);
    //output for bets
    betPanel = new JPanel();
    betPanel.setBackground(new Color(112, 128, 144));
    betPanel.setBounds(682, 65, 222, 65);
    betPanel.add(currentBet);
    betLabel = createStatusLabel(Integer.toString(bet));
    betPanel.add(betLabel);
    mainOutPut.add(betPanel);
    //creates button for spinning the wheel
    spinButton = new JButton("SPIN WHEEL");
    spinButton.setForeground(Color.BLACK);
    spinButton.setBackground(new Color(34, 139, 34));
    spinButton.setBounds(381, 57, 139, 65);
    mainOutPut.add(spinButton);



    this.getContentPane().add(slotPanel);       
    this.getContentPane().add(mainOutPut);      
    //this.setVisible(true);
    addActionListeners();
    addOtherListeners();
}
private void addActionListeners() {
    if(this.actionListener == null)
            return; 
    this.spinButton.addActionListener(this.actionListener); 
}


private void addOtherListeners() {
    addWindowListener(windowListener);
}
public static void present() {
     // The recommended way of startin a Swing / GUI Thread 
      EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                SlotController listener = new SlotController(); 
                SlotView game = new SlotView(listener);
                listener.setGameView(game);
                game.setVisible(true);
            }
        }
      );
}
public static void main(String[] args) {

    present();
/*  JFrame frame = new JFrame();
    frame.setSize(new Dimension(1000,400));
    frame.getContentPane().setBackground(new Color(100, 149, 237));
    frame.setResizable(false);
    JPanel jpn = new JPanel();
    //frame.getContentPane().setLayout(null);
    frame.getContentPane().add(jpn);
    JButton spin= new JButton("spin");
    spin.setBounds(10,10,100,100);
    jpn.setBackground(new Color(100, 149, 237));
    jpn.add(spin);
    frame.setTitle("Test");
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    */
}

}

它不需要我要求它的图像并播放它们。此外,信用不会更新

,因为您在项目中没有创建一个文件夹,然后将它们移动到那里,并且不再使用此类外部路线,我会给您一个示例假设他们在我拥有项目的包装中创建一个称为图像的文件夹,以便路线只需给予它

ImageIcon("imagenes/imagen.png");

更改

ImageIcon ("C: \ Users \ bn \ Pictures \ images \ Watermelon.png");

 ImageIcon ("C:  Users  bn  Pictures \ images  Watermelon.png");

最新更新