如何使用其他面板的switch语句?

时间:2017-04-25 21:34:38

标签: java swing

我正在为一个基本上是各种老虎机的课程开展项目。顶部有3列,用户可以单击按钮更改其上方面板中的形状。底部是实际的老虎机,目标是尝试猜测"老虎机"将在纺纱后展示。虽然我在更改用户应该在顶部更改的面板中的形状时遇到了问题,但我已经编写了面板和开关声明。谁能让我朝着正确的方向发展呢?

这是创建按钮及其操作的面板:

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

public class SlotPanel extends JPanel {
 ImagePanel slotOne, slotTwo, slotThree; // three panels for the computer to 
generate
 ImagePanel userOne, userTwo, userThree; // three panels chosen by the user

// Panels used in SlotPanel
 JPanel titlePanel, guessPanel, slotTitlePanel;
 JPanel selectOnePanel, selectTwoPanel, selectThreePanel;
 JLabel title, guess, slotTitle;
 JButton selectOne, selectTwo, selectThree, spinMachine;
 private int currentShape;

 public SlotPanel() {
 currentShape = 0;
 // Instantiate all the objects declared above
slotOne=new ImagePanel(); slotTwo=new ImagePanel(); slotThree=new ImagePanel();
userOne=new ImagePanel(); userTwo=new ImagePanel(); userThree=new ImagePanel();

titlePanel=new JPanel();
guessPanel=new JPanel();
slotTitlePanel=new JPanel();
title = new JLabel("Austin's Wheel of Wacky Shapes"); // Change this label for the top of the display section for the default display
guess = new JLabel("Your Guess");
slotTitle = new JLabel("Slot Machine Wheel");
selectOnePanel=new JPanel(); selectTwoPanel=new JPanel(); selectThreePanel=new JPanel();
// Each of the three buttons to select the object has the same label
selectOne=new JButton("Wheel 1"); selectTwo=new JButton("Wheel 2"); selectThree=new JButton("Wheel 3");
// Button to spin the machine at the bottom
spinMachine=new JButton("Spin Machine!");

ObjectChanger listener = new ObjectChanger();
selectOne.addActionListener (listener);
selectTwo.addActionListener (listener);
selectThree.addActionListener (listener);

setBackgroundColor(Color.gray);
setPreferredSize(new Dimension(350, 415));

// Select the sizes for each of the panels
titlePanel.setPreferredSize(new Dimension(300,20));
guessPanel.setPreferredSize(new Dimension(300,20));
slotTitlePanel.setPreferredSize(new Dimension(300,20));
selectOnePanel.setPreferredSize(new Dimension(110, 50));
selectOnePanel.setBackground(Color.red);
selectTwoPanel.setPreferredSize(new Dimension(110, 50));
selectTwoPanel.setBackground(Color.red);
selectThreePanel.setPreferredSize(new Dimension(110, 50));
selectThreePanel.setBackground(Color.red);
spinMachine.setPreferredSize(new Dimension(240, 20));
selectOne.setPreferredSize(new Dimension(110,30));
selectTwo.setPreferredSize(new Dimension(110,30));
selectThree.setPreferredSize(new Dimension(110,30));
// Action listeners that will take care of the changing of the objects displayed
selectOne.addActionListener(new ObjectChanger());
selectTwo.addActionListener(new ObjectChanger());
selectThree.addActionListener(new ObjectChanger());
// Action listener to get the "machine" in motion
spinMachine.addActionListener(new Spin());
slotOne.setPreferredSize(new Dimension(110,100));
slotOne.setBackground(Color.white);
slotTwo.setPreferredSize(new Dimension(110,100));
slotTwo.setBackground(Color.white);
slotThree.setPreferredSize(new Dimension(110,100));
slotThree.setBackground(Color.white);


// Add the title JLabel to the top panel
titlePanel.add(title);
guessPanel.add(guess);
slotTitlePanel.add(slotTitle);
// Add the selection button to the user-selectable panel one
selectOnePanel.add(selectOne); //
// Add the selection button to the user-selectable panel two
selectTwoPanel.add(selectTwo);
// Add the selection button to the user-selectable panel three
selectThreePanel.add(selectThree);

add(titlePanel);
add(guessPanel);
add(userOne);
add(userTwo);
add(userThree);
add(selectOnePanel);
add(selectTwoPanel);
add(selectThreePanel);
add(spinMachine);
add(slotTitlePanel);
add(slotOne);
add(slotTwo);
add(slotThree);
}

public void setBackgroundColor(Color color){

}

// ActionListener class to determine which shape button was pressed
public class ObjectChanger implements ActionListener{

