gpt4 book ai didi

java - 按钮和 Action 监听器的网格

转载 作者:行者123 更新时间:2023-12-01 18:01:58 24 4
gpt4 key购买 nike

我使用下面的代码创建一个由 (xSize = 10) x (ySize = 10) 个 JButton 组成的网格。只要我在该代码所在的类中进行更改,我就可以更改按钮。

但是我正在尝试从单独的类中更改按钮上的文本。

public class Ocean {

public Ocean(){
}

private final int xSize = 10;
private final int ySize = 10;
private final JButton[][] buttons = new JButton[xSize][ySize];

public gameBoard() {
for (int row = 0; row < xSize; row++) {
for (int col = 0; col < ySize; col++) {
buttons[row][col] = new JButton(/*space1*/);
buttons[row][col].setText("E");
buttons[row][col].addActionListener(new myActionListener());
buttons[row][col].setPreferredSize(new Dimension(50, 50));
playerPanel.add(buttons[row][col]);
}
}

}

下一个类

public class Battleship {

int length = 4;

public Battleship() {
}

public changeText(int row, int col) {
ocean.buttons[row][col].setText("H"); need to make this work
}

我希望这能以更好的方式提出这个问题

谢谢

最佳答案

归结起来,你的问题很简单:如何让一个对象改变另一个对象的状态?

有很多可能的方法来给这只猫剥皮,但最简单的就是提供一个类,这里​​是 Ocean(或者可能是 gameBoard?你的代码很困惑)外部 ActionListener 可以调用来改变其状态的公共(public)方法。例如,您可以为按钮网格持有类提供一个公共(public)方法:setText(...),如下所示:

public void setText(int row, int col, String text) {
buttons[row][col].setText(text);
}

可以通过构造函数参数向 ActionListener 传递对此网格持有类的引用,从而允许它在需要时调用此方法。

另请注意,您始终可以通过 ActionListener actionPerformed 方法的 ActionEvent 参数获取对按下的 JButton 的引用。它有一个 getSource() 方法,该方法返回对启动事件的对象(此处为 JButton)的引用。

如果您需要更详细的解决方案,请考虑创建并发布有效的 Minimal, Complete, and Verifiable example .

例如:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SimpleButtonGridMain extends JPanel {
private static void createAndShowGui() {
SimpleButtonGrid simpleButtonGrid = new SimpleButtonGrid();

MyButtonListener myButtonListener = new MyButtonListener();
simpleButtonGrid.addActionListener(myButtonListener);

JFrame frame = new JFrame("SimpleButtonGridMain");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(simpleButtonGrid);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

class SimpleButtonGrid extends JPanel {
private static final int ROWS = 10;
private static final int COLS = ROWS;
private JButton[][] buttons = new JButton[ROWS][COLS];

public SimpleButtonGrid() {
setLayout(new GridLayout(ROWS, COLS, 3, 3));
for (int row = 0; row < buttons.length; row++) {
for (int col = 0; col < buttons[row].length; col++) {
String text = String.format(" [ %d, %d ] ", col, row);
JButton button = new JButton(text);
add(button);
buttons[row][col] = button;
}
}
}

public void addActionListener(ActionListener listener) {
for (JButton[] jButtons : buttons) {
for (JButton jButton : jButtons) {
jButton.addActionListener(listener);
}
}
}
}

class MyButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
JButton sourceBtn = (JButton) e.getSource();

String message = "Button pressed: " + actionCommand;
JOptionPane.showMessageDialog(sourceBtn, message, "Button Pressed", JOptionPane.PLAIN_MESSAGE);
sourceBtn.setText("Pressed");
sourceBtn.setEnabled(false);
}
}

关于java - 按钮和 Action 监听器的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40251500/

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