我正在做一个使用JAVA制作棋盘游戏的项目。现在我想要一个玩家获胜,分数将显示在Jtextfield中。为了做到这一点,我首先使用一个方法从GamePlan类中的Declarewin方法()中的Player1Graphics类中获取JTextfield。但是这个向我展示了一个NullPointerException。
所以我想到了另一种方式。因为我不能将GameGui类的对象赋予任何其他类,因为它会创建无限循环。所以我使用了另一个名为Player类的类,并创建了一个变量" SName"这将取得"得分"在decarewin()方法中的变量。然后我在Player1Graphics类中使用了updatetextfied()方法,该类是GameGui类的内部类,并设置" SName"现在,所有变量运行良好,它们正在按预期传递值,JTextfield文本值也在更新,但JTextfield的视觉效果没有更新。它没有从0更改为1或1到2。
我正在给下面的代码.............
这是来自GameGui Class ........
的代码public class GameGui extends JFrame{
GameGui(){
super("Board");
this.setVisible(true);
this.setSize(700,800);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(main_panel);
main_panel.setLayout(new BorderLayout());
main_panel.setBackground(Color.WHITE);
resourcer();
}
resourcer(){
//players
JPanel playersroom = new JPanel();
//playersroom.setBorder(BorderFactory.createLineBorder(Color.black, 1));
playersroom.setBackground(Color.white);
playersroom.setLayout(new BorderLayout());
JPanel player1room = new JPanel();
player1room.setLayout(new BorderLayout());
//player1room.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.BLACK));
player1room.setPreferredSize(new Dimension(350,300));
player1room.add(new Player1graphics(),BorderLayout.CENTER);
playersroom.add(player1room,BorderLayout.WEST);
arena.add(playersroom,BorderLayout.CENTER);
main_panel.add(arena,BorderLayout.SOUTH);
}
这是拥有JTextfield的player1Graphics类,它是GameGui类的内部类。
public class Player1graphics extends JPanel{
private String newname;
JLabel name = new JLabel("PLAYER 1",SwingConstants.CENTER);
JButton setname = new JButton("CN");
GamePlan g = new GamePlan();
JButton token = new JButton("POISON");
JTextField showscore = new JTextField(20); //this is the JTextfield
Player1graphics(){
//repaint();
this.setLayout(null);
this.setBackground(new Color(255,220,154));
//setname code & name code
//JButton setname = new JButton("CN");
setname.setBounds(160, 40, 30, 20);
setname.setBorder( BorderFactory.createLineBorder(Color.black, 1, true));
setname.setToolTipText("Change the name");
name.setBounds(132, 70, 80, 30);
name.setOpaque(true);
name.setBackground(new Color(102,140,185));
name.setForeground(new Color(202,208,215));
name.setBorder(BorderFactory.createLineBorder(Color.black, 1));
setname.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newname = JOptionPane.showInputDialog(null,"ENTER YOUR NAME");
if (newname==null){
name.setText("PLAYER 1");
}else{
t1=1;
name.setText(newname);
getfirstplayername(newname);
}
}
});
//token button code
//JButton token = new JButton("POISON");
token.setBackground(new Color(233,249,254));
token.setBounds(132, 110, 80, 30);
token.setToolTipText("token can be used only one time");
ActionClass ac = new ActionClass();
ac.getfirstplayerpoisonbutton(token);
token.addActionListener(ac);
//won and showin textfieldcode
JLabel won = new JLabel("Won",SwingConstants.CENTER);
won.setBorder( BorderFactory.createLineBorder(new Color(102,140,185), 2, true));
won.setBounds(132, 150, 50, 30);
won.setBackground(new Color(102,140,185));
won.setForeground(Color.WHITE);
//JTextfield codes
//JTextField showwin = new JTextField();
//GamePlan gm = new GamePlan();
Player player = new Player();
showscore.setBounds(185, 150, 30, 30);
showscore.setEditable(false);
//showwin.setOpaque(true);
//showwin.setBackground(Color.WHITE);
//showwin.setText(player.sname);
updatetextfield(player.sname);
this.add(name);
this.add(setname);
this.add(token);
this.add(won);
this.add(showscore);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D myg = (Graphics2D) g;
myg.setStroke(new BasicStroke(1));
myg.setColor(new Color(163,193,231));
myg.fillOval(25, 10, 300, 200);
}
public void updatetextfield(String s){
showscore.setText(s);
showscore.repaint();
//JOptionPane.showMessageDialog(null, showwin.getText());
}
}
这是JTextfield代码.................
JTextField showscore = new JTextField(20);
Player player = new Player();
showscore.setBounds(185, 150, 30, 30);
showscore.setEditable(false);
updatetextfield(player.sname);
这是updatetextfield方法代码..........
public void updatetextfield(String s){
showscore.setText(s);
showscore.repaint();
//JOptionPane.showMessageDialog(null, showscore.getText());
}
这是我在播放器类中创建的变量..........
public class Player{
String sname = Integer.toString(GamePlan.score);
}
这是一个declarewin方法,其中得分变量更新,它位于另一个名为GamePlan Class的类中。
public class GamePlan{
static int score=0;
public void declarewin(){
score++; //score variable updating
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
bbutton[i][j].setEnabled(false);
}
}
JOptionPane.showMessageDialog(null, score);
Player pl = new Player();
JOptionPane.showMessageDialog(null, pl.sname);
JOptionPane.showMessageDialog(null, "YOU have won the game");
}
我已经使用了很多JOption来查看变量是否正在传递值,而且我还使用了gettext()方法来查看JTextfield的值是否正在发生变化。是的,所有值都在变化但是只有Textfield的视觉效果不会改变。虽然在JTextfield内部文本值为1或2,但它在GUI中显示为0.IT永远不会改变................
我花了几个小时来解决这个问题,但我无法解决这个问题。这只会让我发疯。
有人请帮忙让我摆脱这个烂摊子.............:| :|