 @Override
  public void actionPerformed(ActionEvent event){

    //Identifies which buttons have been pressed to change their according slot wheel
      if (event.getSource() == selectOne);{
        while (currentShape == 0){
          switch (currentShape){
              case 0:
                  currentShape++;
              break;
              case 1:
                  currentShape++;
              break;
              case 2:
                  currentShape++;
              break;
              case 3:
                  currentShape = 0;
              break;
          }
        }
      }
    if (event.getSource() == selectTwo);{
        while (currentShape == 0){
          switch (currentShape){
              case 0:
                  currentShape++;
              break;
              case 1:
                  currentShape++;
              break;
              case 2:
                  currentShape++;
              break;
              case 3:
                  currentShape = 0;
              break;
          }
          }
      }
    if (event.getSource() == selectThree);{
        while (currentShape == 0){
          switch (currentShape){
              case 0:
                  currentShape++;
              break;
              case 1:
                  currentShape++;
              break;
              case 2:
                  currentShape++;
              break;
              case 3:
                  currentShape = 0;
              break;
          }
          }
      }
    }
}

// ActionListener class to determine when the Spin button is called

public class Spin implements ActionListener{

  public void actionPerformed(ActionEvent event){
    Random generator = new Random();

        //Changes the background color to green if all of the wheels match
        //or red if even one does not match to its designated wheel
        if (slotOne == userOne && slotTwo == userTwo && slotThree == userThree)
            setBackgroundColor(Color.green);
        else
            setBackgroundColor(Color.red);


    }
  }
}

这就是我制作的图像面板,用于创建正在更改的面板:

import java.awt.*;
import javax.swing.*;

// This class describes each of the Image Panels (6 total displayed)
// And provides methods for drawing of the shapes
public class ImagePanel extends JPanel{
// Use an int value to keep the current shape for the object
private int currentShape, count;
// Constructs the panels by assigning their shape and size
public ImagePanel() {
setPreferredSize(new Dimension(110,100));
setBackground(Color.black);
currentShape = 0;

}

// Redraws the object after "Spin" has been selected
public void changeShape(){
  currentShape++;
  if (currentShape > 3)
        currentShape = 0;

  // Repaint is called
  repaint();
}

// Used when the "SpinMachine" button is called to randomly generate shapes
public void changeShape(int setShape){
  currentShape=setShape;
  repaint();
}

// Accessor for the shape
public int getShape(){
  return currentShape;
}

// PaintComponent method takes care of the drawing
public void paintComponent(Graphics g){

  super.paintComponent(g);
  g.setColor(Color.blue);

//Switch method to change between the four shapes needed for the slot machine
  switch (currentShape) {
    case 0: g.fillRect(25, 20, 35, 70);  
    break;
    case 1: 
      g.fillOval(25, 20, 70, 70);
      break;
    case 2:
      int x[]={55,15,95}, y[]={25,95,95};
      g.fillPolygon(x,y,3);
      break;
    case 3:
       g.fillRect(25, 25, 70, 35);
      break;
    default:
      break;
   }
  }
 }

感谢您提前获得的任何帮助!

1 个答案:

答案 0 :(得分:-1)

