gpt4 book ai didi

java - 如何在 JTable 中创建翻转效果?

转载 作者:行者123 更新时间:2023-11-30 03:39:36 25 4
gpt4 key购买 nike

在我的应用程序中,我想使用 JTable 的行创建翻转效果。例如,当鼠标进入该行时,该行的背景颜色会发生变化;反之,当鼠标退出该行时,该行的颜色将恢复正常。

我该怎么做?

最佳答案

使用下面的代码,它将让您了解如何对 JTable 的行应用翻转效果。

import java.awt.Component;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.table.*;

public class RollOverTable extends JTable {

private int rollOverRowIndex = -1;

public RollOverTable(TableModel model) {
super(model);
RollOverListener lst = new RollOverListener();
addMouseMotionListener(lst);
addMouseListener(lst);
}

public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if( isRowSelected(row) || (row == rollOverRowIndex) ) {
c.setForeground(getSelectionForeground());
c.setBackground(getSelectionBackground());
}
else {
c.setForeground(getForeground());
c.setBackground(getBackground());
}
return c;
}


private class RollOverListener extends MouseInputAdapter {

public void mouseExited(MouseEvent e) {
rollOverRowIndex = -1;
repaint();
}

public void mouseMoved(MouseEvent e) {
int row = rowAtPoint(e.getPoint());
if( row != rollOverRowIndex ) {
rollOverRowIndex = row;
repaint();
}
}
}
}

关于java - 如何在 JTable 中创建翻转效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27075493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com