【发布时间】:2023-04-04 23:17:01
【问题描述】:
所以到目前为止,我已经创建了一个二维引用数组,并为每个引用添加了一个按钮(或者至少尝试过)。然后我为每个按钮添加了一个动作监听器。当一个按钮被按下时,它应该查看数组中的哪个按钮被按下。如果它是“命中”增量命中并从“?”更改按钮到“X”。如果是“未命中”增量未命中并从“?”更改按钮到 ” ”。它仍然没有改变按钮,也没有增加计数。我做错了什么?
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;
}
}
}
}
【问题讨论】:
-
而不是每次单击按钮时创建一个新的
JPanel和文本字段,为什么不维护对文本字段的引用,作为实例字段,并简单地更新那里的内容(通过setText)? -
我也看不到您更新按钮状态的任何地方。确定按钮在数组中的位置(行/列)并简单地执行单个计算而不是循环这么多也会更容易,但这只是我......
-
我不知道你是如何进行计算以获得按钮的位置来比较命中和未命中的......
-
我可能会创建一个二维数组,其中包含 'int
s.0' 为空,非0值代表您可能喜欢的每种船舶类型...然后您可以使用循环查找按钮的索引并循环“ship”数组中的位置,或者您可以通过Map或直接通过get/setClientProperty将Point对象与按钮相关联>
标签: java swing user-interface jbutton increment