黑马程序员技术交流社区
标题:
代码异常求解
[打印本页]
作者:
yang649981273
时间:
2014-8-17 19:21
标题:
代码异常求解
这段代码在运行空格清空
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyWindowDemo {
private Frame f;
private Button but;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okbut;
MyWindowDemo(){
into();
}
public void into(){
f=new Frame("my frame");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
but=new Button("转到");
tf=new TextField(50);
ta=new TextArea(25,65);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_SPACE){
tf.setText("");
ta.setText("");
}
}
});
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
});
but.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
showDir();
}
});
}
private void showDir(){
String dirName=tf.getText();
File dir=new File(dirName);
if(dir.exists()&&dir.isDirectory()){
ta.setText("");
String[] Names=dir.list();
for(String name:Names){
ta.append(name+"\r"+"\n");
}
}
else{
OKexce();
String into = "您输入的信息是错误的"+dirName+"请重新输入";
lab.setText(into);
d.setVisible(true);
}
}
void OKexce(){
d=new Dialog(f,"提示信息-self",true);
d.setLayout(new FlowLayout());
d.setBounds(400,200,240,150);
lab=new Label();
okbut=new Button("OK");
d.add(lab);
d.add(okbut);
d.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
});
okbut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setVisible(false);
}
});
okbut.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER);
d.setVisible(false);
}
});
}
public static void main(String[] args) {
new MyWindowDemo();
}
}
复制代码
的时候老是空一格字符要怎样吧那个字符除去!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2