这段代码的问题是:
1.一旦文本框中的文本发生变化时,就会立即弹出对话框请用户确认修改,而不是在用户离开文本框时才弹出对话框!!!
2.如果用户点击弹出的确认对话框中的"否"按钮文本框中文字还原时,确认对话框却不会消失,只有点击"是"时,对话框才能消失!我估计肯定是第64行tf.setText("请在此处进行输入和修改!");}对文本框中文字进行还原时文本框中文字发生了变化,触发了TextListener!
以上两处错误,不知哪位高手能帮我指点一二,一定高分奉送!!!
1.一旦文本框中的文本发生变化时,就会立即弹出对话框请用户确认修改,而不是在用户离开文本框时才弹出对话框!!!
2.如果用户点击弹出的确认对话框中的"否"按钮文本框中文字还原时,确认对话框却不会消失,只有点击"是"时,对话框才能消失!我估计肯定是第64行tf.setText("请在此处进行输入和修改!");}对文本框中文字进行还原时文本框中文字发生了变化,触发了TextListener!
以上两处错误,不知哪位高手能帮我指点一二,一定高分奉送!!!
下面是我修改过后的程序:
import java.awt.*;
import java.awt.event.*;public class TextEditFrame
{
public static void main(String args[])
{
MyTextEditFrame TE_frame=new MyTextEditFrame();
}
}class MyTextEditFrame extends Frame
implements ActionListener,TextListener,FocusListener
{
MenuBar m_MenuBar;
Menu menuFile;
MenuItem mi_File_Exit;
Dialog MegDlg;
Button btnY,btnN,btn;
boolean modified;
TextField tf=new TextField("请在此处进行输入和修改!",50);
MyTextEditFrame()
{
super("文本编辑框");
setLayout(new FlowLayout());
this.addWindowListener(new WinAdpt());//Frame响应窗口关闭事件
m_MenuBar=new MenuBar();
menuFile=new Menu("文件");
mi_File_Exit=new MenuItem("退出",new MenuShortcut('X'));
mi_File_Exit.addActionListener(this);
menuFile.add(mi_File_Exit);
m_MenuBar.add(menuFile);
this.setMenuBar(m_MenuBar);
add(tf);
tf.addTextListener(this);
// tf.addActionPerformed(this);
tf.addFocusListener(this);
btnY=new Button("是");
btnN=new Button("否");
btn=new Button("OK");
btnY.addActionListener(this);
btnN.addActionListener(this);
add(btn);
setSize(350,150);
show();
}
public void textValueChanged(TextEvent e)
{
if(e.getSource()==tf)
modified=true;
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="是")
MegDlg.dispose();
else if(e.getActionCommand()=="否")
{ MegDlg.dispose();
tf.setText("请在此处进行输入和修改!");}
else if(e.getActionCommand()=="退出")
{dispose();
System.exit(0);}
}
public void focusLost(FocusEvent e)
{
if(e.getSource()==tf)
if(modified)
{MegDlg=new Dialog(this,"确认修改",true);
Panel p1=new Panel();
p1.add(new Label("是否保留对文本框的修改?"));
MegDlg.add("Center",p1);
Panel p2=new Panel();
p2.add(btnY);
p2.add(btnN);
MegDlg.add("South",p2);
MegDlg.setSize(200,100);
MegDlg.show();}
}
public void focusGained(FocusEvent e){}
} class WinAdpt extends WindowAdapter//创建窗口剪裁类子类,处理窗口关闭事件
{
public void windowClosing(WindowEvent e)
{
((Frame)e.getWindow()).dispose();//获得引发事件的窗口并关闭之
System.exit(0);
}
}