Java更改按钮的名称并递增计数

时间:2014-12-18 03:44:27

标签: java swing user-interface jbutton increment

到目前为止,我已经创建了一个2D引用数组,并为每个引用添加了一个按钮(或者至少尝试过)。然后我在一个动作监听器中添加了每个按钮。按下按钮时,应该查看按下数组中的按钮。如果它是"击中"增加命中并更改" ?"到" X&#34 ;.如果它是" miss"增加未命中并更改" ?"到" &#34 ;.它仍然没有改变按钮,也没有递增计数。我做错了什么?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class Battleship extends JFrame{
    public int k;
    public int l;
    public int z;
     // Initialize counters.
    public int shipstartx = (int) (Math.random() * 10);
    public int shipstarty = (int) (Math.random() * 10);
    public int possitioning = (int) (Math.random() * 10);
    // Make hit and miss buttons.  Also make the normal button
    public Battleship(){
         ButtonListener listener1 = new ButtonListener();
         JButton Original[][] = new JButton[10][10];
        // Make the 10 by 10 grid.
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(10, 10));
        for (int i = 0; i <10; i++){
        for (int j = 0; j <10; j++){
            Original[i][j] = new JButton(" ?");
            p1.add(Original[i][j]);
            Original[i][j].addActionListener(listener1);
        }
        }
        // Add everything to a field so that the player can see it.
        add(new JTextField("Try to sink the battleship!"), BorderLayout.NORTH);
        add(p1, BorderLayout.CENTER);
        JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout(3,1));
        p2.add(new JTextField("Hits " + (k)));
        p2.add(new JTextField("Misses " + l));
        p2.add(new JTextField("Sinks " + z));
        add(p2, BorderLayout.WEST);
        // Now to make it so that the buttons actually do something.
    }
 public static void main (String[] args){
     Battleship frame = new Battleship();
     frame.setTitle("Battleship");
     frame.setSize(400, 300);
     frame.setLocationRelativeTo(null);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
 }
class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        boolean hit = false;
        for (int a = 0; a < 10; a++){
        for (int b = 0; b < 10; b++){
          if (possitioning % 2 == 0){
           if (shipstartx+5 <= 10){
            if ((a == shipstartx || a == shipstartx+1 || a == shipstartx+2
                || a == shipstartx + 3 || a == shipstartx+4 || a == shipstartx+5)
                && b == shipstarty)
                hit = true;
            }
           else if ((a == shipstartx ||  a == shipstartx-1 || a == shipstartx-2
                   || a == shipstartx-3 || a == shipstartx-4 || a == shipstartx-5)
                   && b == shipstarty)
               hit = true;
          }
          else if (shipstarty+5 <= 10){
              if ((b == shipstarty || b == shipstarty+1 || b == shipstarty+2
                 || b == shipstarty+3 || b == shipstarty+4 || b == shipstarty+5)
                 && a == shipstartx)
               hit = true;
          }
          else if ((b == shipstarty || b == shipstarty-1 || b == shipstarty-2
                     || b == shipstarty-3 || b == shipstarty-4 || b == shipstarty-5)
                     && a == shipstartx)
              hit = true;
          else
              hit = false;
        }
        }
   if (hit == true){
       k=k+1;
    }
   else {
       l=l+1;
   }
   JPanel p2 = new JPanel();
   p2.setLayout(new GridLayout(3,1));
   p2.add(new JTextField("Hits " + (k)));
   p2.add(new JTextField("Misses " + l));
   p2.add(new JTextField("Sinks " + z));
   add(p2, BorderLayout.WEST);
if (k == 3){
  z++;
  k = 0;
  l = 0;
 }
}
}
}

1 个答案:

答案 0 :(得分:2)

而不是......

JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(3,1));
p2.add(new JTextField("Hits " + (k)));
p2.add(new JTextField("Misses " + l));
p2.add(new JTextField("Sinks " + z));

每次单击按钮时,都会创建三个JTextField类型的实例字段,然后使用这些字段,

private JTextField hitsField;
private JTextField missiesField;
private JTextField sinksField;

然后在你的构造函数中......

JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(3, 1));

hitsField = new JTextField("Hits: 0", 10);
missiesField = new JTextField("Misses: 0", 10);
sinksField = new JTextField("Sinks: 0", 10);

p2.add(hitsField);
p2.add(missiesField);
p2.add(sinksField);

在你的ActionListener中使用类似......

的内容
hitsField.setText("Hits " + (k));
missiesField.setText("Misses " + (l));
sinksField.setText("Sinks " + (z));

您还可以找到从ActionEvent ...

点击的按钮
JButton btn = (JButton)e.getSource();