Java 如何制作按钮列表给你所按下按钮的坐标
因此,基本上我正在尝试用GUI制作一个国际象棋/跳棋程序,棋盘由8x8个按钮组成,我需要当我按下I按钮时,按钮侦听器会告诉我按下了哪个按钮。 我还没有添加按钮列表,因为我希望你能找到更好的方法 注:跳棋板被称为“棋盘” 代码:Java 如何制作按钮列表给你所按下按钮的坐标,java,swing,Java,Swing,因此,基本上我正在尝试用GUI制作一个国际象棋/跳棋程序,棋盘由8x8个按钮组成,我需要当我按下I按钮时,按钮侦听器会告诉我按下了哪个按钮。 我还没有添加按钮列表,因为我希望你能找到更好的方法 注:跳棋板被称为“棋盘” 代码: import java.awt.*; 导入java.awt.image.buffereImage; 导入java.io.File; 导入java.io.IOException; 导入javax.imageio.imageio; 导入javax.swing.*; 导入jav
import java.awt.*;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
导入javax.swing.*;
导入javax.swing.border.*;
公共类BetterGui{
私有最终JPanel gui=新JPanel(新边界布局(3,3));
公共JButton[][]棋盘格=新JButton[8][8];
私人JPanel棋盘;
专用最终JLabel消息=新JLabel(
“阿莫斯是国王”);
私有静态最终字符串COLS=“ABCDEFGH”;
贝特古伊(董事会){
更新GUI(板);
}
公开最终作废更新GUI(董事会){
//设置主GUI
setboorder(新的EmptyBorder(5,5,5,5));
JToolBar工具=新的JToolBar();
工具。可设置浮动(错误);
添加(工具、边框布局、页面开始);
tools.add(newjbutton(“new”);//TODO-添加功能!
添加(新的JButton(“Save”);//TODO-添加功能!
添加(新的JButton(“还原”);//TODO-添加功能!
tools.addSeparator();
tools.add(新的JButton(“辞职”);//TODO-添加功能!
tools.addSeparator();
工具。添加(消息);
添加(新JLabel(“?”),BorderLayout.LINE_START);
棋盘=新的JPanel(新的网格布局(0,9));
棋盘。顺序(新的线边框(颜色。黑色));
添加(棋盘);
//创建棋盘方块
Insets buttonMargin=新的Insets(0,0,0,0);
对于(int ii=0;ii<棋盘格.长度;ii++){
对于(intjj=0;jj<棋盘格[ii]。长度;jj++){
JButton b=新JButton();
b、 毛缘(纽扣边缘);
//我们的棋子大小为64x64像素,因此我们将
//使用透明图标“填写此内容”。。
BuffereImage img=null;
试一试{
如果(ii%2==jj%2){
if(board.GameBoard[ii][jj].ContainingChecker.CheckerType==“红色”){
img=ImageIO.read(新文件(“C:\\Users\\Awesome\\Desktop\\large\u blue\u circle.png”);//最终C:\\ImageTest\\pic2.jpg
}else if(board.GameBoard[ii][jj].ContainingChecker.CheckerType==“黑色”){
img=ImageIO.read(新文件(“C:\\Users\\Awesome\\Desktop\\chcekrplayer.png”);//最终C:\\ImageTest\\pic2.jpg
}否则{
img=ImageIO.read(新文件(“C:\\Users\\Awesome\\Desktop\\clearImage.png”);//最终C:\\ImageTest\\pic2.jpg
}
}否则{
img=ImageIO.read(新文件(“C:\\Users\\Awesome\\Desktop\\clearImage.png”);//最终C:\\ImageTest\\pic2.jpg
}
}捕获(IOE异常){
e、 printStackTrace();
}
ImageIcon图标=新的ImageIcon(img);
b、 设置图标(图标);
如果((jj%2==1&&ii%2==1)
//) {
||(jj%2==0&&ii%2==0)){
b、 挫折地面(颜色:白色);
}否则{
b、 挫折背景(颜色:黑色);
}
棋盘格[jj][ii]=b;
}
}
//填满棋盘
棋盘。添加(新JLabel(“”);
//填写第一行
对于(int ii=0;ii<8;ii++){
棋盘(
新JLabel(COLS.substring(ii,ii+1),SwingConstants.CENTER);
}
//填写黑色非典当品行
对于(int ii=0;ii<8;ii++){
对于(intjj=0;jj<8;jj++){
开关(jj){
案例0:
添加(新的JLabel(“+(ii+1),SwingConstants.CENTER));
违约:
添加(棋盘格[jj][ii]);
}
}
}
}
公共最终无效初始值egui2(){
//设置主GUI
setboorder(新的EmptyBorder(5,5,5,5));
JToolBar工具=新的JToolBar();
工具。可设置浮动(错误);
添加(工具、边框布局、页面开始);
tools.add(newjbutton(“new”);//TODO-添加功能!
添加(新的JButton(“Save”);//TODO-添加功能!
添加(新的JButton(“还原”);//TODO-添加功能!
tools.addSeparator();
tools.add(新的JButton(“辞职”);//TODO-添加功能!
tools.addSeparator();
工具。添加(消息);
添加(新JLabel(“?”),BorderLayout.LINE_START);
棋盘=新的JPanel(新的网格布局(0,9));
棋盘。顺序(新的线边框(颜色。黑色));
添加(棋盘);
//创建棋盘方块
Insets buttonMargin=新的Insets(0,0,0,0);
对于(int ii=0;ii<棋盘格.长度;ii++){
对于(intjj=0;jj<棋盘格[ii]。长度;jj++){
JButton b=新JButton();
b、 毛缘(纽扣边缘);
//我们的棋子大小为64x64像素,因此我们将
//使用透明图标“填写此内容”。。
BuffereImage img=null;
试一试{
如果(ii%2==jj%2){
如果(ii<2){
img=ImageIO.read(新文件(“C:\\Users
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.*;
public class BetterGui {
private final JPanel gui = new JPanel(new BorderLayout(3, 3));
public JButton[][] chessBoardSquares = new JButton[8][8];
private JPanel chessBoard;
private final JLabel message = new JLabel(
"Amos is the King");
private static final String COLS = "ABCDEFGH";
BetterGui(Board board) {
updateGui(board);
}
public final void updateGui(Board board) {
// set up the main GUI
gui.setBorder(new EmptyBorder(5, 5, 5, 5));
JToolBar tools = new JToolBar();
tools.setFloatable(false);
gui.add(tools, BorderLayout.PAGE_START);
tools.add(new JButton("New")); // TODO - add functionality!
tools.add(new JButton("Save")); // TODO - add functionality!
tools.add(new JButton("Restore")); // TODO - add functionality!
tools.addSeparator();
tools.add(new JButton("Resign")); // TODO - add functionality!
tools.addSeparator();
tools.add(message);
gui.add(new JLabel("?"), BorderLayout.LINE_START);
chessBoard = new JPanel(new GridLayout(0, 9));
chessBoard.setBorder(new LineBorder(Color.BLACK));
gui.add(chessBoard);
// create the chess board squares
Insets buttonMargin = new Insets(0, 0, 0, 0);
for (int ii = 0; ii < chessBoardSquares.length; ii++) {
for (int jj = 0; jj < chessBoardSquares[ii].length; jj++) {
JButton b = new JButton();
b.setMargin(buttonMargin);
// our chess pieces are 64x64 px in size, so we'll
// 'fill this in' using a transparent icon..
BufferedImage img = null;
try {
if (ii % 2 == jj % 2) {
if (board.GameBoard[ii][jj].ContainingChecker.CheckerType == "red") {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\large_blue_circle.png")); // eventually C:\\ImageTest\\pic2.jpg
} else if (board.GameBoard[ii][jj].ContainingChecker.CheckerType == "black") {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\chcekrplayer.png")); // eventually C:\\ImageTest\\pic2.jpg
} else {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\clearImage.png")); // eventually C:\\ImageTest\\pic2.jpg
}
} else {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\clearImage.png")); // eventually C:\\ImageTest\\pic2.jpg
}
} catch (IOException e) {
e.printStackTrace();
}
ImageIcon icon = new ImageIcon(img);
b.setIcon(icon);
if ((jj % 2 == 1 && ii % 2 == 1)
//) {
|| (jj % 2 == 0 && ii % 2 == 0)) {
b.setBackground(Color.WHITE);
} else {
b.setBackground(Color.BLACK);
}
chessBoardSquares[jj][ii] = b;
}
}
//fill the chess board
chessBoard.add(new JLabel(""));
// fill the top row
for (int ii = 0; ii < 8; ii++) {
chessBoard.add(
new JLabel(COLS.substring(ii, ii + 1), SwingConstants.CENTER));
}
// fill the black non-pawn piece row
for (int ii = 0; ii < 8; ii++) {
for (int jj = 0; jj < 8; jj++) {
switch (jj) {
case 0:
chessBoard.add(new JLabel("" + (ii + 1), SwingConstants.CENTER));
default:
chessBoard.add(chessBoardSquares[jj][ii]);
}
}
}
}
public final void initializeGui2() {
// set up the main GUI
gui.setBorder(new EmptyBorder(5, 5, 5, 5));
JToolBar tools = new JToolBar();
tools.setFloatable(false);
gui.add(tools, BorderLayout.PAGE_START);
tools.add(new JButton("New")); // TODO - add functionality!
tools.add(new JButton("Save")); // TODO - add functionality!
tools.add(new JButton("Restore")); // TODO - add functionality!
tools.addSeparator();
tools.add(new JButton("Resign")); // TODO - add functionality!
tools.addSeparator();
tools.add(message);
gui.add(new JLabel("?"), BorderLayout.LINE_START);
chessBoard = new JPanel(new GridLayout(0, 9));
chessBoard.setBorder(new LineBorder(Color.BLACK));
gui.add(chessBoard);
// create the chess board squares
Insets buttonMargin = new Insets(0, 0, 0, 0);
for (int ii = 0; ii < chessBoardSquares.length; ii++) {
for (int jj = 0; jj < chessBoardSquares[ii].length; jj++) {
JButton b = new JButton();
b.setMargin(buttonMargin);
// our chess pieces are 64x64 px in size, so we'll
// 'fill this in' using a transparent icon..
BufferedImage img = null;
try {
if (ii % 2 == jj % 2) {
if (ii < 2) {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\large_blue_circle.png")); // eventually C:\\ImageTest\\pic2.jpg
} else if (ii > 4) {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\chcekrplayer.png")); // eventually C:\\ImageTest\\pic2.jpg
} else {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\clearImage.png")); // eventually C:\\ImageTest\\pic2.jpg
}
} else {
img = ImageIO.read(new File("C:\\Users\\Awesome\\Desktop\\clearImage.png")); // eventually C:\\ImageTest\\pic2.jpg
}
} catch (IOException e) {
e.printStackTrace();
}
ImageIcon icon = new ImageIcon(img);
b.setIcon(icon);
if ((jj % 2 == 1 && ii % 2 == 1)
//) {
|| (jj % 2 == 0 && ii % 2 == 0)) {
b.setBackground(Color.WHITE);
} else {
b.setBackground(Color.BLACK);
}
chessBoardSquares[jj][ii] = b;
}
}
//fill the chess board
chessBoard.add(new JLabel(""));
// fill the top row
for (int ii = 0; ii < 8; ii++) {
chessBoard.add(
new JLabel(COLS.substring(ii, ii + 1), SwingConstants.CENTER));
}
}
public final JComponent getChessBoard() {
return chessBoard;
}
public final JComponent getGui() {
return gui;
}
}
public class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
AbstractButton myBtn = (AbstractButton) e.getSource();
// call whatever methods needed on myBtn
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class MyGrid extends JPanel {
public static final String ROW = "row";
public static final String COL = "col";
private static final int SIDES = 8;
private static final int CELL_SZ = 60;
private static final Dimension CELL_DIMENSION = new Dimension(CELL_SZ, CELL_SZ);
private static final Color DARK = Color.GRAY;
private static final int LT = 250;
private static final Color LIGHT = new Color(LT, LT, LT);
private JLabel[][] cells = new JLabel[SIDES][SIDES];
private Icon whiteIcon;
private Icon redIcon;
public MyGrid() {
whiteIcon = createIcon(LIGHT);
redIcon = createIcon(Color.RED);
MyMouse myMouse = new MyMouse();
setLayout(new GridLayout(SIDES, SIDES));
for (int row = 0; row < cells.length; row++) {
for (int col = 0; col < cells[row].length; col++) {
JLabel cell = new JLabel();
cell.setOpaque(true);
cell.setPreferredSize(CELL_DIMENSION);
cell.putClientProperty(ROW, row);
cell.putClientProperty(COL, col);
cell.addMouseListener(myMouse);
Color bg = row % 2 == col % 2 ? LIGHT : DARK;
if (bg.equals(DARK) && row < 3) {
cell.setIcon(redIcon);
} else if (bg.equals(DARK) && row > 4) {
cell.setIcon(whiteIcon);
}
cell.setBackground(bg);
cells[row][col] = cell;
add(cell);
}
}
}
private Icon createIcon(Color color) {
BufferedImage img = new BufferedImage(CELL_SZ, CELL_SZ, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int gap = 4;
g2.setColor(color);
g2.fillOval(gap, gap, CELL_SZ - 2 * gap, CELL_SZ - 2 * gap);
g2.dispose();
return new ImageIcon(img);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
MyGrid mainPanel = new MyGrid();
JFrame frame = new JFrame("MyGrid");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
public class MyMouse extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
JComponent cell = (JComponent) e.getSource();
if (cell != null) {
Integer row = (Integer) cell.getClientProperty(MyGrid.ROW);
Integer col = (Integer) cell.getClientProperty(MyGrid.COL);
System.out.printf("[%d, %d]%n", row, col);
}
}
}