import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestText{
public TestText(){
init();
}
private void init(){
JFrame frame = new JFrame("TestText");
JPanel panel = new JPanel();
JTextField text = new JTextField(12);
JLabel label = new JLabel("");
JButton la = new JButton("kao");
text.addFocusListener(new MonitorLostFocus(this));
panel.add(text);
panel.add(label);
panel.add(la);
panel.setLayout(new GridLayout(3,1));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public void respondLostFocus(){
String temp = new String("");
temp = text.getText();
label.setText(temp);
}
public static void main(String args[]){
new TestText();
}
}class MonitorLostFocus extends MouseAdapter{
TestText test;
public MonitorLostFocus(TestText superTest){
test = superTest;
}
public void focusLost(MouseEvent e){
test.respondLostFocus();
}
}错误信息:
F:\java\test\TestText.java:17: addFocusListener(java.awt.event.FocusListener) in java.awt.Component cannot be applied to (MonitorLostFocus)
text.addFocusListener(new MonitorLostFocus(this));
^
F:\java\test\TestText.java:28: cannot resolve symbol
symbol : variable text
location: class TestText
temp = text.getText();
^
F:\java\test\TestText.java:29: cannot resolve symbol
symbol : variable label
location: class TestText
label.setText(temp);
^
3 errorsProcess completed.
import java.awt.event.*;
import javax.swing.*;
public class TestText{
public TestText(){
init();
}
private void init(){
JFrame frame = new JFrame("TestText");
JPanel panel = new JPanel();
JTextField text = new JTextField(12);
JLabel label = new JLabel("");
JButton la = new JButton("kao");
text.addFocusListener(new MonitorLostFocus(this));
panel.add(text);
panel.add(label);
panel.add(la);
panel.setLayout(new GridLayout(3,1));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public void respondLostFocus(){
String temp = new String("");
temp = text.getText();
label.setText(temp);
}
public static void main(String args[]){
new TestText();
}
}class MonitorLostFocus extends MouseAdapter{
TestText test;
public MonitorLostFocus(TestText superTest){
test = superTest;
}
public void focusLost(MouseEvent e){
test.respondLostFocus();
}
}错误信息:
F:\java\test\TestText.java:17: addFocusListener(java.awt.event.FocusListener) in java.awt.Component cannot be applied to (MonitorLostFocus)
text.addFocusListener(new MonitorLostFocus(this));
^
F:\java\test\TestText.java:28: cannot resolve symbol
symbol : variable text
location: class TestText
temp = text.getText();
^
F:\java\test\TestText.java:29: cannot resolve symbol
symbol : variable label
location: class TestText
label.setText(temp);
^
3 errorsProcess completed.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestText{
public TestText(){
init();
}
JTextField text = new JTextField(12);//拿出来
JLabel label = new JLabel("");
private void init(){
JFrame frame = new JFrame("TestText");
JPanel panel = new JPanel(); panel.add(text);
panel.add(label);
panel.setLayout(new GridLayout(2,1));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public void respondLostFocus(){
String temp = new String("");
temp = text.getText();
label.setText(temp);
} public static void main(String args[]){
new TestText();
}
}class MonitorLostFocus extends MouseAdapter{
TestText test;
public MonitorLostFocus(TestText superTest){
test = superTest;
}
public void focusLost(MouseEvent e){
test.respondLostFocus();
}
}
你怎么可以在respondLostFocus()里引用init()中的私有变量text和lable呢
import java.awt.event.*;
import javax.swing.*;
public class TestText{
JFrame frame;
JPanel panel;
JTextField text;
JLabel label;
public TestText(){
init();
}
private void init(){
frame = new JFrame("TestText");
panel = new JPanel();
text = new JTextField(12);
label = new JLabel("");
panel.add(text);
panel.add(label);
panel.setLayout(new GridLayout(2,1));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public void respondLostFocus(){
String temp = new String("");
temp = text.getText();
label.setText(temp);
}
public static void main(String args[]){
new TestText();
}
}class MonitorLostFocus extends MouseAdapter{
TestText test;
public MonitorLostFocus(TestText superTest){
test = superTest;
}
public void focusLost(MouseEvent e){
test.respondLostFocus();
}
}将组件对象放在类中定义,不要放在init方法中定义
import java.awt.event.*;
import javax.swing.*;
public class TestText{
public JTextField text; //这样就可以在respondLostFocus()中调用了
public JLabel label;
public TestText(){
init();
}
private void init(){
JFrame frame = new JFrame("TestText");
JPanel panel = new JPanel();
text = new JTextField(12);
label = new JLabel("");
panel.add(text);
panel.add(label);
panel.setLayout(new GridLayout(2,1));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public void respondLostFocus(){
String temp= new String("");
temp = text.getText();
label.setText(temp);
}
public static void main(String args[]){
new TestText();
}
}class MonitorLostFocus extends MouseAdapter{
TestText test;
public MonitorLostFocus(TestText superTest){
test = superTest;
}
public void focusLost(MouseEvent e){
test.respondLostFocus();
}
}
import java.awt.event.*;
import javax.swing.*;
public class TestText{
private JFrame frame;
private JPanel panel;
private JTextField text;
private JLabel label;
private JButton la;
public TestText(){
init();
}
private void init(){
frame = new JFrame("TestText");
panel = new JPanel();
text = new JTextField(12);
label = new JLabel("");
la = new JButton("kao");
text.addFocusListener(new MonitorLostFocus(this));
panel.add(text);
panel.add(label);
panel.add(la);
panel.setLayout(new GridLayout(3,1));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public void respondLostFocus(){
String temp = new String("");
temp = text.getText();
label.setText(temp);
}
public static void main(String args[]){
new TestText();
}
}class MonitorLostFocus extends MouseAdapter{
TestText test;
public MonitorLostFocus(TestText superTest){
test = superTest;
}
public void focusLost(MouseEvent e){
test.respondLostFocus();
}
}
另外那个错误比较复杂了!
* Created on 2004-12-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test;/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestText extends Frame{
JTextField text = new JTextField(12);
JLabel label = new JLabel("");public TestText(){
init();
}
public void init(){
JFrame frame = new JFrame("TestText");
JPanel panel = new JPanel();
panel.add(text);
panel.add(label);
panel.setLayout(new GridLayout(2,1));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public void respondLostFocus(){
String temp = new String("");
temp = text.getText();
label.setText(temp);
}
public static void main(String args[]){
new TestText();
}
}
import java.awt.event.*;
import javax.swing.*;
public class TestText{
JFrame frame;
JPanel panel;
JTextField text;
JLabel label;
JButton la;
public TestText(){
init();
}
private void init(){
frame = new JFrame("TestText");
panel = new JPanel();
text = new JTextField(12);
label = new JLabel("");
la = new JButton("kao");
text.addFocusListener(new MonitorLostFocus(this));
label.setText("kao");
panel.add(text);
panel.add(label);
panel.add(la);
panel.setLayout(new GridLayout(3,1));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public void respondLostFocus(){
System.out.println("hjksdhfkjd");
String temp = new String("");
temp = text.getText();
label.setText(temp);
} //respondLostFocus1()
public static void main(String args[]){
new TestText();
}
}class MonitorLostFocus extends FocusAdapter{
TestText test;
public MonitorLostFocus(TestText superTest){
test = superTest;
}
public void focusLost(FocusAdapter e){
test.respondLostFocus();
}
}现在已经通过编译,可是失去焦点事件没用!!!怎么回事啊