把所有代码贴上来吧..
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);
button.setSize(8, 3);
wrongDirectoryDialog.add(button);
wrongDirectoryDialog.setBounds(600, 300, 300, 80);
wrongDirectoryDialog.setVisible(true);
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();
}
}
}
});
}
}
|