黑马程序员-day22综合练习

---------------------- android培训java培训、期待与您交流! ----------------------


出现了几个小问题,解决起来花了很长时间...自学就是麻烦


package day22;


import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class day22 {


/**
* @param args
*/
private static Frame frame = new Frame("MyFrame");
private static MenuBar mb = new MenuBar();
private static Menu fileMenu = new Menu("File");
private static Menu openMenu = new Menu("Open");
private static MenuItem openTxtMenuItem = new MenuItem("Open txt");
private static MenuItem saveMenuItem = new MenuItem("Save");
private static MenuItem quitMenuItem = new MenuItem("Quit");
private static TextArea ta = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
private static TextField tf = new TextField();
private static Dialog wrongDirectoryDialog = new Dialog(frame,"WrongDirectory",true);
private static Label lable = new Label();
private static Button button = new Button("OK");
private static File file = null;
private static boolean modi = false;
public static void main(String[] args) 
{
// TODO Auto-generated method stub
//鼠标的进入,双击,释放,单击
//显示输入目录下的文件
//模拟记事本的读取.保存
ui();
windowEvent();
textFieldEvent();
menuEvent();
}
public static void ui()
{
frame.setMenuBar(mb);
frame.setLayout(new BorderLayout());
mb.add(fileMenu);
fileMenu.add(openMenu);
openMenu.add(openTxtMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(quitMenuItem);
frame.add(tf,BorderLayout.NORTH);
frame.add(ta);
frame.setBounds(480,100,460,500);
frame.setVisible(true);
ta.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ket)
{
modi = false;
}
});
}
public static void dialogui()
{
wrongDirectoryDialog.setLayout(new BorderLayout());
wrongDirectoryDialog.add(lable,BorderLayout.NORTH);
wrongDirectoryDialog.add(button);
wrongDirectoryDialog.setBounds(600, 300, 300, 80);

button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent aeb)
{
wrongDirectoryDialog.setVisible(false);
}
});
wrongDirectoryDialog.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent wed)
{
wrongDirectoryDialog.setVisible(false);
}
});
}
public static void menuEvent()
{
quitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent aeq)
{
System.exit(0);
}
});
saveMenuItem.addActionListener(new ActionListener()
{
private BufferedWriter bw;


@SuppressWarnings("null")
public void actionPerformed(ActionEvent aes)
{
if(file != null)
{
try
{
bw = new BufferedWriter(new FileWriter(file));
bw.write(ta.getText());
}
catch(IOException ioe)
{
throw new RuntimeException("文件写入失败");
}
finally
{
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
FileDialog saveFD = new FileDialog(frame,"Save",FileDialog.SAVE);
saveFD.setVisible(true);
file = new File(saveFD.getDirectory(),saveFD.getFile());
try
{
bw = new BufferedWriter(new FileWriter(file));
bw.write(ta.getText());
}
catch(IOException ioe)
{
throw new RuntimeException("文件写入失败");
}
finally
{
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}
}
});
openTxtMenuItem.addActionListener(new ActionListener()
{
private BufferedReader br;

public void actionPerformed(ActionEvent aeo)
{
FileDialog openFD = new FileDialog(frame,"Open",FileDialog.LOAD);
openFD.setVisible(true);
file = new File(openFD.getDirectory(),openFD.getFile());
try
{
ta.setText("");
br = new BufferedReader(new FileReader(file));
String buffer = null;
while((buffer = br.readLine()) != null)
{
ta.append(buffer+"\r\n");
}
}
catch(IOException ioe)
{
throw new RuntimeException("文件读取失败");
}
finally
{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});

}
public static void windowEvent()
{
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent wef)
{
System.exit(0);
}
});
}
public static void textFieldEvent()
{
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ket)
{
if(ket.getKeyCode() == KeyEvent.VK_ENTER)
{
String dir = tf.getText();
File directory = new File(dir);
if (directory.isDirectory())
{
tf.setText("");
ta.setText("");
for(File f : directory.listFiles())
{
ta.append(f.getAbsolutePath()+"\r\n");
}
}
else
{
lable.setText("Wrong directory :"+dir);
dialogui();
wrongDirectoryDialog.setVisible(true);
}
}
}
});
}
}


---------------------- android培训java培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net/heima

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值