我必须编写一个GUI程序来玩灯光游戏。该游戏应显示6盏灯,包括一个菜单栏。

这是我到目前为止所走的距离:

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


我正在使用这个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


现在,我被困在试图使一个循环显示6个灯并使它们与6个按钮一起工作。我也无法显示任何按钮。我需要将按钮添加到buttonPanel中,但是我不确定该怎么做。

我的按钮应该切换并对应于某些指示灯。

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


如果有人愿意或有时间帮助我,将不胜感激。谢谢!

最佳答案

基本上,您没有在任何东西上添加任何按钮...

您有一个很好的循环,在这里建立逻辑似乎很有意义...

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


例如...

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


现在,下次您需要在按钮上附加某种处理程序,以便可以相应地更改灯光状态...

为此,您需要查看How to use buttonsHow to write an action listener

就个人而言,我会让LightsPanel负责单个光源,并根据需要简单地创建该面板的任意多个实例。然后,您可以简单地创建一个switch方法,根据需要打开或关闭灯光...

10-08 01:54