파일 작업 에 대한 FileDialog 대화 상자
3136 단어 자바
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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;
import javax.swing.JFrame;
public class FileDialogs extends JFrame{
Menu menu;
MenuBar mbar;
MenuItem open,load;
File file;
FileReader fread;
BufferedReader breader;
FileWriter fwiter;
BufferedWriter bwiter;
TextArea area;
FileDialog log_save,log_open;
FileDialogs()
{
menu=new Menu(" ");
mbar=new MenuBar();
load=new MenuItem("load");
open=new MenuItem("open");
menu.add(load);
menu.add(open);
mbar.add(menu);
log_save=new FileDialog(this, " ",FileDialog.SAVE);
log_open=new FileDialog(this," ",FileDialog.LOAD);
setMenuBar(mbar);
setBounds(100,100,500,500);
setLayout(new FlowLayout());
setVisible(true);
validate();
area=new TextArea();
add(area);
log_save.addWindowListener(new WindowAdapter()
{@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
log_save.setVisible(false);
super.windowClosing(arg0);
}
});
log_open.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
log_open.setVisible(false);
super.windowClosing(e);
}
});
open.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
log_open.setVisible(true);
area.setText(null);
String s=null;
if(log_open.getFile()!=null)
{
File file=new File(log_open.getDirectory(),log_open.getFile());
try {
fread=new FileReader(file);
breader=new BufferedReader(fread);
while((s=breader.readLine())!=null)
{
area.append(s+"
");
}
breader.close();
fread.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
load.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
log_save.setVisible(true);
String s=null;
File file = new File(log_save.getDirectory(),log_save.getFile());
try {
fwiter=new FileWriter(file);
bwiter=new BufferedWriter(fwiter);
s=area.getText();
bwiter.write(area.getText(),0,(area.getText().length()));
bwiter.close();
fwiter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.