- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作俄罗斯方 block 克隆。该游戏使用 JTable
作为棋盘的表示。棋盘是一个二维整数数组。
我试图做到这一点,当某个单元格具有某个值时,该单元格将更改为某种颜色。我以为我已经正常工作了,但它不起作用。我真的很感激一些帮助。
谢谢。
这是我的代码:
董事会:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Timer;
import javax.swing.table.*;
/**
* @author _______________
*
* Board.java
*
* The Board class gives information to the InitializeJTable class regarding
* the data-type of the JTable elements, how many rows/columns are in the JTable, etc;
*
* I extended Board with AbstractTableModel, a class that allows me to display any data-type
* inside the elements of the JTable.
*
* This class also has method declarations that are required by AbstractTableModel, these methods are:
* - getRowCount()
* - getColumnCount()
* - getValueAt(int, int)
*
*/
@SuppressWarnings("serial")
public class Board extends AbstractTableModel {
TableColorSetter TCS;
boolean lost = false;
InitializeGUI iGUI;
InitializeJTable iJT;
public int SPEED = 1000;
L l = new L();
public Block currentBlock = l;
public int[][] boxes;//A 2D Array of booleans that defines the status of the Tetris board. Used by InitializeJTable and InitializePreviewJTable
/*
* boardType is an integer that works with the constructor.
*
* If boardType is equal to 0, the constructor generates a 22x10 array (This is used with the main board)
* However, if boardType is equal to 1, the constructor generates a 4x4 array (This is used with the preview/hold board, a smaller board on the side)
*/
public Board(int boardType) {
if(boardType == 0) {
boxes = new int[22][10];
loop();
} else if(boardType == 1) boxes = new int[4][4];
}
/*
* getRowCount()
*
* getRowCount is a required declaration by the AbstractTableModel class.
* @return the amount of rows in the boxes boolean
*/
@Override
public int getRowCount() {
return boxes.length;
}
public int getTetrisType(Block block) {
return block.getTetrisType();
}
public void letPieceDown(int origA, int origB, int tetrisType) {
boxes[origA][origB] = 0;
boxes[origA + 1][origB] = tetrisType;
}
/*
* getColumnCount()
*
* getColumnCount() is a required declaration by the AbstractTableModel class.
* @return the amount of columns in the boxes boolean
*/
@Override
public int getColumnCount() {
return boxes[0].length;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
boxes[rowIndex][columnIndex] = (int)aValue;
fireTableCellUpdated(rowIndex, columnIndex);
}
/*
* getValueAt()
*
* getValueAt() is a required declaration by the AbstractTableModel class.
* returns the amount of columns in the boxes boolean
*
* @param int rowIndex The row index of the boolean that you want to return.
* int columnIndex The column index of the boolean that you want to return.
*
* @return the value of the boolean at the specified location in the boolean array.
*/
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return boxes[rowIndex][columnIndex];
}
public int getVal(int row, int col) {
return boxes[row][col];
}
/**
*
* loop();
*
* The loop() method is a very rough draft in regards to placing Tetris objects on the board and having them move, and stack.
*
* TODO:
*/
public void loop() {
Timer timer = new
Timer(SPEED, new ActionListener() {
int x = 0;
int y = 0;
@Override
public void actionPerformed(ActionEvent e) {
while(lost = false) {
for(int a = 0; a < getColumnCount(); a++) {
for(int b = 0; b < getRowCount(); b++) {
TCS.getTableCellRendererComponent(iJT.getTable(), "", false, false, b, a);
}
}
}
if(x == getRowCount() - 1) {
x = 0;
} else if(boxes[x + 1][y] != 0 && x == 0) {
boxes[x][y] = getTetrisType(currentBlock);
fireTableDataChanged();
//System.out.println("GAME OVER!");
//y++; //Just for testing.
PlaySound sound = new PlaySound();
sound.setSoundType(1);
sound.start();
((Timer)e.getSource()).stop();
} else if(boxes[x + 1][y] != 0) {
x = 0;
} else {
//System.out.println("Pos: " + x + ", 0. Row count: " + getRowCount() + " -- " + x);
letPieceDown(x, y, getTetrisType(currentBlock));
fireTableDataChanged();
x++;
}
}
});
timer.start();
}
}
初始化JTable:
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
/**
* @author _________________
*
* InitializeJTable.java
*
* The InitializeJTable class sets up the JTable to look like a Tetris board.
*
* @TODO: Add more commenting
*/
@SuppressWarnings("serial")
public class InitializeJTable extends JPanel {
private int COLS = 10;
private int HEIGHT = 30;
Color white = Color.WHITE;
Color black = Color.BLACK;
Board tetrisBoard;
JTable table;
public Board returnBoard() {
return tetrisBoard;
}
public JTable getTable() {
return table;
}
public InitializeJTable() { //The InitializeJTable constructor
tetrisBoard = new Board(0); //Create a new board of 22x10 booleans, with a boardType of 0.
table = new JTable(tetrisBoard); //Apply the information from the Board constructor to the JTable. (Information like, how many rows/cols in the JTable, what data-type the JTable elements are, etc)
//setLayout(new GridBagLayout()); //Sets the Layout of the JTable to a GridBagLayout. This means, it makes the JTable look like a grid.
table.setCellSelectionEnabled(false); //Disables dragging/selection of columns on the JTable with the mouse. We won't be needing that.
TableColumnModel columns = table.getColumnModel(); //Gets information about the columns of the JTable. I need this to set the length of the columns (to make them square)
table.setRowHeight(HEIGHT); //Sets the height of the JTable rows to HEIGHT int, currently set to 30. Can be easily changed.
//table.setForeground(white); //Use this to make all blocks white. Still need to make it work correctly.
/*
* Sadly, there isn't a -- table.setColumnHeight(int) -- method in the JTable API. (to set the length of the columns to the specified integer)
*
* So I have to manually set the width of each column. This is why I declared the TableColumnModel above
*/
for(int a = 0; a < COLS; a++) {
columns.getColumn(a).setPreferredWidth(HEIGHT); //Sets the height of the JTable columns to HEIGHT int, currently set to 30. Can be changed easily.
}
add(table);//The table's all set up correctly, add it.
}
}
最佳答案
数据/模型和 View /表之间存在关系。模型维护“什么”, View 控制“如何”。
JTable
提供了一种方法,您可以通过使用 TableCellRenderers
来实现“如何”(内容渲染)的行为,这些负责确定如何应根据模型中“内容”的值“绘制”单元格。
首先查看 How to Use Tables和 Using Custom Renderers
现在,此示例使用 double
值来确定单元格值与 1
的距离,该值代表颜色(黑色 = 0;白色 = 1)电池应该被绘制(paint)。为了实现这一点,它使用自定义的 TableCellRenderer
将模型中的值(“什么”)转换为颜色(或“如何”)
public class PaintTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, "", isSelected, hasFocus, row, column);
if (value instanceof Double) {
double distance = (double) value;
int part = (int) (255 * distance);
Color color = new Color(part, part, part);
setBackground(color);
} else {
setBackground(Color.WHITE);
}
return this;
}
}
它还演示了您可能还需要了解的有关 JTable
的其他一些事情。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
public class Smile {
public static void main(String[] args) {
new Smile();
}
public Smile() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private double[][] smily = {
{1, 1, 1, 1, 1, 0.996, 1, 0.843, 0.784, 0.788, 0.773, 0.769, 0.765, 0.765, 0.788, 0.784, 0.847, 1, 0.996, 1, 1, 1, 1, 0.996},
{1, 1, 1, 1, 1, 0.871, 0.733, 0.761, 0.847, 0.941, 0.941, 0.941, 0.941, 0.941, 0.933, 0.843, 0.761, 0.733, 0.871, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0.784, 0.733, 0.902, 0.941, 0.941, 0.941, 0.945, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.898, 0.733, 0.784, 1, 1, 1, 1},
{1, 1, 1, 0.765, 0.773, 0.945, 0.945, 0.941, 0.929, 0.937, 0.941, 0.941, 0.945, 0.941, 0.957, 0.91, 0.941, 0.941, 0.941, 0.78, 0.761, 1, 1, 1},
{1, 1, 0.808, 0.773, 0.941, 0.941, 0.941, 0.941, 0.294, 0.447, 0.941, 0.941, 0.941, 0.941, 0.702, 0.239, 0.886, 0.941, 0.945, 0.941, 0.78, 0.8, 1, 1},
{1, 0.89, 0.725, 0.945, 0.941, 0.941, 0.945, 0.843, 0, 0, 0.922, 0.945, 0.941, 0.941, 0.408, 0, 0.663, 0.941, 0.941, 0.945, 0.941, 0.725, 0.89, 1},
{0.992, 0.753, 0.902, 0.941, 0.945, 0.945, 0.941, 0.725, 0.051, 0, 0.808, 0.941, 0.941, 0.945, 0.294, 0.051, 0.553, 0.941, 0.941, 0.941, 0.941, 0.91, 0.741, 0.984},
{0.871, 0.78, 0.941, 0.941, 0.945, 0.945, 0.941, 0.694, 0.051, 0, 0.784, 0.945, 0.941, 0.941, 0.278, 0, 0.518, 0.941, 0.941, 0.945, 0.941, 0.941, 0.78, 0.878},
{0.816, 0.855, 0.941, 0.945, 0.945, 0.945, 0.941, 0.737, 0, 0.051, 0.82, 0.941, 0.941, 0.941, 0.302, 0.051, 0.565, 0.941, 0.949, 0.945, 0.941, 0.941, 0.863, 0.804},
{0.8, 0.945, 0.941, 0.945, 0.945, 0.941, 0.941, 0.875, 0, 0, 0.937, 0.941, 0.941, 0.941, 0.443, 0, 0.694, 0.945, 0.945, 0.945, 0.945, 0.949, 0.941, 0.765},
{0.769, 0.941, 0.945, 0.957, 0.961, 0.941, 0.945, 0.941, 0.443, 0.565, 0.945, 0.941, 0.941, 0.941, 0.769, 0.388, 0.918, 0.941, 0.941, 0.941, 0.945, 0.941, 0.941, 0.78},
{0.753, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.953, 0.941, 0.941, 0.941, 0.941, 0.945, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.788},
{0.741, 0.945, 0.839, 0.427, 0.624, 0.941, 0.941, 0.945, 0.941, 0.941, 0.941, 0.949, 0.945, 0.945, 0.941, 0.941, 0.941, 0.941, 0.941, 0.6, 0.376, 0.941, 0.945, 0.784},
{0.749, 0.941, 0.914, 0.345, 0.647, 0.941, 0.945, 0.949, 0.945, 0.945, 0.941, 0.941, 0.945, 0.941, 0.945, 0.945, 0.945, 0.945, 0.941, 0.702, 0.384, 0.941, 0.941, 0.78},
{0.796, 0.945, 0.941, 0.627, 0.592, 0.941, 0.941, 0.941, 0.945, 0.945, 0.945, 0.941, 0.949, 0.945, 0.941, 0.945, 0.945, 0.937, 0.945, 0.58, 0.631, 0.941, 0.937, 0.776},
{0.812, 0.859, 0.941, 0.855, 0.384, 0.957, 0.941, 0.945, 0.945, 0.945, 0.941, 0.953, 0.941, 0.945, 0.945, 0.945, 0.941, 0.941, 0.941, 0.384, 0.941, 0.941, 0.867, 0.812},
{0.871, 0.788, 0.941, 0.941, 0.533, 0.51, 0.941, 0.941, 0.941, 0.945, 0.949, 0.945, 0.945, 0.945, 0.941, 0.937, 0.941, 0.945, 0.522, 0.522, 0.941, 0.941, 0.792, 0.886},
{0.992, 0.761, 0.914, 0.941, 0.941, 0.325, 0.612, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.624, 0.318, 0.941, 0.945, 0.91, 0.765, 0.988},
{1, 0.882, 0.741, 0.941, 0.941, 0.922, 0.337, 0.475, 0.894, 0.941, 0.941, 0.941, 0.941, 0.941, 0.945, 0.894, 0.49, 0.325, 0.925, 0.941, 0.941, 0.753, 0.894, 1},
{1, 1, 0.796, 0.78, 0.941, 0.941, 0.941, 0.592, 0.447, 0.565, 0.667, 0.737, 0.737, 0.667, 0.565, 0.451, 0.588, 0.941, 0.941, 0.941, 0.796, 0.808, 1, 1},
{1, 1, 0.996, 0.753, 0.788, 0.941, 0.941, 0.941, 0.941, 0.702, 0.584, 0.557, 0.553, 0.592, 0.698, 0.906, 0.941, 0.945, 0.941, 0.796, 0.769, 0.996, 1, 1},
{1, 1, 1, 1, 0.769, 0.745, 0.922, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.941, 0.918, 0.741, 0.776, 1, 1, 0.996, 1},
{1, 0.996, 1, 1, 1, 0.851, 0.733, 0.773, 0.867, 0.945, 0.941, 0.941, 0.941, 0.941, 0.945, 0.867, 0.769, 0.725, 0.851, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0.984, 0.843, 0.78, 0.761, 0.78, 0.796, 0.796, 0.784, 0.765, 0.78, 0.835, 0.984, 1, 1, 1, 1, 1, 1}
};
public TestPane() {
AsciiTableModel model = new AsciiTableModel();
model.setData(smily);
JTable table = new JTable(model);
table.setRowHeight(24);
Enumeration<TableColumn> columns = table.getColumnModel().getColumns();
while (columns.hasMoreElements()) {
TableColumn col = columns.nextElement();
col.setWidth(24);
col.setPreferredWidth(24);
col.setMinWidth(24);
col.setMaxWidth(24);
}
table.setRowHeight(24);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setDefaultRenderer(Object.class, new PaintTableCellRenderer());
setLayout(new BorderLayout());
add(new JScrollPane(table));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class PaintTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, "", isSelected, hasFocus, row, column);
if (value instanceof Double) {
double distance = (double) value;
int part = (int) (255 * distance);
Color color = new Color(part, part, part);
setBackground(color);
} else {
setBackground(Color.WHITE);
}
return this;
}
}
public class AsciiTableModel extends AbstractTableModel {
private double[][] data;
public AsciiTableModel() {
data = new double[24][24];
}
public void setData(double[][] value) {
data = value;
fireTableDataChanged();
}
@Override
public int getRowCount() {
return 24;
}
@Override
public int getColumnCount() {
return 24;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
}
}
我本来打算提供一张“悲伤”的脸,你可以在它们之间切换,但我女儿想让我和她一起去画画,抱歉;)
关于java - 如何根据单元格中的值对 JTable 的各个单元格着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30552644/
我已经尝试在我的 CSS 中添加一个元素来删除每三个 div 的 margin-right。不过,似乎只是出于某种原因影响了第 3 次和第 7 次。需要它在第 3、6、9 等日工作... CSS .s
如何使 div/input 闪烁或“脉冲”?例如,假设表单字段输入了无效值? 最佳答案 使用 CSS3 类似 on this page ,您可以将脉冲效果添加到名为 error 的类中: @-webk
我目前正在尝试构建一个简单的 wireframe来自 lattice 的情节包,但由沿 y 轴的数百个点组成。这导致绘图被线框网格淹没,您看到的只是一个黑色块。我知道我可以用 col=FALSE 完全
在知道 parent>div CSS 选择器在 IE 中无法识别后,我重新编码我的 CSS 样式,例如: div#bodyMain div#paneLeft>div{/*styles here*/}
我有两个 div,一个在另一个里面。当我将鼠标悬停 到最外面的那个时,我想改变它的颜色,没问题。但是,当我将鼠标悬停 到内部时,我只想更改它的颜色。这可能吗?换句话说,当 将鼠标悬停到内部 div 上
我需要展示这样的东西 有人可以帮忙吗?我可以实现以下输出 我正在使用以下代码:: GridView.builder( scrollDirection: Axis.vertical,
当 Bottom Sheet 像 Android 键盘一样打开时,是否有任何方法可以手动上推布局( ScrollView 或回收器 View 或整个 Activity )?或者你可以说我想以 Bott
我有以下代码,用于使用纯 HTML 和 CSS 显示翻转。当您将鼠标悬停在文本上时,它会更改左右图像。 在我测试的所有浏览器中都运行良好,Safari 4 除外。据我收集的信息,Safari 4 支持
我构建了某种 CMS,但在使用 TinyMCE 和 Bootstrap 时遇到了一些问题。 我有一个页面,其中概述了一个 div,如果用户单击该 div,他们可以从模态中选择图像。该图像被插入到一个
出于某种原因,当我设置一个过渡时,当我的鼠标悬停在一个元素上时,背景会改变颜色,它只适用于一个元素,但它们都共享同一个类?任何帮助我的 CSS .outer_ad { position:rel
好吧,这真的很愚蠢。我不知道 Android Studio 中的调试监视框架发生了什么。我有 1.5.1 的工作室。 是否有一些来自 intellij 的 secret 知识来展示它。 最佳答案 与以
我有这个标记: some code > 我正在尝试获取此布局: 注意:上一个和下一个按钮靠近#player 我正在尝试这样: .nextBtn{
网站:http://avuedesigns.com/index 首页有 6 个菜单项。我希望每件元素在您经过时都有自己的颜色。 这是当您将鼠标悬停在 div 上时将所有内容更改为白色的行 li#hom
我需要在 index.php 文件中显示它,但没有任何效果。我所有的文章都没有正确定位。我将其用作代码: 最佳答案 您可以首先检查您
我是一名优秀的程序员,十分优秀!