如何从另一个面板使用switch语句?

问题描述:

我正在为一个本质上是各种*的类项目工作。它有三列,让用户点击一个按钮来改变它上面的面板的形状。底部是实际的*,目标是尝试猜测“*”在旋转后会显示什么。我已经编写了面板和switch语句,但我无法更改面板中用户应该更改顶部的形状。任何人都可以让我在正确的方向如何去做这件事?如何从另一个面板使用switch语句?

这是创建按钮和他们的行动小组:

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; 
    } 
    } 
} 

谢谢你提前获得任何帮助!

+0

你需要的是某种模型和观察者模式。模型持有并控制着状态,当状态发生变化时它会向所有感兴趣的各方发出通知。看看[模型 - 视图 - 控制器](https://blog.codinghorror.com/understanding-model-view-controller/)和[观察者模式](http://www.oodesign.com/observer-pattern .html)了解更多详情 – MadProgrammer

好的,这里是你的代码的工作版本,通过了解下面的错误,我添加了自己的主要方法用于测试目的。

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值e试图改变JPanels中的形状,但switch语句是一种更奇怪的方法,因为你只是想增加它,还有一次你增加了实际上没有做任何事情的数字,因为你从来没有称过变化形状方法。我在ImagePanel类中添加了一个incrementCurrent方法,现在可以处理这个问题。

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

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