java中的requestFocus,Swing中requestFocusInWindow()和grabFocus()之间的区别

I would like to know the difference between requestFocusInWindow() and grabFocus() methods. Both of them work fine for grabbing the focus for me in this program. Therefore, i couldn't understand the difference.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Focus extends JFrame

{

JButton jb;

public Focus()

{

createAndShowGUI();

}

private void createAndShowGUI()

{

setTitle("grabFocus() vs requestFocusInWindow()");

setLayout(new FlowLayout());

setSize(400,400);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jb=new JButton("Open Dialog");

jb.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae)

{

showDialog();

}

});

add(jb);

}

private void showDialog()

{

JDialog jd=new JDialog();

jd.setLayout(new GridLayout(2,2));

jd.setVisible(true);

JLabel l1=new JLabel("Label 1");

JLabel l2=new JLabel("Label 2");

JTextField jt1=new JTextField(20);

JTextField jt2=new JTextField(20);

jd.add(l1);

jd.add(jt1);

jd.add(l2);

jd.add(jt2);

// Both of them are doing the thing

//jt2.grabFocus();

jt2.requestFocus();

jd.pack();

}

public static void main(String args[])

{

SwingUtilities.invokeLater(new Runnable(){

public void run()

{

new Focus();

}

});

}

}

解决方案

The answer is simple, grabFocus() grabs the focus, no matter whether the top-level ancestor is the focused window. If the window is not active, then it is made active to let the component get the focus.

Whereas, requestFocusInWindow() gets the focus for the component on which it is called only when its top-level ancestor is the focused window.

In your example, JDialog is the top level ancestor, it gets focus automatically when the JButton is clicked. So requestFocusInWindow() and grabFocus() does not make a difference.

I have re-written the program to better understand the difference using a pragmatic approach.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Focus extends JFrame

{

JButton jb;

JTextField jt;

public Focus()

{

createAndShowGUI();

}

private void createAndShowGUI()

{

setTitle("grabFocus() vs requestFocusInWindow()");

setLayout(new FlowLayout());

setSize(400,400);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jb=new JButton("Open Dialog");

jb.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae)

{

showDialog();

}

});

add(jb);

jt=new JTextField(20);

add(jt);

}

private void showDialog()

{

JDialog jd=new JDialog();

jd.setLayout(new GridLayout(2,2));

jd.setVisible(true);

JLabel l1=new JLabel("Label 1");

JLabel l2=new JLabel("Label 2");

JTextField jt1=new JTextField(20);

JTextField jt2=new JTextField(20);

jd.add(l1);

jd.add(jt1);

jd.add(l2);

jd.add(jt2);

jt.requestFocusInWindow();

//jt.grabFocus();

jd.pack();

}

public static void main(String args[])

{

SwingUtilities.invokeLater(new Runnable(){

public void run()

{

new Focus();

}

});

}

}

Here, requestFocusInWindow() is called on jt and it doesn't work (i.e. jt doesn't get the focus) because the JDialog is made active when the JButton is clicked and JTextField in the JDialog gets the focus.

Next, grabFocus() works. When the JButton is clicked, JDialog is displayed, but will not be active. Because upon call to the grabFocus(), immediately the JFrame becomes the active top-level ancestor and jt is forced to get the focus.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Swing ,可以通过添加键盘事件监听器来处理键盘事件的传递。以下是一个示例代码: ```java import javax.swing.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Game extends JFrame implements KeyListener { public Game() { // 设置窗口属性 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 500); setVisible); // 添加键盘事件监听器 addKeyListener(this); } public static void main(String[] args) { SwingUtilities.invokeLater(Game::new); } @Override public void keyTyped(KeyEvent e) { // 处理按键按下事件 char keyChar = e.getKeyChar(); System.out.println("Key Typed: " + keyChar); } @Override public void keyPressed(KeyEvent e) { // 处理按键按下事件 int keyCode = e.getKeyCode(); System.out.println("Key Pressed: " + keyCode); } @Override public void keyReleased(KeyEvent e) { // 处理按键释放事件 int keyCode = e.getKeyCode(); System.out.println("Key Released: " + keyCode); } } ``` 在上面的代码,`Game` 类继承自 `JFrame` 并实现了 `KeyListener` 接口。在构造函数,窗口的属性被设置,并且键盘事件监听器被添加到窗口上。 然后,通过实现 `keyTyped`、`keyPressed` 和 `keyReleased` 方法来处理键盘事件。在这些方法,你可以根据需要执行特定的操作。 注意:为了确保键盘事件能够被正确地捕获和处理,窗口必须具有焦点。你可以通过单击窗口或调用 `requestFocus()` 方法来设置焦点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值