Tag: mouselistener

MouseListener帮助Java

我正在尝试用Java Swing编写一个程序,它输出一个10 x 10网格的几何矩形,里面装满了randoms颜色。 但是,当用户单击显示窗口中的一个矩形时,矩形应重新绘制()并更改为另一种颜色。 到目前为止,我已经运行了基本程序,但我无法弄清楚如何为其实现mouseListener,以便在用户单击内部时更改矩形的颜色。 此时,矩形仅在显示窗口展开和最小化时重新绘制。 任何建议/帮助将不胜感激! 谢谢! 这是我到目前为止所拥有的…… import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.geom.*; public class ColorGrid extends JPanel { int w, x, y, z; Color c = new Color((int)(Math.random() * 0xFFFFFF)); public void paint(Graphics g){ Graphics2D g2 = (Graphics2D) g; setLayout(new GridLayout(10,10)); int w = x = y = z […]

使用setValueAt重新创建互斥复选框

我有一个使用自定义DefaultTableModel的JTable,它在最后一列中有一些布尔值(显示为勾选框)。 当我添加一个MouseListener来检索被点击的值时,似乎不再发生勾选框的切换。 // As soon as this is used in the component // that is using the JTable, the toggling stops table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { int col = table.getSelectedColumn(); int row = table.getSelectedRow(); Object o = table.getModel().getValueAt(row, col); 我假设侦听器正在使用该事件。 我可以将哪些内容添加到MouseListener代码以恢复切换行为? 编辑: 哎呀,似乎问题在于我的覆盖: @Override public void setValueAt(Object aValue, int row, int column) […]