我可以在java AWT中为图形形状添加关键监听器吗? [英] Can I add key listener to graphical shapes in java AWT ?

查看:61
本文介绍了我可以在java AWT中为图形形状添加关键监听器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UI由8个圆角矩形组成,每个形状在键盘导航按钮(向上和向下)选择时应显示不同的框架。



当我尝试为形状添加关键监听器时..



My UI consists of 8 rounded rectangle shapes and each shape when chosen by keyboard's navigation buttons (Up and Down) should display a distinct frame.

When I tried to add key listener to the shapes..

((Component)homeShape).addKeyListener(new KeyListener(){

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {}

    @Override
    public void keyReleased(KeyEvent e) {}});





1。形状需要被组合成一个组件。



2。将keyListener添加到UI中的n个形状是一项繁琐的工作。



我尝试过:



我使用了鼠标监听器,效果非常好。



1. The shape needs to be typecasted to a component.

2. It is a tedious job to add keyListener to "n" number of shapes in the UI.

What I have tried:

I used mouse listener and it worked perfectly well.

@Override
	public void mouseClicked(MouseEvent e) {
	if ((e.getButton() == 1) && homeShape.contains(e.getX(),e.getY()) ) {
			System.out.println("homebutton clicked");
			//code that displays new frame in the screen
		}
}





鼠标侦听器检测到用边界点击的形状(e.getX()和e.getY())。



但我现在想用键盘做,只需用向上,向下,向左,向右箭头按钮。



1。 KeyListener将如何检测它导航的形状?



2。我应该如何使用KeyListener来浏览形状?

推荐答案

1。 KeyListener不知道你指的是哪种形状,你只能通过鼠标捕获那些信息。

2.你需要知道焦点的位置,即最后点击的形状。您还需要确定每次按键时形状的像素数。
1. The KeyListener will not know which shape you are referring to, you can only capture that information via the mouse.
2. You would need to know where the focus is, i.e what shape was last clicked. You also need to decide how many pixels of a shape constitute each key press.


这篇关于我可以在java AWT中为图形形状添加关键监听器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