Ⅰ java中如何让一个标签获得焦点
获取焦点的方法为requestFocus();组件一般都有这个方法,这个方法的作用就是使调用方法的组件获取焦点,你在窗口函数里面调用requestFocus();方法就可以了。
Ⅱ java 焦点
在JTextField的按键响应事件中判断是否为上下键,如果是,对JComboBox中进行选项切换。
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
// 添加field
JTextField field = new JTextField();
field.setPreferredSize(new Dimension(100, 20));
frame.getContentPane().add(field);
// 添加box
final JComboBox box = new JComboBox(new String[] { "1", "2", "3" });
box.setPreferredSize(new Dimension(100, 20));
frame.getContentPane().add(box);
// field添加按键响应
field.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e);
int box_index = box.getSelectedIndex();
if (e.getKeyCode() == KeyEvent.VK_UP) {
box.setSelectedIndex(box_index == 0 ? 0 : box_index - 1);
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
box
.setSelectedIndex(box_index == box.getItemCount() - 1 ? box_index
: box_index + 1);
}
}
});
frame.setBounds(0, 0, 320, 240);
frame.setVisible(true);
}
Ⅲ JAVA 如何获取焦点
不能同时用1个键的 你用其他键 注册热键2个就可以了
Ⅳ java怎么让JTextField获取焦点,然后把光标定位在JTextField中
一般在jframe中用如下代码实现:
in = new JTextField(40);
f.addWindowListener( new WindowAdapter() {
public void windowOpened( WindowEvent e ){
in.requestFocus();
}
});
f是你的Jframe对象,in是JTextField对象。
Ⅳ 高手支招 java FocusListener焦点事件如何实现在两个JTextField中确认焦点!
刚才写了个程序,楼主看看符合要求不
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
class TestFocus extends JFrame implements FocusListener {
JButton btn;
JTextArea text1,text2;
boolean b=true;
public TestFocus() {
btn=new JButton("切换焦点");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(b) {
//text2.setRequetFocusEnabled(fasle);
text1.grabFocus();
b=false;
}
else{
text2.grabFocus();
b=true;
}
}
});
JPanel p1=new JPanel(new FlowLayout());
p1.add(Box.createGlue());
p1.add(btn);
p1.add(Box.createGlue());
JPanel p2=new JPanel(new FlowLayout());
text1=new JTextArea();
text2=new JTextArea();
p2.add(create(text1,"第一个文本域"));
p2.add(create(text2,"第二个文本域"));
text1.addFocusListener(this);
text2.addFocusListener(this);
getContentPane().add(p1,"North");
getContentPane().add(p2,"Center");
setVisible(true);
setSize(200,200);
}
public static void main(String [] args) {
new TestFocus();
}
public jscrollPane create(JTextArea text,String s) {
text.setLineWrap(true);
JScrollPane jsp=new JScrollPane(text);
Border border=BorderFactory.createLineBorder(Color.blue);
jsp.setBorder(BorderFactory.createTitledBorder(border,s,TitledBorder.LEFT,TitledBorder.TOP));
return jsp;
}
public void focusGained(FocusEvent e) {
JTextArea text=(JTextArea)e.getSource();
if(text==text1) {
text1.setText("");
text1.setText("获得焦点");
}
if(text==text2) {
text2.setText("");
text2.setText("获得焦点");
}
}
public void focusLost(FocusEvent e) {
JTextArea text=(JTextArea)e.getSource();
if(text==text1) {
text1.setText("");
text1.setText("失去焦点");
}
if(text==text2) {
text2.setText("");
text2.setText("失去焦点");
}
}
}