Ⅰ 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("失去焦點");
}
}
}