gpt4 book ai didi

java - 如何将鼠标光标放在 JFileChooser 上的 OPEN 按钮上?

转载 作者:行者123 更新时间:2023-11-30 03:52:16 26 4
gpt4 key购买 nike

我试图将鼠标光标默认放在 JFileChooser 上的批准按钮上,但我找不到以前做过此操作的任何示例。我尝试过使用硬编码的 x,y 位置,但是当我在另一台电脑上运行我的应用程序时,这是没有用的。任何帮助将不胜感激,我的代码如下:

FileOpenDialog fileChooser = new FileOpenDialog(index);
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
}
});

// Need to snap mouse cursor to OPEN button here somehow or within overidden
// method of showOpenDialog???

int returnVal = fileChooser.showOpenDialog(mainFrame);
System.out.println("Return Value is " + returnVal);

if (returnVal == FileOpenDialog.APPROVE_OPTION) {
setFileIndex(index);
setInputFile(fileChooser.getSelectedFile());
}

class FileOpenDialog extends JFileChooser {

public String fileName;
public String dialogTitle;

public FileOpenDialog(int index) {
initComponent(index);
}

private void initComponent(int index) {
setBackground(Color.lightGray);
setAcceptAllFileFilterUsed(false);

CustomFileFilter myFilter = new CustomFileFilter();
setFileFilter(myFilter);

switch (index) {
case 0:
setFileName("\\MelbCupHorses.txt");
setDialogTitle("Please Choose Horses File");
break;
case 1:
setFileName("\\MelbCupEntrants.txt");
setDialogTitle("Please Choose Employees File");
break;
}

System.out.println(getCurrentDirectory().toString() + fileName);

File file = new File(getCurrentDirectory().toString() + fileName);
setSelectedFile(file);
}


/**
* @return the dialogTitle
*/
@Override
public String getDialogTitle() {
return dialogTitle;
}

/**
* @param dialogTitle the dialogTitle to set
*/
@Override
public void setDialogTitle(String dialogTitle) {
this.dialogTitle = dialogTitle;
}
}

谢谢你。

最佳答案

由于对话框的模式状态,它可能有点棘手,但是使用 WindowListenerjava.awt.Robot ,可以实现

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FileChooserExample {

public static void main(String[] args) {
new FileChooserExample();
}

public FileChooserExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

FileOpenDialog dialog = new FileOpenDialog(0);
dialog.showOpenDialog(null);

}
});
}

class FileOpenDialog extends JFileChooser {

public String fileName;
public String dialogTitle;

public FileOpenDialog(int index) {
initComponent(index);
}

@Override
protected JDialog createDialog(Component parent) throws HeadlessException {
JDialog dialog = super.createDialog(parent);
dialog.addWindowListener(new WindowAdapter() {

@Override
public void windowOpened(WindowEvent e) {
JDialog dialog = (JDialog) e.getWindow();
JButton button = dialog.getRootPane().getDefaultButton();
Point pos = button.getLocationOnScreen();
Dimension size = button.getSize();
pos.x += (size.width / 2);
pos.y += (size.height / 2);

try {
Robot bot = new Robot();
bot.mouseMove(pos.x, pos.y);
} catch (AWTException ex) {
ex.printStackTrace();
}
}

});
return dialog;
}

private void initComponent(int index) {
setBackground(Color.lightGray);
setAcceptAllFileFilterUsed(false);

System.out.println(getCurrentDirectory().toString() + fileName);

File file = new File(getCurrentDirectory().toString() + fileName);
setSelectedFile(file);
}

/**
* @return the dialogTitle
*/
@Override
public String getDialogTitle() {
return dialogTitle;
}

/**
* @param dialogTitle the dialogTitle to set
*/
@Override
public void setDialogTitle(String dialogTitle) {
this.dialogTitle = dialogTitle;
}
}
}

下一个问题是...你为什么乱搞我的鼠标?

关于java - 如何将鼠标光标放在 JFileChooser 上的 OPEN 按钮上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24134978/

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