我必须编写一个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
如果有人愿意或有时间帮助我,我们将不胜感激。谢谢!
答案 0 :(得分:2)
基本上,你没有添加任何按钮......
你有这个伟大的循环,这似乎有意义在这里建立你的逻辑...
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 buttons和How to write an action listener。
就个人而言,我会让LightsPanel
负责一个灯光,只需根据需要创建这个面板的多个实例。然后你可以简单地创建一个switch
方法,根据需要打开或关闭灯......