/**
* 多文档界面
*/
package work;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextArea;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class J_DocumentText extends JFrame
{
private JDesktopPane m_desktop = new JDesktopPane();
private int m_count = 0;
public J_DocumentText()
{
super("多文档文本编辑器");
JMenuBar themMenuBar = new JMenuBar();
JMenu theMenuFile = new JMenu("File");
JMenuItem theMenuItem = new JMenuItem("New");
setJMenuBar(themMenuBar);
themMenuBar.add(theMenuFile);
theMenuFile.add(theMenuItem);
theMenuFile.setMnemonic('F');
theMenuItem.setMnemonic('N');
Container theContainer = getContentPane();
theContainer.add(m_desktop);
theMenuItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String File_name =JOptionPane.showInputDialog(m_desktop, "请输入文件名");
String s = "Document" + m_count;
m_count++;
JInternalFrame theInternalFrame= new JInternalFrame(s, true, true, true);
JMenuBar sMenuBar = newJMenuBar();
JButton sButton = newJButton("reserve");
theInternalFrame.setMenuBar(sMenuBar);
sMenuBar.add(sButton);
J_TextArea theTextArea = newJ_TextArea(File_name);
Container c =theInternalFrame.getContentPane();
c.setLayout(new BorderLayout());
c.add(theTextArea,BorderLayout.CENTER);
theInternalFrame.pack();
sButton.addActionListener(newActionListener()
{
@Override
public voidactionPerformed(ActionEvent ae)
{
try
{
FileReader fin =new FileReader(File_name);
BufferedReader dfin= new BufferedReader(fin);
FileWriter fout =new FileWriter(File_name, false);
BufferedWriterdfout = new BufferedWriter(fout);
dfout.write(theTextArea.getText());
dfout.flush();
dfout.close();
} catch (Exception te)
{
System.err.println("发生异常");
te.printStackTrace();
}
}
}
);
m_desktop.add(theInternalFrame);
theInternalFrame.setVisible(true);
}
}
);
}
public static void main(String[] args)
{
JFrame app = new J_DocumentText();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(450, 250);
app.setVisible(true);
}
}