简体   繁体   English

在用Java创建Lights游戏时需要帮助

[英]I need help in creating a Lights Game in Java

I have to write a GUI program to play a lights game. 我必须编写一个GUI程序来玩灯光游戏。 This game should display 6 lights, include a menu bar. 该游戏应显示6盏灯,包括一个菜单栏。

This is how far I have gotten so far: 这是我到目前为止所走的距离:

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

public class LightsGame extends JFrame{

LightsPanel lightPnl = new LightsPanel();

JButton switchButton[] = new JButton[6];

private LightsGame(){

    JFrame frame = new JFrame("Lights Game");

    JPanel buttonPanel = new JPanel();
    JPanel labelPanel = new JPanel();
    this.getContentPane().add(labelPanel);
    JLabel label = new JLabel("Switch on all the Lights");
    labelPanel.add(label);  

    //LightsPanel lightPnl = new LightsPanel();

    //Code for buttons:
    for (int k = 0; k <= 5; k++){
        switchButton[k] = new JButton("Light " + k );
        buttonPanel.add(switchButton[k]);
    }

    frame.add(labelPanel, BorderLayout.NORTH); 

    frame.add(lightPnl, BorderLayout.CENTER);
    //lightPnl.add(light1, BorderLayout.CENTER);

    frame.add(buttonPanel, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       

    frame.setVisible(true); //Displays the frame
    frame.pack(); //Makes the frame as big as it needs to be.


}//LightsGame()

private class ButtonHandler implements ActionListener {

    public void actionPerformed(ActionEvent e){

     if( e.getSource() == switchButton[0])  // toggle lights controlled by button 0

        //Switch the light on or off
        lightPnl.toggleLight();
    }//actionPerformed

}//ButtonHandler

private class MenuHandler implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        //Stop the Program
        System.exit(0);
    }//ActionPerformed

}//MenuHandler

public static void main(String[] args){

    //JLabel label = new JLabel("Hello");  // ?
    LightsGame game = new LightsGame();


}//main

 }//LightsGame

and I am using this LightsPanel class (that needs work too): 我正在使用这个LightsPanel类(也需要工作):

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

 public class LightsPanel extends JPanel{

//Background color
private final Color backClr = Color.LIGHT_GRAY,
//Outline color
outlineClr = Color.BLACK,
//Color of the light when it is on
onClr = Color.YELLOW,
//Color of the light when it is off
offClr = Color.GRAY;

private boolean switchedOn;    //  ?
    boolean[] array = new boolean[6];   

public LightsPanel(){
    super(); 
    setPreferredSize(new Dimension(800, 200));
    boolean[] switchedOn = new boolean[6];  //  ?
    //switchedOn = false;       

}//LightsPanel()

public void paintComponent(Graphics gc){
    super.paintComponent(gc);

    Graphics2D l = (Graphics2D)gc;

    //Draw the background
    setBackground(backClr);

    //Draw the light
    if (switchedOn)
        l.setColor(onClr);
    else
        l.setColor(offClr);
        l.fillOval(75, 75, 50, 50);

    //Draw the outline of the light 
    l.setColor(outlineClr);
    l.drawOval(75, 75, 50, 50);

}//paintComponent   

public void toggleLight() {

//Change the state of the light
if(switchedOn)
    switchedOn = false;
else
    switchedOn = true; 

//Redraw the panel
repaint();

}//toggleLight

public void allLightsOn() {

}//allLightsOn
 }//LightsPanel

Right now I am stuck trying to make a loop to display 6 lights and have them work with 6 buttons. 现在,我被困在试图使一个循环显示6个灯并使它们与6个按钮一起工作。 I cannot get the buttons to show up either. 我也无法显示按钮。 I need to add buttons to the buttonPanel but I'm not sure how to do that. 我需要将按钮添加到buttonPanel中,但是我不确定该怎么做。

My buttons should toggle and correspond to certain lights.. 我的按钮应该切换并对应于某些指示灯。

Button 0 : lights 0, 2
Button 1 : lights 1, 3
Button 2 : lights 0, 1, 2, 3, 5
Button 3 : lights 0, 1, 2, 4, 5
Button 4 : lights 2, 4
Button 5 : lights 3, 5

If anyone is willing or has time to help me with this it would be greatly appreciated. 如果有人愿意或有时间帮助我,将不胜感激。 Thank you! 谢谢!

Basically, you're not adding any buttons to anything... 基本上,您没有在任何按钮上添加任何按钮...

You have this great loop, which seems to make sense to build your logic here... 您有一个很好的循环,在这里建立逻辑似乎很有意义...

for (int k = 0; k <= 5; k++){
    switchButton[k] = new JButton("Light" + k );
}

For example... 例如...

for (int k = 0; k < switchButton.length; k++){
    switchButton[k] = new JButton("Light" + k );
    buttonPane.add(switchButton[k]);
}

Now the next time you need to attach some kind of handler to the button so you can change the light state accordingly... 现在,下次您需要在按钮上附加某种处理程序,以便可以相应地更改灯光状态...

For this, you will need to take a look at How to use buttons and How to write an action listener . 为此,您需要看一下如何使用按钮如何编写动作监听器

Personally, I would make the LightsPanel responsible for a single light and simply create as many instance of this panel as you need. 就我个人而言,我会让LightsPanel负责单个灯光,并根据需要简单地创建该面板的LightsPanel多个实例。 Then you could simply create a switch method which turns the light on or off as required... 然后您可以简单地创建一个switch方法,根据需要打开或关闭灯...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM