我对摇摆非常不熟悉,但在这里我正在尝试制作一个“clacker”游戏,每次掷两个骰子时,要么单击两个(如果它们是相同的数字,则单击 1)按钮代表两个数字或者只是一个代表两个数字之和的按钮。如果单击按钮,则可见设置为 false。我有一个 JButton 数组来保存 12 个按钮,但每次引用数组按钮时,它都会抛出 NullPointerException。我尝试将创建 JButton 数组的代码放在构造函数和 actionPerformed 中,但它仍然抛出相同的异常。
这是代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Clacker implements ActionListener
{
JPanel panel;
JFrame frame;
JLabel dieFace1, dieFace2, numTurns;
JButton[] buttons;
JButton rollDie, reset;
String turn="Turns: ";
int turns, numCovered;
public Clacker()
{
frame = new JFrame("clacker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel=new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(50,30,50,30));
panel.setBackground(Color.gray);
panel.setLayout(new GridLayout(5,10,1,1));
dieFace1=new JLabel(new ImageIcon("die3.jpg"));
dieFace1.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
dieFace1.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
panel.add(dieFace1);
dieFace2=new JLabel(new ImageIcon("die3.jpg"));
dieFace2.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
dieFace2.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
panel.add(dieFace2);
rollDie=new JButton("Roll Die");
rollDie.setAlignmentX(JButton.RIGHT_ALIGNMENT);
rollDie.addActionListener(this);
panel.add(rollDie);
numTurns=new JLabel(turn+turns);
numTurns.setAlignmentX(JLabel.LEFT_ALIGNMENT);
numTurns.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
panel.add(numTurns);
setArray();
reset=new JButton("Reset");
reset.setAlignmentX(JButton.LEFT_ALIGNMENT);
reset.addActionListener(this);
panel.add(reset);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public void setArray()
{
JButton[] buttons = new JButton[12];
for (int i=0; i<12; i++)
{
buttons[i]= new JButton(i+1+"");
buttons[i].setActionCommand(i+"");
buttons[i].addActionListener(this);
panel.add(buttons[i]);
buttons[i].setEnabled(false);
System.out.println(buttons[i]);
}
}
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("Roll Die"))
{
Random asdf = new Random();
int newRoll= asdf.nextInt(6)+1;
dieFace1.setIcon(new ImageIcon("die"+newRoll+".jpg"));
Random asdf2 = new Random();
int newRoll2= asdf2.nextInt(6)+1;
dieFace2.setIcon(new ImageIcon("die"+newRoll2+".jpg"));
int sum=newRoll+newRoll2;
turns++;
numTurns.setText(turn+turns);
for (int i=0; i<7; i++) ////here
{
if (i==newRoll)
buttons[i].setEnabled(true);
if (i==newRoll2)
buttons[i].setEnabled(true);
}
for (int i=7; i<13; i++)
{
if (sum==i)
buttons[i].setEnabled(true);
}
String eventName = event.getActionCommand();
for (int b=1; b<13; b++)
{
if (eventName.equals(buttons[b].getActionCommand())) ////here
{
if (Integer.parseInt(eventName)>6)
{
buttons[b].setText("");
numCovered++;
buttons[b].setVisible(false);
for (int i=0; i<7; i++)
buttons[i].setEnabled(false);
}
}
}
for (int i=0; i<buttons.length; i++) ////here
buttons[i].setEnabled(false);
if (numCovered==12)
{
rollDie.setEnabled(false);
numTurns.setText("Win! "+turns+" turns");
}
}
else if (event.getActionCommand().equals("Reset"))
{
turns=0;
numTurns.setText(turn+turns);
rollDie.setEnabled(true);
numCovered=0;
for (int i=0; i<buttons.length; i++) ////here
{
buttons[i].setVisible(true);
buttons[i].setEnabled(false);
}
}
}
private static void runGUI()
{ JFrame.setDefaultLookAndFeelDecorated(true); Clacker game = new Clacker();}
public static void main(String[] args)
{ javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() { runGUI();}});}
}