導航:首頁 > 編程語言 > 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窗體獲得焦點相關的資料

熱點內容
計算機網路技術與應用視頻 瀏覽:924
linux裝apache 瀏覽:272
手機谷歌打不開網站怎麼辦 瀏覽:110
燈控台編程好了怎麼使用 瀏覽:342
如何用金山毒霸切斷網路連接 瀏覽:873
怎麼設置文件打開密碼怎麼設置 瀏覽:5
網路小說用什麼寫 瀏覽:956
擴展名為rm的文件如何打開 瀏覽:707
導入android項目沒有rjava 瀏覽:899
帶英文和數字的文件名 瀏覽:843
黃山數控編程培訓在哪裡 瀏覽:111
win10俠盜5無限讀取 瀏覽:557
js工廠模式 瀏覽:927
iphone6s清理緩存 瀏覽:465
ug編程怎麼選不上字體 瀏覽:435
pc文件加密哪個好 瀏覽:393
蘋果軟體更新4位密碼 瀏覽:96
手機如何將文件弄成文件夾 瀏覽:275
swordigo第四關 瀏覽:996
t420win10網卡驅動 瀏覽:805

友情鏈接