Java 编程:setActionCommand() 错误
Java programming : setActionCommand() error
我正在开发一款类似俄罗斯方块的多人游戏,其中包含一个 RMI。我有一个界面 (BorderLayout),带有 JButtons(板)网格,工作正常,侧面有几个面板,底部有一个面板,上面有 3 个 JButtons,代表 3 个俄罗斯方块。
当我 运行 客户端时,问题就来了:当我启动客户端时,我有一个方法可以获取 3 件的池(并获取每件的详细信息),因此我可以获得名称、颜色等. 定义当我点击这些 JButtons 时的正确操作。
public void setBoutons(Piece[] bloc)
{
this.piece1 = bloc[0];
this.piece2 = bloc[1];
this.piece3 = bloc[2];
this.nompiece1 = piece1.getNom();
this.nompiece2 = piece2.getNom();
this.nompiece3 = piece3.getNom();
//System.out.println(nompiece1);
this.bouton1.setActionCommand(this.nompiece1);
this.bouton2.setActionCommand(this.nompiece2);
this.bouton3.setActionCommand(this.nompiece3);
this.couleur1 = piece1.getCouleur();
this.couleur2 = piece2.getCouleur();
this.couleur3 = piece3.getCouleur();
this.disposition1 = piece1.getDisposition();
this.disposition2 = piece2.getDisposition();
this.disposition3 = piece3.getDisposition();
}
结果是我无法设置 ActionCommand。我尝试了很多方法,甚至只是使用像 "hello" 这样的简单字符串,但我一直遇到这个错误:
Client exception: java.lang.NullPointerException
java.lang.NullPointerException
at Fenetre.setBoutons(Fenetre.java:174)
at Fenetre.<init>(Fenetre.java:60)
at Client.main(Client.java:22)
我把整个代码放在下面,除了 actionPerformed 部分(需要很多 space,如果需要请问我):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Fenetre extends JFrame implements ActionListener{
private JButton[][] cases;
private JButton bouton1;
private JButton bouton2;
private JButton bouton3;
private String nompiece1;
private String nompiece2;
private String nompiece3;
private Color couleur1;
private Color couleur2;
private Color couleur3;
private byte[][] disposition1;
private byte[][] disposition2;
private byte[][] disposition3;
private Piece piece1;
private Piece piece2;
private Piece piece3;
private byte p1;
private byte p2;
private byte p3;
private Interface ninja;
private JPanel grille;
private JPanel zonepieces;
private JPanel zonedroite;
private JPanel zonehaut;
private JLabel pseudo;
private JLabel score;
public Fenetre(Interface uneinterface)
{
this.ninja = uneinterface;
try
{
Piece[] unpool = ninja.PoolPieces();
this.setBoutons(unpool);
}
catch (RemoteException e)
{
e.printStackTrace();
}
this.p1 = 0;
this.p2 = 0;
this.p3 = 0;
this.cases = new JButton[10][10];
this.grille = new JPanel();
this.zonepieces = new JPanel();
this.zonedroite = new JPanel();
this.zonehaut = new JPanel();
this.grille.setLayout(new GridLayout(10, 10, 2, 2));
for(int j = 0; j<10; j++)
{
for(int i = 0; i<10; i++)
{
this.cases[j][i] = new JButton(" ");
this.cases[j][i].addActionListener(this);
this.cases[j][i].setActionCommand(""+j+i);
this.cases[j][i].setBackground(Color.gray);
this.grille.add(cases[j][i]);
}
}
this.bouton1 = new JButton();
this.bouton1.addActionListener(this);
this.bouton1.setBackground(Color.WHITE);
this.bouton2 = new JButton();
this.bouton2.addActionListener(this);
this.bouton2.setBackground(Color.WHITE);
this.bouton3 = new JButton();
this.bouton3.addActionListener(this);
this.bouton3.setBackground(Color.WHITE);
this.zonepieces.setLayout(new FlowLayout());
this.zonepieces.add(bouton1);
this.zonepieces.add(bouton2);
this.zonepieces.add(bouton3);
this.pseudo = new JLabel("Pseudo");
this.zonehaut.add(pseudo);
this.score = new JLabel("Score");
this.zonedroite.add(score);
add(grille, BorderLayout.CENTER);
add(zonepieces, BorderLayout.SOUTH);
add(zonedroite, BorderLayout.EAST);
add(zonehaut, BorderLayout.NORTH);
this.setTitle("1010");
this.setSize(700, 700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void setBoutons(Piece[] bloc)
{
this.piece1 = bloc[0];
this.piece2 = bloc[1];
this.piece3 = bloc[2];
this.nompiece1 = piece1.getNom();
this.nompiece2 = piece2.getNom();
this.nompiece3 = piece3.getNom();
//System.out.println(nompiece1);
this.bouton1.setActionCommand(this.nompiece1);
this.bouton2.setActionCommand(this.nompiece2);
this.bouton3.setActionCommand(this.nompiece3);
this.couleur1 = piece1.getCouleur();
this.couleur2 = piece2.getCouleur();
this.couleur3 = piece3.getCouleur();
this.disposition1 = piece1.getDisposition();
this.disposition2 = piece2.getDisposition();
this.disposition3 = piece3.getDisposition();
}
提前致谢:)
在此构造函数中:
public Fenetre(Interface uneinterface)
{
this.ninja = uneinterface;
try
{
Piece[] unpool = ninja.PoolPieces();
this.setBoutons(unpool);
}
catch (RemoteException e)
{
e.printStackTrace();
}
您正在调用 setBoutons
方法,该方法又访问 bouton1
字段。但是该字段仅在稍后的构造函数中初始化:
this.bouton1 = new JButton();
这意味着它在您调用 setBoutons
时为空。更改您的构造函数以首先初始化按钮,您应该没问题。
在Java中,所有具有引用类型(非基元)的字段在初始化之前都是空的。
我正在开发一款类似俄罗斯方块的多人游戏,其中包含一个 RMI。我有一个界面 (BorderLayout),带有 JButtons(板)网格,工作正常,侧面有几个面板,底部有一个面板,上面有 3 个 JButtons,代表 3 个俄罗斯方块。 当我 运行 客户端时,问题就来了:当我启动客户端时,我有一个方法可以获取 3 件的池(并获取每件的详细信息),因此我可以获得名称、颜色等. 定义当我点击这些 JButtons 时的正确操作。
public void setBoutons(Piece[] bloc)
{
this.piece1 = bloc[0];
this.piece2 = bloc[1];
this.piece3 = bloc[2];
this.nompiece1 = piece1.getNom();
this.nompiece2 = piece2.getNom();
this.nompiece3 = piece3.getNom();
//System.out.println(nompiece1);
this.bouton1.setActionCommand(this.nompiece1);
this.bouton2.setActionCommand(this.nompiece2);
this.bouton3.setActionCommand(this.nompiece3);
this.couleur1 = piece1.getCouleur();
this.couleur2 = piece2.getCouleur();
this.couleur3 = piece3.getCouleur();
this.disposition1 = piece1.getDisposition();
this.disposition2 = piece2.getDisposition();
this.disposition3 = piece3.getDisposition();
}
结果是我无法设置 ActionCommand。我尝试了很多方法,甚至只是使用像 "hello" 这样的简单字符串,但我一直遇到这个错误:
Client exception: java.lang.NullPointerException
java.lang.NullPointerException
at Fenetre.setBoutons(Fenetre.java:174)
at Fenetre.<init>(Fenetre.java:60)
at Client.main(Client.java:22)
我把整个代码放在下面,除了 actionPerformed 部分(需要很多 space,如果需要请问我):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Fenetre extends JFrame implements ActionListener{
private JButton[][] cases;
private JButton bouton1;
private JButton bouton2;
private JButton bouton3;
private String nompiece1;
private String nompiece2;
private String nompiece3;
private Color couleur1;
private Color couleur2;
private Color couleur3;
private byte[][] disposition1;
private byte[][] disposition2;
private byte[][] disposition3;
private Piece piece1;
private Piece piece2;
private Piece piece3;
private byte p1;
private byte p2;
private byte p3;
private Interface ninja;
private JPanel grille;
private JPanel zonepieces;
private JPanel zonedroite;
private JPanel zonehaut;
private JLabel pseudo;
private JLabel score;
public Fenetre(Interface uneinterface)
{
this.ninja = uneinterface;
try
{
Piece[] unpool = ninja.PoolPieces();
this.setBoutons(unpool);
}
catch (RemoteException e)
{
e.printStackTrace();
}
this.p1 = 0;
this.p2 = 0;
this.p3 = 0;
this.cases = new JButton[10][10];
this.grille = new JPanel();
this.zonepieces = new JPanel();
this.zonedroite = new JPanel();
this.zonehaut = new JPanel();
this.grille.setLayout(new GridLayout(10, 10, 2, 2));
for(int j = 0; j<10; j++)
{
for(int i = 0; i<10; i++)
{
this.cases[j][i] = new JButton(" ");
this.cases[j][i].addActionListener(this);
this.cases[j][i].setActionCommand(""+j+i);
this.cases[j][i].setBackground(Color.gray);
this.grille.add(cases[j][i]);
}
}
this.bouton1 = new JButton();
this.bouton1.addActionListener(this);
this.bouton1.setBackground(Color.WHITE);
this.bouton2 = new JButton();
this.bouton2.addActionListener(this);
this.bouton2.setBackground(Color.WHITE);
this.bouton3 = new JButton();
this.bouton3.addActionListener(this);
this.bouton3.setBackground(Color.WHITE);
this.zonepieces.setLayout(new FlowLayout());
this.zonepieces.add(bouton1);
this.zonepieces.add(bouton2);
this.zonepieces.add(bouton3);
this.pseudo = new JLabel("Pseudo");
this.zonehaut.add(pseudo);
this.score = new JLabel("Score");
this.zonedroite.add(score);
add(grille, BorderLayout.CENTER);
add(zonepieces, BorderLayout.SOUTH);
add(zonedroite, BorderLayout.EAST);
add(zonehaut, BorderLayout.NORTH);
this.setTitle("1010");
this.setSize(700, 700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void setBoutons(Piece[] bloc)
{
this.piece1 = bloc[0];
this.piece2 = bloc[1];
this.piece3 = bloc[2];
this.nompiece1 = piece1.getNom();
this.nompiece2 = piece2.getNom();
this.nompiece3 = piece3.getNom();
//System.out.println(nompiece1);
this.bouton1.setActionCommand(this.nompiece1);
this.bouton2.setActionCommand(this.nompiece2);
this.bouton3.setActionCommand(this.nompiece3);
this.couleur1 = piece1.getCouleur();
this.couleur2 = piece2.getCouleur();
this.couleur3 = piece3.getCouleur();
this.disposition1 = piece1.getDisposition();
this.disposition2 = piece2.getDisposition();
this.disposition3 = piece3.getDisposition();
}
提前致谢:)
在此构造函数中:
public Fenetre(Interface uneinterface)
{
this.ninja = uneinterface;
try
{
Piece[] unpool = ninja.PoolPieces();
this.setBoutons(unpool);
}
catch (RemoteException e)
{
e.printStackTrace();
}
您正在调用 setBoutons
方法,该方法又访问 bouton1
字段。但是该字段仅在稍后的构造函数中初始化:
this.bouton1 = new JButton();
这意味着它在您调用 setBoutons
时为空。更改您的构造函数以首先初始化按钮,您应该没问题。
在Java中,所有具有引用类型(非基元)的字段在初始化之前都是空的。