[英]Do event on JButton Click
So I have created a swing GUI that is a board (n by n) that contains n^2 blocks(buttons).所以我创建了一个 swing GUI,它是一个包含 n^2 个块(按钮)的板(n x n)。 The breakdown of the code is like so: create GUI (Jframe and Jpanel) -> create Board (array of NxN blocks) -> create blocks(JButton).
代码分解如下:创建 GUI(Jframe 和 Jpanel)-> 创建 Board(NxN 块数组)-> 创建块(JButton)。 Once the board is created I generate and maze and its show each button with the proper border etc. My issue is that I want an action to be performed when a button is clicked.
创建板后,我会生成并迷宫,并显示带有正确边框的每个按钮等。我的问题是我希望在单击按钮时执行操作。 This is the GUI class
这是图形用户界面 class
EDIT: To clarify, my question is: How do I implement my block (Jbutton) to do an event on click.编辑:澄清一下,我的问题是:如何实现我的块(Jbutton)来执行点击事件。 Also, want to be able to obtain the Block object so that I can manipulate it.
另外,希望能够获得块 object 以便我可以操作它。
public class GUI {
private int width;
private int height;
private JFrame frame;
private Color color;
private JPanel panel;
private GridLayout layout;
private Board board;
static final Color PLAYER = Color.BLUE;
static final Color OPEN = Color.LIGHT_GRAY;
static final Color OCCUPIED = Color.ORANGE;
public GUI(int width, int height, Color color){
this.width = width;
this.height = height;
frame = new JFrame("2D MAP");
this.color = color;
panel = new JPanel();
layout = new GridLayout(width,height);
board = new Board(width,height);
board.populate();
}
This is the Board class这是板class
public class Board {
private Random rand = new Random();
private int width;
private int height;
private int x; // Random X coordinate to start
private int y; // Random Y coordinate to start
private Map<String, Integer> dx = Map.of("N", -1, "S", 1, "E", 0, "W", 0);
private Map<String, Integer> dy = Map.of("N", 0, "S", 0, "E", -1, "W", 1);
private Map<String, String> oppositeDir = Map.of("N", "S", "S", "N", "E", "W", "W", "E");
private String[] directions = new String[]{"N", "S", "E", "W"};
Block[][] board;
public Board(int width, int height){
this.width = width;
this.height = height;
this.x = rand.nextInt(this.height);
this.y = rand.nextInt(this.width);
board = new Block[width][height];
}
And finally this is the Block class aka the button最后这是块 class 又名按钮
public class Block{
private int topBorder = 1, leftBorder = 1, rightBorder = 1, botBorder = 1;
private int x;
private int y;
private JButton button;
private Color color;
private String type;
private Map<String, Block> neighbors;
public Block(int x, int y, JButton button, Color color, String type){
this.x = x;
this.y = y;
this.color = color;
this.button = new JButton();
this.type = type;
this.neighbors = new HashMap<String, Block>();
}
To add button actions to buttons you simply have to add ActionListener
to a button via addActionListener
method要将按钮操作添加到按钮,您只需通过
addActionListener
方法将ActionListener
添加到按钮
this.button.addActionListener(new ActionListener(){
void actionPerformed(ActionEvent e){
//handle button click
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.