簡體   English   中英

使用Swing的游戲場

[英]Game field using Swing

我的任務有問題。 問題是我完全不知道該怎么做。 我必須像在圖片上一樣制作一個字段窗口。 字段數量(X x Y)應由用戶設置。 每個字段都應能夠單擊(它們應該像按鈕一樣工作),並且單擊后用戶應具有一些選擇(例如,更改字段的顏色,其名稱等)。

任何建議都應使用Swing的哪些組件,以及任何教程? 到目前為止,我什么都沒找到。

在此處輸入圖片說明

我不再使用Swing。 我強烈建議改用JavaFX。 但是我做了類似的事情(為我的兒子制作了一個Othello游戲),然后我只使用了JTable。 您可以將幾乎任何對象都設置為表格單元對象,但需要一些擺弄,例如特定顏色的圖像,並且它帶有用於單擊單元格的偵聽器。

相同的解決方案適用於JavaFX,它使用表格來顯示可點擊的圖像。

我在Swing中的Table類的示例:

package spelmeny.table;

import java.awt.Component;
import java.util.Enumeration;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import spelmeny.logik.GameModel;
import spelmeny.style.GameStyle;

/**
*
* @author stefan santesson
*/
public class OthelloTable {

JTable table;
GameModel gameModel;
DefaultTableModel tm;
String[] heading = new String[]{"1", "2", "3", "4", "5", "6", "7", "8"};
GameStyle style;

public OthelloTable(JTable table, GameModel gameModel, GameStyle style) {
    this.table = table;
    table.setRowHeight(GameStyle.CELL_SIZE);
    this.gameModel = gameModel;
    this.style = style;
}

public void setStyle(GameStyle style) {
    this.style = style;
}

public void updateTable() {
    int colCount = table.getColumnModel().getColumnCount();
    Object[][] tableVal = new Object[colCount][8];
    int[][] board = gameModel.getBoardData();
    for (int row = 0; row < 8; row++) {
        for (int col = 0; col < 8; col++) {
            tableVal[row][col] = board[row][col];
        }
    }
    for (int row = 0; row < 8; row++) {
        for (int col = 0; col < colCount; col++) {
            tm.setValueAt(tableVal[row][col], row, col);
        }
    }
}

public void setTable() {
    tm = new MyTableModel(heading);
    IconRenderer icnRenderer = new IconRenderer();
    int[][] board = gameModel.getBoardData();

    for (int row = 0; row < 8; row++) {
        Object[] rowData = new Object[8];
        for (int col = 0; col < 8; col++) {
            rowData[col] = board[row][col];
        }
        tm.addRow(rowData);
    }

    table.setModel(tm);
    Enumeration<TableColumn> columns = table.getColumnModel().getColumns();
    for (int colIndex = 0; colIndex < 8; colIndex++) {
        TableColumn cbCol = table.getColumnModel().getColumn(colIndex);
        cbCol.setCellRenderer(icnRenderer);
        columns.nextElement().setPreferredWidth(GameStyle.CELL_SIZE);
    }
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
}

class MyTableModel extends DefaultTableModel {

    public MyTableModel(Object[] os) {
        super();
        for (Object o : os) {
            addColumn(o);
        }
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        switch (columnIndex) {
            default:
                return Integer.class;
        }
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        return false;
    }
}

class IconRenderer extends DefaultTableCellRenderer {

    public IconRenderer() {
        super();
    }

    @Override
    public void setValue(Object value) {
        if (value instanceof Integer) {
            switch ((Integer) value) {
                case GameModel.BLACK:
                    setIcon(style.getBlackIcn());
                    break;
                case GameModel.WHITE:
                    setIcon(style.getWhiteIcn());
                    break;
                case GameModel.POSSIBLE:
                    setIcon(style.getPossibleIcn());
                    break;
                default:
                    setIcon(style.getEmptyIcn());
            }
        }

    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Object firstColumnValue = table.getValueAt(row, 0);

        setVerticalAlignment(JLabel.TOP);
        setValue(value);
        setBackground(table.getBackground());
        setForeground(table.getForeground());
        return this;
    }
}

}

我必須像在圖片上一樣制作一個字段窗口。

使用GridLayout

每個字段都應該可以點擊

使用一個JButton 添加一個ActionListener來檢測鼠標單擊和按鈕的鍵盤激活。

這是一個同時使用網格布局和按鈕的示例: 制作健壯的,可調整大小的Swing Chess GUI

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM