java - 触发setCursor方法后光标图标没有变化

标签 java swing icons mouse-cursor

我的应用程序中有一个 JTable,它带有可调整大小的标题列。通常,当我将光标移到表头上以调整大小时,光标图标会变为调整大小箭头,如 <-->。

但在下面的场景中情况有所不同。

在同一 Frame 中有一个按钮操作,在执行操作期间,我将光标设置为忙碌图标,并在操作完成后将其更改回默认光标,使用 Container.setCursor(Cursor 游标) 方法。

有时,如果我将光标移到调整大小的表头上,在按钮操作后,光标图标不会更改为调整大小箭头,光标根本不会改变。

这是否可以被视为 Java Swing 中的错误,或者是否有针对此问题的解决方案?

更新:附上示例代码

import java.util.*;  
import java.awt.*;  
import javax.swing.*;  
import java.awt.event.*;

public class ColumnResizeIconTest extends JFrame {

JScrollPane scrollPane;
JTable table;
JButton button;

public ColumnResizeIconTest() {
    setLayout(new BorderLayout());
    addComponents();
    setSize(300,300);
}

private void addComponents() {
    addButton();
    addTable();
}

private void addButton() {
    button = new JButton("Click Me");
    button.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            setWaitCursor();
            for(int i=0; i<2000; i++) {
                System.out.print(i);
            }
            setDefaultCursor();
        }
    });
    add(button, BorderLayout.NORTH);
}

private void addTable() {
    scrollPane = new JScrollPane(createTable());
    add(scrollPane, BorderLayout.CENTER);
}

private JTable createTable() {
    Object[][] cellData = { { "1-1", "1-2","1-3" }, { "2-1", "2-2", "2-3" }, { "3-1", "3-2", "3-3" } };
    String[] columnNames = { "column1", "column2", "column3" };
    table = new JTable(cellData, columnNames);
    return table;
}

private void setWaitCursor() {
    Container container = getContentPane();
    setWaitCursor(container);
}

private void setWaitCursor(Container container) {
    for(int iCount = 0; iCount < container.getComponentCount(); iCount++) {
        Component child = (Component) container.getComponent(iCount);
        if(child instanceof Container) {
            setWaitCursor((Container) child);
        } else {
            child.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        }
    }
    container.setCursor(new Cursor(Cursor.WAIT_CURSOR));
}

private void setDefaultCursor() {
    Container container = getContentPane();
    setDefaultCursor(container);
}

private void setDefaultCursor(Container container) {
    for(int iCount = 0; iCount < container.getComponentCount(); iCount++) {
        Component child = (Component) container.getComponent(iCount);
        if(child instanceof Container) {
            setDefaultCursor((Container) child);
        } else {
            child.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    }
    container.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}

public static void main(String[] argv) throws Exception {
    ColumnResizeIconTest test = new ColumnResizeIconTest();
    test.setVisible(true);
}
}

点击按钮几次并尝试调整表格列的大小。光标卡在默认光标上。

最佳答案

正如我的评论中已经提到的:重新/设置游标并非完全微不足道,即使对于单个组件也是如此 :-) 基本问题(在递归游标设置中等待)是假设所有组件 < em>确实有默认光标。

正如您在表格标题中看到的那样,该假设是不正确的:在该组件上,“默认”是 defaultCursor 或 resizeCursor,具体取决于鼠标位置。此外,内部光标切换不是很智能:它不检查状态(从我的头顶上看,刚才被这个事实击中了:-)

不完全确定你想要达到什么,所以没有具体的解决方案,除了完全放弃递归设置,很难做到正确。选项可能是

  • 使 glassPane(框架的根 Pane )可见并在其上设置 waitCursor
  • 使用 JLayer (jdk7) 或 JXLayer (jdk6) 在较小的区域上并在其上设置 waitCursor
  • 使用侵入性较小的可视化,f.i.某处的 JProgressBar 或 JXBusyLabel ( in the SwingX project)

附录(@mKorbel :-)

问题很容易重现,只需对 OPs SSCCE 稍作更改(感谢!):如下更改 addButton 方法,然后单击按钮,当等待光标显示时,将鼠标移到标题中然后到另一列(跨列边界)。多次这样做会导致标题上出现不可预测的游标 ...

private void addButton() {
    button = new JButton("Click Me");
    final ActionListener off = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            setDefaultCursor();
            button.setEnabled(true);
        }

    };
    button.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            setWaitCursor();
            button.setEnabled(false);
            Timer timer = new Timer(2000, off);
            timer.setRepeats(false);
            timer.start();
        }
    });

    add(button, BorderLayout.NORTH);
}

关于java - 触发setCursor方法后光标图标没有变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8830642/

相关文章:

java - 我如何实现一个具有多行按钮的 Java GUI,这些按钮彼此等距

python - 在 Mac OS 上设置 tkinter 图标

java - Libgdx Pixmap drawCircle 正在绘制

java - Android 闪屏到 FragmentActivity

java - Swing:如何将自定义光标设置为透明?

java - 使除 1 个元素外的所有元素向 FlowLayout 左侧 float

c# - 任务栏中显示的错误(默认?)图标

c# - 怎么给桌面应用程序快捷方式添加图标

java - Android中如何管理数据

java - C# Xamarin Java.Interop 错误?