刚才写了个程序,楼主看看符合要求不
import javaxswing;
import javaxswingborder;
import javaawt;
import javaawtevent;
class TestFocus extends JFrame implements FocusListener {
JButton btn;
JTextArea text1,text2;
boolean b=true;
public TestFocus() {
btn=new JButton("切换焦点");
btnaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(b) {
//text2setRequetFocusEnabled(fasle);
text1grabFocus();
b=false;
}
else{
text2grabFocus();
b=true;
}
}
});
JPanel p1=new JPanel(new FlowLayout());
p1add(BoxcreateGlue());
p1add(btn);
p1add(BoxcreateGlue());
JPanel p2=new JPanel(new FlowLayout());
text1=new JTextArea();
text2=new JTextArea();
p2add(create(text1,"第一个文本域"));
p2add(create(text2,"第二个文本域"));
text1addFocusListener(this);
text2addFocusListener(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) {
textsetLineWrap(true);
JScrollPane jsp=new JScrollPane(text);
Border border=BorderFactorycreateLineBorder(Colorblue);
jspsetBorder(BorderFactorycreateTitledBorder(border,s,TitledBorderLEFT,TitledBorderTOP));
return jsp;
}
public void focusGained(FocusEvent e) {
JTextArea text=(JTextArea)egetSource();
if(text==text1) {
text1setText("");
text1setText("获得焦点");
}
if(text==text2) {
text2setText("");
text2setText("获得焦点");
}
}
public void focusLost(FocusEvent e) {
JTextArea text=(JTextArea)egetSource();
if(text==text1) {
text1setText("");
text1setText("失去焦点");
}
if(text==text2) {
text2setText("");
text2setText("失去焦点");
}
}
}
用WindowFocusListener可以实现:
import javaawt;
import javaxswing;
import javaawtevent;
public class Test extends JFrame
{
public static void main(String[] args)
{
new Test()setVisible(true);
}
public Test()
{
setSize(300,300);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
addWindowFocusListener(new WindowFocusListener()
{
public void windowGainedFocus(WindowEvent e)
{
setTitle("窗体获得焦点");
}
public void windowLostFocus(WindowEvent e)
{
setTitle("窗体失去焦点");
}
});
}
}
以上就是关于高手支招 java FocusListener焦点事件如何实现在两个JTextField中确认焦点!全部的内容,包括:高手支招 java FocusListener焦点事件如何实现在两个JTextField中确认焦点!、急!!!!!!!编写一个java程序,在程序中建立一个窗口, *** 作窗口"失去焦点"与"得到焦点"的事、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)