【问题标题】:How to assign or initiate an integer into JButton?如何将整数分配或初始化到 JButton?
【发布时间】:2015-05-02 01:59:30
【问题描述】:
package javaapplication3;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class BattleShip2 extends JFrame implements ActionListener {
    JButton[][] array2dtop = new JButton[10][10];
    BattleShip2(String title) { //Board Display
        super(title);//show Title
        setLayout(new FlowLayout()); // button posistion
         for(int x = 0; x < array2dtop.length; x++) {
            for(int y = 0; y < array2dtop.length; y++) {
                array2dtop[x][y] = new JButton(String.valueOf((x * 10) + y)); //display   
                array2dtop[x][y].setBounds(20,40,140,20);
                array2dtop[x][y].setActionCommand("x"); //itself value
                array2dtop[x][y].addActionListener(this);
                add(array2dtop[x][y]);
            }
        }

    }

    public void actionPerformed(ActionEvent evt) { //operation
        String cmd = evt.getActionCommand();

        if(cmd == "x") {
            System.out.println("x");
        }
    }

}

public class pratice3 {
    public static void main(String[] args) {
        BattleShip2 board = new BattleShip2("BattleShip");
        board.setSize(500, 400);
        board.setLocation(250, 250);
        board.setDefaultCloseOperation(board.EXIT_ON_CLOSE); //Press x to EXIT
        board.setVisible(true);
    }
}

您好,我正在创建一个战舰游戏,我的想法是当用户或 AI 按下按钮时,它可以显示船的大小 例如,有 3 艘船:ship1(size = 3)、ship2(size = 2)、ship3(size = 1),当用户/AI 击中 ship1,他们看到 3 时,他们会知道他们击中了 ship1

但我在 setActioncommand 上遇到问题

关于代码,

array2dtop[x][y].setActionCommand("x")

我想分配或声明一个整数(船的大小)到 array2dtop[x][y] 但我不知道,但分配一个字符串以及如何在 actionPerformed 中使用它

【问题讨论】:

  • 所以你有一个按钮矩阵,你希望一个 JButton 在你点击后神奇地变成一个整数?我敢肯定这是不对的——你能更详细地解释一下你要做什么吗?
  • 所以在actionperformed函数中,字符串cmd是在比较字符串x,所以可以用整数而不是字符串进行比较吗?

标签: java arrays swing jframe jbutton


【解决方案1】:

我想在array2dtop[x][y]中分配或声明一个整数(船的大小)

您可以使用Integer.parseInt 将字符串解析为整数:

class BattleShip2 extends JFrame implements ActionListener {

    JButton[][] array2dtop = new JButton[10][10];

    BattleShip2(String title) {

        super(title);
        setLayout(new GridLayout(10, 10, 5, 5));
        for (int x = 0; x < array2dtop.length; x++) {
            for (int y = 0; y < array2dtop.length; y++) {
                array2dtop[x][y] = new JButton(String.valueOf((x * 10) + y));
                array2dtop[x][y].setActionCommand(String.valueOf(3));
                array2dtop[x][y].addActionListener(this);
                // Force buttons to be square.
                Dimension dim = array2dtop[x][y].getPreferredSize();
                int buttonSize = Math.max(dim.height, dim.width);
                array2dtop[x][y].setPreferredSize(new Dimension(buttonSize, buttonSize));

                add(array2dtop[x][y]);
            }
        }
    }

    public void actionPerformed(ActionEvent evt) {

        int cmd = Integer.parseInt(evt.getActionCommand());
        if (cmd == 3) {
            System.out.println(3);
        }
    }
}

public class pratice3 {

    public static void main(String[] args) {

        BattleShip2 board = new BattleShip2("BattleShip");
        board.pack();
        board.setLocationRelativeTo(null);
        board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        board.setVisible(true);
    }
}

注意事项:

  • 我将布局更改为GridLayout,其垂直和水平间距与FlowLayout 的默认设置相同。它将确保所有按钮的大小相同。
  • 不要使用setBounds,让布局管理器来处理。
  • 我添加了 3 行使按钮呈方形的,您可以将其移除。
  • 不要将setSize 用于JFrame,而是使用pack
  • 使用board.setLocationRelativeTo(null)(在调用pack 之后)设置位置通常是一个更好的主意,因为您不知道屏幕的大小。
  • board.EXIT_ON_CLOSE 应该以静态方式访问:JFrame.EXIT_ON_CLOSE(例如)。
  • 在调用Integer.parseInt 时,您可能想要捕捉NumberFormatException

话虽如此,还有其他方法可以实现这一点,例如在按钮上使用setClientProperty,或者只是将数字传递给ActionListener 的构造函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-10
    • 2020-10-03
    • 2018-11-26
    • 2011-12-28
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2020-05-04
    相关资源
    最近更新 更多