java相关 键盘焦点问题
importjava.awt.EventQueue;importjavax.swing.JFrame;importjavax.swing.JPanel;importjav...
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
public class ANjian extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ANjian frame = new ANjian();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ANjian() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 96, 32);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(152, 10, 114, 44);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(10, 75, 96, 49);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(162, 64, 104, 44);
contentPane.add(textField_3);
textField_3.setColumns(10);
}
}
要求是用键盘上的上下左右键控制焦点按右键焦点到 右边的文本框 按左键到左边的文本框 按下键到下边的文本框 就这点要求 谢谢了 展开
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
public class ANjian extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ANjian frame = new ANjian();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ANjian() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 96, 32);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(152, 10, 114, 44);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(10, 75, 96, 49);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(162, 64, 104, 44);
contentPane.add(textField_3);
textField_3.setColumns(10);
}
}
要求是用键盘上的上下左右键控制焦点按右键焦点到 右边的文本框 按左键到左边的文本框 按下键到下边的文本框 就这点要求 谢谢了 展开
1个回答
展开全部
给你弄好了,见代码
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Test extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
public class mKeyListener implements KeyListener {
public void keyPressed(KeyEvent e) {
String name = ((JTextField) e.getSource()).getName();
if (name.equals("textField")) {
switch(e.getKeyCode()){
case 39:
textField_1.grabFocus();
break;
case 40:
textField_2.grabFocus();
break;
}
}else if(name.equals("textField_1")){
switch(e.getKeyCode()){
case 37:
textField.grabFocus();
break;
case 40:
textField_3.grabFocus();
break;
}
}else if(name.equals("textField_2")){
switch(e.getKeyCode()){
case 39:
textField_3.grabFocus();
break;
case 38:
textField.grabFocus();
break;
}
}else if(name.equals("textField_3")){
switch(e.getKeyCode()){
case 37:
textField_2.grabFocus();
break;
case 38:
textField_1.grabFocus();
break;
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test() {
mKeyListener m1 = new mKeyListener();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setName("textField");
textField.setBounds(10, 10, 96, 32);
textField.requestFocus();
textField.addKeyListener(m1);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setName("textField_1");
textField_1.setBounds(152, 10, 114, 44);
textField_1.addKeyListener(m1);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setName("textField_2");
textField_2.setBounds(10, 75, 96, 49);
textField_2.addKeyListener(m1);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setName("textField_3");
textField_3.setBounds(162, 64, 104, 44);
textField_3.addKeyListener(m1);
contentPane.add(textField_3);
textField_3.setColumns(10);
}
}
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Test extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
public class mKeyListener implements KeyListener {
public void keyPressed(KeyEvent e) {
String name = ((JTextField) e.getSource()).getName();
if (name.equals("textField")) {
switch(e.getKeyCode()){
case 39:
textField_1.grabFocus();
break;
case 40:
textField_2.grabFocus();
break;
}
}else if(name.equals("textField_1")){
switch(e.getKeyCode()){
case 37:
textField.grabFocus();
break;
case 40:
textField_3.grabFocus();
break;
}
}else if(name.equals("textField_2")){
switch(e.getKeyCode()){
case 39:
textField_3.grabFocus();
break;
case 38:
textField.grabFocus();
break;
}
}else if(name.equals("textField_3")){
switch(e.getKeyCode()){
case 37:
textField_2.grabFocus();
break;
case 38:
textField_1.grabFocus();
break;
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test() {
mKeyListener m1 = new mKeyListener();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setName("textField");
textField.setBounds(10, 10, 96, 32);
textField.requestFocus();
textField.addKeyListener(m1);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setName("textField_1");
textField_1.setBounds(152, 10, 114, 44);
textField_1.addKeyListener(m1);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setName("textField_2");
textField_2.setBounds(10, 75, 96, 49);
textField_2.addKeyListener(m1);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setName("textField_3");
textField_3.setBounds(162, 64, 104, 44);
textField_3.addKeyListener(m1);
contentPane.add(textField_3);
textField_3.setColumns(10);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询