2015-06-02 2 views
0

Мой код должен отображаться в качестве фона типа игрового автомата. В каждой полосе нет изображений, которые не активируются, и когда вы нажимаете spin, они рандомизируют, какие из них становятся видимыми, а если нет предполагается, что они видны, они установлены как невидимые. Моя проблема - когда вы нажимаете кнопку, изображения не отображаются. Когда я устанавливаю их видимыми, как правило, они появляются. Поэтому я считаю, что это когда я пытаюсь установить их видимыми в операторах if/else.Java не реализует Int внутри, если изменилось

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.*; 
import java.awt.Dimension; 
import javax.swing.ImageIcon; 

public class Slots1 extends JFrame implements ActionListener 
{ 
    JPanel panel; 
    JButton spin; 
    int wheelOne = 0; 
    JLabel ImgSlot1; 
    JLabel ImgSlot12; 
    JLabel ImgSlot13; 
    JLabel ImgSlot14; 
    JLabel ImgSlot15; 
    JLabel ImgSlot16; 
    JLabel ImgSlot17; 

    public Slots1() 
    { 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    double width = screenSize.getWidth(); 
    double height = screenSize.getHeight(); 
    int wid = (int) width; 
    int hgt = (int) height; 

    JFrame frame = new JFrame("sesame chicken"); 
    frame.setSize(wid,hgt); 
    frame.setUndecorated(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    panel = new JPanel(null); 
    panel.setBackground(Color.GRAY); 

    ImageIcon pic = new ImageIcon("Backr.png"); 
    JLabel layout = new JLabel(pic); 
    layout.setLocation(0,0); 
    layout.setSize(1366,768); 

    ImageIcon Slot1 = new ImageIcon("SLu1.png"); 
    ImageIcon Slot2 = new ImageIcon("SLu2.png"); 
    ImageIcon Slot3 = new ImageIcon("SLu3.png"); 
    ImageIcon Slot4 = new ImageIcon("SLu4.png"); 
    ImageIcon Slot5 = new ImageIcon("SLu5.png"); 
    ImageIcon Slot6 = new ImageIcon("SLu6.png"); 
    ImageIcon Slot7 = new ImageIcon("SLu7.png"); 

    ImgSlot1 = new JLabel(Slot1); 
    ImgSlot1.setLocation(75,25); 
    ImgSlot1.setSize(150,500); 
    JLabel ImgSlot12 = new JLabel(Slot2); 
    ImgSlot12.setLocation(75,25); 
    ImgSlot12.setSize(150,500); 
    JLabel ImgSlot13 = new JLabel(Slot3); 
    ImgSlot13.setLocation(75,25); 
    ImgSlot13.setSize(150,500); 
    JLabel ImgSlot14 = new JLabel(Slot4); 
    ImgSlot14.setLocation(75,25); 
    ImgSlot14.setSize(150,500); 
    JLabel ImgSlot15 = new JLabel(Slot5); 
    ImgSlot15.setLocation(75,25); 
    ImgSlot15.setSize(150,500); 
    JLabel ImgSlot16 = new JLabel(Slot6); 
    ImgSlot16.setLocation(75,25); 
    ImgSlot16.setSize(150,500); 


    JLabel ImgSlot17 = new JLabel(Slot7); 
    ImgSlot17.setLocation(75,25); 
    ImgSlot17.setSize(150,500); 

    ImgSlot1.setVisible(false); 
    ImgSlot12.setVisible(false); 
    ImgSlot13.setVisible(false); 
    ImgSlot14.setVisible(false); 
    ImgSlot15.setVisible(false); 
    ImgSlot16.setVisible(false); 
    ImgSlot17.setVisible(false); 

    JButton spin = new JButton("SPIN"); 
    spin.setSize(100,50); 
    spin.setLocation(100, 650); 
    spin.setBackground(Color.green); 
    spin.setForeground(Color.red); 

    panel.add(ImgSlot1); 
    panel.add(ImgSlot12); 
    panel.add(ImgSlot13); 
    panel.add(ImgSlot14); 
    panel.add(ImgSlot15); 
    panel.add(ImgSlot16); 
    panel.add(ImgSlot17); 
    panel.add(spin); 
    panel.add(layout); 
    frame.add(panel); 

    frame.setVisible(true); 
    panel.setVisible(true); 

    spin.addActionListener(this); 
    } 
    public void actionPerformed(ActionEvent e) 
    { 
    Object source = e.getSource(); 
    if (source == spin){ 
     Random rand = new Random(); 
     wheelOne = rand.nextInt(7); 
    } 

    if (wheelOne == 1){ 
     ImgSlot1.setVisible(true);} 
    else if (wheelOne == 2){ 
     ImgSlot12.setVisible(true);} 
    else if (wheelOne == 3){ 
     ImgSlot13.setVisible(true);} 
    else if ((wheelOne == 4)){ 
     ImgSlot14.setVisible(true);} 
    else if (wheelOne == 5){ 
     ImgSlot15.setVisible(true);} 
    else if (wheelOne == 6){ 
     ImgSlot16.setVisible(true);} 
    else if (wheelOne == 7){ 
     ImgSlot17.setVisible(true);} 

    if (wheelOne != 1){ 
     ImgSlot1.setVisible(false);} 
    if (wheelOne != 2){ 
     ImgSlot12.setVisible(false);} 
    if (wheelOne != 3){ 
     ImgSlot13.setVisible(false);} 
    if (wheelOne != 4){ 
     ImgSlot14.setVisible(false);} 
    if (wheelOne != 5){ 
     ImgSlot15.setVisible(false);} 
    if (wheelOne != 6){ 
     ImgSlot16.setVisible(false);} 
    if (wheelOne != 7){ 
     ImgSlot17.setVisible(false);} 

    } 
    public static void main(String[] args) 
    { 
    Slots1 run = new Slots1(); 
    } 
} 

Это код, который я закончил с

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.*; 
import java.awt.Dimension; 
import javax.swing.ImageIcon; 

public class Slots1 extends JFrame implements ActionListener 
{ 
    JPanel panel; 
    JButton spin; 
    JLabel ImgSlot1; 
    JLabel ImgSlot2; 
    JLabel ImgSlot3; 
    JLabel ImgSlot4; 
    JLabel ImgSlot5; 
    JLabel ImgSlot6; 
    JLabel ImgSlot7; 
    int wheelOne = 1; 
    public Slots1() 
    { 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    double width = screenSize.getWidth(); 
    double height = screenSize.getHeight(); 
    int wid = (int) width; 
    int hgt = (int) height; 

    JFrame frame = new JFrame("sesame chicken"); 
    frame.setSize(wid,hgt); 
    frame.setUndecorated(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    panel = new JPanel(null); 
    panel.setBackground(Color.GRAY); 

    ImageIcon pic = new ImageIcon("Backr.png"); 
    JLabel layout = new JLabel(pic); 
    layout.setLocation(0,0); 
    layout.setSize(1366,768); 

    ImageIcon Slot1 = new ImageIcon("SLu1.png"); 
    ImageIcon Slot2 = new ImageIcon("SLu2.png"); 
    ImageIcon Slot3 = new ImageIcon("SLu3.png"); 
    ImageIcon Slot4 = new ImageIcon("SLu4.png"); 
    ImageIcon Slot5 = new ImageIcon("SLu5.png"); 
    ImageIcon Slot6 = new ImageIcon("SLu6.png"); 
    ImageIcon Slot7 = new ImageIcon("SLu7.png"); 

    ImgSlot1 = new JLabel(Slot1); 
    ImgSlot1.setLocation(75,25); 
    ImgSlot1.setSize(150,500); 
    ImgSlot2 = new JLabel(Slot2); 
    ImgSlot2.setLocation(75,25); 
    ImgSlot2.setSize(150,500); 
    ImgSlot3 = new JLabel(Slot3); 
    ImgSlot3.setLocation(75,25); 
    ImgSlot3.setSize(150,500); 
    ImgSlot4 = new JLabel(Slot4); 
    ImgSlot4.setLocation(75,25); 
    ImgSlot4.setSize(150,500); 
    ImgSlot5 = new JLabel(Slot5); 
    ImgSlot5.setLocation(75,25); 
    ImgSlot5.setSize(150,500); 
    ImgSlot6 = new JLabel(Slot6); 
    ImgSlot6.setLocation(75,25); 
    ImgSlot6.setSize(150,500); 
    ImgSlot7 = new JLabel(Slot7); 
    ImgSlot7.setLocation(75,25); 
    ImgSlot7.setSize(150,500); 

    ImgSlot1.setVisible(false); 
    ImgSlot2.setVisible(false); 
    ImgSlot3.setVisible(false); 
    ImgSlot4.setVisible(false); 
    ImgSlot5.setVisible(false); 
    ImgSlot6.setVisible(false); 
    ImgSlot7.setVisible(false); 

    spin = new JButton("SPIN"); 
    spin.setSize(100,50); 
    spin.setLocation(100, 650); 
    spin.setBackground(Color.green); 
    spin.setForeground(Color.red); 

    panel.add(ImgSlot1); 
    panel.add(ImgSlot2); 
    panel.add(ImgSlot3); 
    panel.add(ImgSlot4); 
    panel.add(ImgSlot5); 
    panel.add(ImgSlot6); 
    panel.add(ImgSlot7); 
    panel.add(spin); 
    panel.add(layout); 
    frame.add(panel); 

    frame.setVisible(true); 
    panel.setVisible(true); 

    spin.addActionListener(this); 
    System.out.println(spin); 
    } 
    public void actionPerformed(ActionEvent e) 
    { 
    Object source = e.getSource(); 
System.out.println(source); 
System.out.println(spin); 
System.out.println(); 
    if (source.equals(spin)){ 
    Random rand = new Random(); 
    wheelOne = rand.nextInt(7);} 

    if (wheelOne == 0){ 
     ImgSlot1.setVisible(true);} 
    else if (wheelOne != 0){ 
    ImgSlot1.setVisible(false);} 
    if (wheelOne == 1){ 
    ImgSlot2.setVisible(true);} 
    else if (wheelOne != 1){ 
    ImgSlot2.setVisible(false);} 
    if (wheelOne == 2){ 
    ImgSlot3.setVisible(true);} 
    else if (wheelOne != 2){ 
    ImgSlot3.setVisible(false);} 
    if ((wheelOne == 3)){ 
    ImgSlot4.setVisible(true);} 
    else if (wheelOne != 3){ 
    ImgSlot4.setVisible(false);} 
    if (wheelOne == 4){ 
    ImgSlot5.setVisible(true);} 
    else if (wheelOne != 4){ 
    ImgSlot5.setVisible(false);} 
    if (wheelOne == 5){ 
    ImgSlot6.setVisible(true);} 
    else if (wheelOne != 5){ 
    ImgSlot6.setVisible(false);} 
    if (wheelOne == 6){ 
    ImgSlot7.setVisible(true);} 
    else if (wheelOne != 6){ 
     ImgSlot7.setVisible(false);} 

    } 

    public static void main(String[] args) 
    { 
    Slots1 run = new Slots1(); 
    } 
} 
+2

пожалуйста, вы можете уменьшить объем кода, там много повторяющихся частей – Rocketq

+3

Ни в коем случае я не читаю эту стену кода, извините;) – zubergu

+0

http://en.wikipedia.org/wiki/Don%27t_repeat_yourself – kervin

ответ

0

Первая проблема как вы используете класс Random. Если вы создаете экземпляр снова и снова, а затем вызываете random.nextInt(7), вы всегда получите то же значение обратно (возможно, 0).

Вместо этого вам нужно создать экземпляр в конструкторе и назначить его переменной экземпляра.

Вторая проблема заключается в том, что nextInt(7) не возвращает целочисленное значение от 1 до 7, как вы предполагаете, но она возвращает целочисленное значение в диапазоне от 0 до 6.

+0

Да, мне также нужно было удалить JButton из 'JButton spin = new JButton (« Spin »)', потому что у него уже был 'JButton spin' в конструкторе, поэтому он делал его нулевым. –

0

Вы забыли инициализировать ImgSlot12 (и другие из них), вот почему он бросает исключение:

else if (wheelOne == 2){ 
     ImgSlot12.setVisible(true);} 

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at com.company.Slots1.actionPerformed(Slots1.java:132) 
    ... 
+0

Я все еще получаю эту ошибку, но мне нужно было удалить JLabel. 'JLabel ImgSlot13 = новый JLabel (Slot3);' –