急急急!各位大虾!留步!java怎么给文本框添加FocusListener事件?为什么的代码编译成功了事件却联响应

Ram_ningbi 2009-11-18 11:11:27

//用到的数学符号 ∧ ∨ ⊕⊕㈩

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class sum7 {
static JFrame frm=new JFrame("四位加法器");

static TextField txt1=new TextField("加数Q1",4);
static TextField txt6=new TextField("加数Q2",4);
static TextField txt2=new TextField("",1);
static TextField txt3=new TextField("",1);
static TextField txt4=new TextField("",1);
static TextField txt5=new TextField("",1);
static TextField txt7=new TextField("",1);
static TextField txt8=new TextField("",1);
static TextField txt9=new TextField("",1);
static TextField txt10=new TextField("",1);
static TextField txt11=new TextField("",1);
static TextField txt12=new TextField("",1);
static TextField txt13=new TextField("",1);
static TextField txt14=new TextField("",1);
static TextField txt15=new TextField("",1);
static TextField txt16=new TextField("",1);
static TextField txt17=new TextField("",1);
static TextField txt18=new TextField("",1);
static TextField txt19=new TextField("",1);
static TextField txt20=new TextField("",1);
static TextField txt21=new TextField("",1);
static TextField txt22=new TextField("",1);
static TextArea txtr=new TextArea();

public static void main(String args[]){
frm.setSize(700,430);
frm.setLayout(null);
//frm.setOpaque(false);
frm.setBackground(Color.gray);

//以下设置文本框
txt1.setBounds(40,40,60,30);
txt6.setBounds(40,130,60,30);

txt2.setBounds(200,40,60,30);
txt2.setEditable(false);
txt3.setBounds(260,40,60,30);
txt3.setEditable(false);
txt4.setBounds(320,40,60,30);
txt4.setEditable(false);
txt5.setBounds(380,40,60,30);
txt5.setEditable(false);

txt7.setBounds(200,130,60,30);
txt7.setEditable(false);
txt8.setBounds(260,130,60,30);
txt8.setEditable(false);
txt9.setBounds(320,130,60,30);
txt9.setEditable(false);
txt10.setBounds(380,130,60,30);
txt10.setEditable(false);

txt11.setBounds(40,220,60,30);
txt11.setText("进位Ci");
txt11.setEditable(false);
txt12.setBounds(150,220,50,30);
txt12.setEditable(false);
txt13.setBounds(200,220,60,30);
txt13.setEditable(false);
txt14.setBounds(260,220,60,30);
txt14.setEditable(false);
txt15.setBounds(320,220,60,30);
txt15.setEditable(false);
txt16.setBounds(380,220,60,30);
txt16.setText("C0=0");
txt16.setEditable(false);

txt17.setBounds(40,310,60,30);
txt17.setEditable(false);
txt18.setBounds(150,310,50,30);
txt18.setEditable(false);
txt19.setBounds(200,310,60,30);
txt19.setEditable(false);
txt20.setBounds(260,310,60,30);
txt20.setEditable(false);
txt21.setBounds(320,310,60,30);
txt21.setEditable(false);
txt22.setBounds(380,310,60,30);
txt22.setEditable(false);
txtr.setBounds(460,40,230,310);
txtr.setEditable(false);

//以下添加文本框
frm.add(txt1);
frm.add(txt6);
Font font=new Font("楷体",Font.BOLD,16);
txt1.setFont(font);
txt6.setFont(font);
String str1=txt1.getText();
String str2=txt6.getText();
char ch1[]=str1.toCharArray();
char ch2[]=str2.toCharArray();

frm.add(txt2);
txt2.setFont(font);
frm.add(txt3);
txt3.setFont(font);
frm.add(txt4);
txt4.setFont(font);
frm.add(txt5);
txt5.setFont(font);
frm.add(txt7);
txt7.setFont(font);
frm.add(txt8);
txt8.setFont(font);
frm.add(txt9);
txt9.setFont(font);
frm.add(txt10);
txt10.setFont(font);
frm.add(txt11);
txt11.setFont(font);
frm.add(txt12);
txt12.setFont(font);
frm.add(txt13);
txt13.setFont(font);
frm.add(txt14);
txt14.setFont(font);
frm.add(txt15);
txt15.setFont(font);
frm.add(txt16);
txt16.setFont(font);
frm.add(txt17);
txt17.setFont(font);
frm.add(txt18);
txt18.setFont(font);
frm.add(txt19);
txt19.setFont(font);
frm.add(txt20);
txt20.setFont(font);
frm.add(txt21);
txt21.setFont(font);
frm.add(txt22);
txt22.setFont(font);
frm.add(txtr);
Font fon=new Font("楷体",Font.BOLD,20);
txtr.setFont(fon);



//以下为txt1事件 失败
txt1.addFocusListener(new FocusListener(){
public void FocusGained(FocusEvent e){
System.out.println("helloworld");
txt1.setText("");
}
public void FocusLost(FocusEvent e){
txt2.setText(txt1.getText());

}
});

//文本域换行尝试 成功
txtr.setText("计算过程如下:");
txtr.append("\n");
txtr.append("4323434㈩32∧12∨45");

System.out.println(txt1.getText());
frm.setResizable(false);
frm.setVisible(true);
}


}

/* class act extends sum7 implements FocusListener{

public void FocusGained(FocusEvent evt) {
txt1.setText("");

}
public void FocusLost(FocusEvent e){
String str1=txt1.getText();
System.out.println(str1);
}
}


/*
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}*/

...全文
207 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
gentalguo 高手
gentalguo 2009-11-19
  • 打赏
  • 举报
回复

txt1.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
System.out.println("helloworld");
txt1.setText("");
}

public void focusLost(FocusEvent e) {
txt2.setText(txt1.getText());

}
});


这样写,编译都过不去,如上,就是二楼的意见。
999朵玫瑰 2009-11-19
  • 打赏
  • 举报
回复
FocusListener
zhouyongyang 2009-11-19
  • 打赏
  • 举报
回复
方法名用大写有时候是会有错的
wsxing008 2009-11-19
  • 打赏
  • 举报
回复
对,就是把你匿名内部类的方法名改成小写试试
wsxing008 2009-11-19
  • 打赏
  • 举报
回复
你先测试下,用System。out。println(“”);看看有输出吗,就知道是哪原因了,你还有就是类继承JFreame,我现在好久没做了这了,都是B/S结构的,我给你个地址,你看看我以前做的看看有帮助吗
http://download.csdn.net/source/1040948
Ram_ningbi 2009-11-19
  • 打赏
  • 举报
回复
你们俩都答对了!我就没人给一般吧!谢谢诶!
阁楼上的伟哥 2009-11-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 crazylaa 的回复:]
Java code// 以下为txt1事件 失败 txt1.addFocusListener(new FocusListener() {publicvoid focusGained(FocusEvent e) {
System.out.println("helloworld");
txt1.setText("");
}publicvoid focusLost(FocusEvent e) {
txt2.setText(txt1.getText());

}
});

请将FocusGained/FocusLost的F大写改小写
[/Quote]
试试看
crazylaa 2009-11-18
  • 打赏
  • 举报
回复
		// 以下为txt1事件 失败
txt1.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
System.out.println("helloworld");
txt1.setText("");
}

public void focusLost(FocusEvent e) {
txt2.setText(txt1.getText());

}
});


请将FocusGained/FocusLost的F大写改小写

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