gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:49 24 4
gpt4 key购买 nike

我的应用程序中有一个 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/

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