好的,所以这里是您的代码的工作版本,随后了解下面出现的问题,我添加了自己的主要方法用于测试目的。

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

public class SlotPanel extends JPanel {

public static void main(String[] args){
    JFrame frame = new JFrame("TESt");
    frame.add(new SlotPanel());
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
ImagePanel slotOne, slotTwo, slotThree; // three panels for the computer to 
//generate
ImagePanel userOne, userTwo, userThree; // three panels chosen by the user

// Panels used in SlotPanel
JPanel titlePanel, guessPanel, slotTitlePanel;
JPanel selectOnePanel, selectTwoPanel, selectThreePanel;
JLabel title, guess, slotTitle;
JButton selectOne, selectTwo, selectThree, spinMachine;
private int currentShape;

public SlotPanel() {
    currentShape = 0;
    // Instantiate all the objects declared above
    slotOne=new ImagePanel(); slotTwo=new ImagePanel(); slotThree=new ImagePanel();
    userOne=new ImagePanel(); userTwo=new ImagePanel(); userThree=new ImagePanel();

    titlePanel=new JPanel();
    guessPanel=new JPanel();
    slotTitlePanel=new JPanel();
    title = new JLabel("Austin's Wheel of Wacky Shapes"); // Change this label for the top of the display section for the default display
    guess = new JLabel("Your Guess");
    slotTitle = new JLabel("Slot Machine Wheel");
    selectOnePanel=new JPanel(); selectTwoPanel=new JPanel(); selectThreePanel=new JPanel();
    // Each of the three buttons to select the object has the same label
    selectOne=new JButton("Wheel 1"); selectTwo=new JButton("Wheel 2"); selectThree=new JButton("Wheel 3");
    // Button to spin the machine at the bottom
    spinMachine=new JButton("Spin Machine!");

    //Um why?
    //ObjectChanger listener = new ObjectChanger();
    //selectOne.addActionListener (listener);
    //selectTwo.addActionListener (listener);
    //selectThree.addActionListener (listener);

    setBackgroundColor(Color.gray);
    setPreferredSize(new Dimension(350, 415));

    // Select the sizes for each of the panels
    titlePanel.setPreferredSize(new Dimension(300,20));
    guessPanel.setPreferredSize(new Dimension(300,20));
    slotTitlePanel.setPreferredSize(new Dimension(300,20));
    selectOnePanel.setPreferredSize(new Dimension(110, 50));
    selectOnePanel.setBackground(Color.red);
    selectTwoPanel.setPreferredSize(new Dimension(110, 50));
    selectTwoPanel.setBackground(Color.red);
    selectThreePanel.setPreferredSize(new Dimension(110, 50));
    selectThreePanel.setBackground(Color.red);
    spinMachine.setPreferredSize(new Dimension(240, 20));
    selectOne.setPreferredSize(new Dimension(110,30));
    selectTwo.setPreferredSize(new Dimension(110,30));
    selectThree.setPreferredSize(new Dimension(110,30));
    // Action listeners that will take care of the changing of the objects displayed
    selectOne.addActionListener(new ObjectChanger());
    selectTwo.addActionListener(new ObjectChanger());
    selectThree.addActionListener(new ObjectChanger());
    // Action listener to get the "machine" in motion
    spinMachine.addActionListener(new Spin());
    slotOne.setPreferredSize(new Dimension(110,100));
    slotOne.setBackground(Color.white);
    slotTwo.setPreferredSize(new Dimension(110,100));
    slotTwo.setBackground(Color.white);
    slotThree.setPreferredSize(new Dimension(110,100));
    slotThree.setBackground(Color.white);


    // Add the title JLabel to the top panel
    titlePanel.add(title);
    guessPanel.add(guess);
    slotTitlePanel.add(slotTitle);
    // Add the selection button to the user-selectable panel one
    selectOnePanel.add(selectOne); //
    // Add the selection button to the user-selectable panel two
    selectTwoPanel.add(selectTwo);
    // Add the selection button to the user-selectable panel three
    selectThreePanel.add(selectThree);

    add(titlePanel);
    add(guessPanel);
    add(userOne);
    add(userTwo);
    add(userThree);
    add(selectOnePanel);
    add(selectTwoPanel);
    add(selectThreePanel);
    add(spinMachine);
    add(slotTitlePanel);
    add(slotOne);
    add(slotTwo);
    add(slotThree);
}

public void setBackgroundColor(Color color){

}

// ActionListener class to determine which shape button was pressed
public class ObjectChanger implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent event){
        //Identifies which buttons have been pressed to change their according slot wheel
        if (event.getSource() == selectOne){
            userOne.incrementCurrent();
        }
        if (event.getSource() == selectTwo){
            userTwo.incrementCurrent();
        }
        if (event.getSource() == selectThree){
            userThree.incrementCurrent();
        }
    }
}

