gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:08:44 25 4
gpt4 key购买 nike

所以到目前为止,我已经创建了一个二维引用数组,并向每个引用添加了一个按钮(或者至少尝试过)。然后我为每个按钮添加了一个 Action 监听器。当按下按钮时,它应该查看按下了数组中的哪个按钮。如果是“命中”增量命中,请将按钮从“?”更改为“?”到“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 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();

关于Java更改按钮名称并递增计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27538904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com