導航:首頁 > 編程語言 > java窗體獲得焦點

java窗體獲得焦點

發布時間:2023-08-24 18:35:21

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

閱讀全文

與java窗體獲得焦點相關的資料

熱點內容
cad看圖大師下載的文件在哪 瀏覽:227
硬碟損壞內部數據怎麼修 瀏覽:880
微信你真列害圖片大全 瀏覽:49
jsin類型可以傳數值嗎 瀏覽:532
win10文件從左邊滑出 瀏覽:53
win10關機後桌面文件丟失 瀏覽:266
watch卸載自帶應用程序 瀏覽:292
有什麼plc自學網站 瀏覽:219
176純金幣假人版本 瀏覽:334
紅米note4微信計步代碼 瀏覽:259
站長之家有app嗎 瀏覽:912
office應用視頻教程 瀏覽:690
html資料庫有哪些 瀏覽:489
鋼筆工具線不會變細 瀏覽:762
access2010資料庫上機 瀏覽:686
flstudio錄音教程 瀏覽:457
圖形編程軟體哪個好用 瀏覽:393
要怎麼重新下載而且不保存數據 瀏覽:188
手機軟體下載後文件在哪 瀏覽:515
日版蘋果6黑解 瀏覽:799

友情鏈接