// ActionListener class to determine when the Spin button is called

public class Spin implements ActionListener{

    public void actionPerformed(ActionEvent event){
        Random generator = new Random();

        //Changes the background color to green if all of the wheels match
        //or red if even one does not match to its designated wheel
        if (slotOne == userOne && slotTwo == userTwo && slotThree == userThree)
            setBackgroundColor(Color.green);
        else
            setBackgroundColor(Color.red);


    }
}
}

这是第二节课:

import java.awt.*;
import javax.swing.*;

// This class describes each of the Image Panels (6 total displayed)
// And provides methods for drawing of the shapes
public class ImagePanel extends JPanel{
// Use an int value to keep the current shape for the object
private int count;
private int currentShape;
// Constructs the panels by assigning their shape and size
public ImagePanel() {
setPreferredSize(new Dimension(110,100));
setBackground(Color.black);
currentShape = 0;

}
public void incrementCurrent(){
    currentShape++;
    if(currentShape>3)
        currentShape=0;
    changeShape(currentShape);
}
// Redraws the object after "Spin" has been selected
public void changeShape(){
  currentShape++;
  if (currentShape > 3)
        currentShape = 0;

  // Repaint is called
  repaint();
}

// Used when the "SpinMachine" button is called to randomly generate shapes
public void changeShape(int setShape){
  currentShape=setShape;
  repaint();
}

// Accessor for the shape
public int getShape(){
  return currentShape;
}

// PaintComponent method takes care of the drawing
public void paintComponent(Graphics g){

  super.paintComponent(g);
  g.setColor(Color.blue);

//Switch method to change between the four shapes needed for the slot machine
  switch (currentShape) {
    case 0: g.fillRect(25, 20, 35, 70);  
    break;
    case 1: 
      g.fillOval(25, 20, 70, 70);
      break;
    case 2:
      int x[]={55,15,95}, y[]={25,95,95};
      g.fillPolygon(x,y,3);
      break;
    case 3:
       g.fillRect(25, 25, 70, 35);
      break;
    default:
      break;
   }
  }
 }

首先:你在这里添加了两次actionListener:

ObjectChanger listener = new ObjectChanger();
selectOne.addActionListener (listener);
selectTwo.addActionListener (listener);
selectThree.addActionListener (listener);

在这里:

selectOne.addActionListener(new ObjectChanger());
selectTwo.addActionListener(new ObjectChanger());
selectThree.addActionListener(new ObjectChanger());

其次你使用currentShape值来尝试改变JPanels中的形状,但是switch语句是一种奇怪的方法,因为你只想增加它,一旦增加你实际上没有对数字做任何事情因为你从来没有称过改变形状的方法。我在ImagePanel类中添加了一个incrementCurrent方法,现在可以处理它。

   public void incrementCurrent(){
    currentShape++;
    if(currentShape>3)
        currentShape=0;
    changeShape(currentShape);
}

因为这将改变值并重新绘制形状。

相关问题