[英]Game field using 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.