在程序运行的时候焦点总是在窗口上,我想改变它,让焦点移到画布(canva)
我在文档上找到这个函数setFocusable(true),但应用起来不是很使!!!
大家来帮下忙吧。
解决方案 »
- 谁能帮我解释一下定义泛型下界时,使用getSuperclass()不能编译?
- Java for循环优化
- dateoutputstream和dateinputstream怎么用
- 关于JDBC连接sqlserver2000 的问题
- 设置jTextArea某处的字体
- 大侠帮忙啊! ArrayList 二维怎么用
- 刚建了一个java学习群,还没有人加入,有兴趣的都可以加入!
- 各位高手,我想问问如何控制用户在JTextField中的输入????
- 完了,我彻底的完了,求命啊~~~~~~~~~
- 请 问 Java 里 面 实 现 一 块 内 存 的 拷 贝 ?
- 关于类关系问题,恳求答案
- 集合框架中元素的内存释放问题。
public class Gamebank extends Canvas implements KeyListener
{
Mainshare tu = new shangTu();
public Gamebank()
{
setSize(200,400);
setVisible(true);
setBackground(Color.BLACK);
tu.setPlace();
addKeyListener(this);
//requestFocus(true);//设置画布的焦点
requestFocusInWindow();// 这两个设置都不好使
}
public static void main(String[] a)
{
JFrame f = new JFrame();
f.setSize(220,440);
f.setVisible(true);
f.setResizable(false);//设置窗口可变
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(game);
}
}
{
Mainshare tu = new shangTu();
public Gamebank()
{
setSize(200,400);
setVisible(true);
setBackground(Color.BLACK);
tu.setPlace();
addKeyListener(this);
//requestFocus(true);//设置画布的焦点
//requestFocusInWindow();// 这两个设置都不好使
}
public static void main(String[] a)
{
JFrame f = new JFrame();
f.setSize(220,440);
f.setVisible(true);
f.setResizable(false);//设置窗口可变
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(game); game.requestFocusInWindow();// }
}
这样行吗,我最近也在研究此问题,但你没把代码传完整,我们怎么帮你调试。,至少你的代码也要运行通过。
请看以下的代码
public static void main(String[] a)
{
JFrame f = new JFrame();
Gamebank game = new Gamebank();
f.setSize(220,440);
f.setVisible(true);
f.setResizable(false);//设置窗口可变
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/////////////////////////////////////////
//game.requestFocus(true); //在没添加之前设置的焦点都不好使。
//game.requestFocusInWindow();
////////////////////////////////////////
f.getContentPane().add(game);
game.requestFocus(true);//而这个就好使了
game.requestFocusInWindow();//这个也好使了,
}
不过还是先谢谢你了啊。你能能否给我讲一下这个焦点是什么回事吗??