2个回答
展开全部
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
public class MainClass extends JFrame implements ActionListener{
int width = 500,height = 400;
JPanel panel;
JMenuBar bar;
JMenu fileMenu,editMenu,helpMenu;
JMenuItem 打开O,新建N,保存S,另存A,剪切T,复制C,粘贴P,关于A;
JTextArea textArea = null;
File tempFile = null;
public MainClass(){ //构造方法
setTitle("TextEdit");
setSize(width,height);
panel = (JPanel)getContentPane();
bar = new JMenuBar();
fileMenu = new JMenu("文件F");
fileMenu.setMnemonic('F');
editMenu = new JMenu("编辑E");
editMenu.setMnemonic('E');
helpMenu = new JMenu("帮助H");
helpMenu.setMnemonic('H');
打开O = new JMenuItem("打开O");
打开O.setMnemonic('O');
新建N = new JMenuItem("新建N");
新建N.setMnemonic('N');
保存S = new JMenuItem("保存S");
保存S.setMnemonic('S');
另存A = new JMenuItem("另存A");
另存A.setMnemonic('A');
剪切T = new JMenuItem("剪切C");
剪切T.setMnemonic('t');
复制C = new JMenuItem("复制C");
复制C.setMnemonic('C');
粘贴P = new JMenuItem("粘贴P");
粘贴P.setMnemonic('P');
关于A = new JMenuItem("关于A");
关于A.setMnemonic('A');
fileMenu.add(打开O);
fileMenu.add(新建N);
fileMenu.add(保存S);
fileMenu.add(另存A);
bar.add(fileMenu);
editMenu.add(剪切T);
editMenu.add(复制C);
editMenu.add(粘贴P);
bar.add(editMenu);
helpMenu.add(关于A);
bar.add(helpMenu);
textArea = new JTextArea();
panel.add("North",bar);
panel.add("Center", textArea);
打开O.addActionListener(this);
新建N.addActionListener(this);
保存S.addActionListener(this);
另存A.addActionListener(this);
剪切T.addActionListener(this);
复制C.addActionListener(this);
粘贴P.addActionListener(this);
关于A.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent event){ //处理事件
if(event.getSource() == 打开O){ //处理打开
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(panel);
tempFile = jfc.getSelectedFile();
FileReader fis;
try {
fis = new FileReader(tempFile);
textArea.read(fis,null);
textArea.setEditable(true);
}catch(Exception ex){ex.printStackTrace();}
}
if(event.getSource() == 新建N){ //处理新建
textArea.setEditable(true);
textArea.setText(null);
}
if(event.getSource() == 保存S){ //处理保存
if(tempFile == null){
JFileChooser jfc = new JFileChooser();
jfc.showSaveDialog(panel);
tempFile = jfc.getSelectedFile();
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
else{
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
}
if(event.getSource() == 另存A){ //处理另存
JFileChooser jfc = new JFileChooser();
jfc.showSaveDialog(panel);
tempFile = jfc.getSelectedFile();
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
if(event.getSource() == 剪切T){ //处理剪切
textArea.cut();
}
if(event.getSource() == 复制C){ //处理复制
textArea.copy();
}
if(event.getSource() == 粘贴P){ //处理粘贴
textArea.paste();
}
if(event.getSource() == 关于A){ //处理关于
textArea.setText("Manifest-Version: 1.0\n" +
"Created-By: Libra_JL\n" +
"QQ: 254791521\n");
textArea.setEditable(false);
panel.validate();
validate();
}
}
public static void main(String []args){ //主函数
new MainClass();
}
}
import java.awt.event.*;
import javax.swing.*;
public class MainClass extends JFrame implements ActionListener{
int width = 500,height = 400;
JPanel panel;
JMenuBar bar;
JMenu fileMenu,editMenu,helpMenu;
JMenuItem 打开O,新建N,保存S,另存A,剪切T,复制C,粘贴P,关于A;
JTextArea textArea = null;
File tempFile = null;
public MainClass(){ //构造方法
setTitle("TextEdit");
setSize(width,height);
panel = (JPanel)getContentPane();
bar = new JMenuBar();
fileMenu = new JMenu("文件F");
fileMenu.setMnemonic('F');
editMenu = new JMenu("编辑E");
editMenu.setMnemonic('E');
helpMenu = new JMenu("帮助H");
helpMenu.setMnemonic('H');
打开O = new JMenuItem("打开O");
打开O.setMnemonic('O');
新建N = new JMenuItem("新建N");
新建N.setMnemonic('N');
保存S = new JMenuItem("保存S");
保存S.setMnemonic('S');
另存A = new JMenuItem("另存A");
另存A.setMnemonic('A');
剪切T = new JMenuItem("剪切C");
剪切T.setMnemonic('t');
复制C = new JMenuItem("复制C");
复制C.setMnemonic('C');
粘贴P = new JMenuItem("粘贴P");
粘贴P.setMnemonic('P');
关于A = new JMenuItem("关于A");
关于A.setMnemonic('A');
fileMenu.add(打开O);
fileMenu.add(新建N);
fileMenu.add(保存S);
fileMenu.add(另存A);
bar.add(fileMenu);
editMenu.add(剪切T);
editMenu.add(复制C);
editMenu.add(粘贴P);
bar.add(editMenu);
helpMenu.add(关于A);
bar.add(helpMenu);
textArea = new JTextArea();
panel.add("North",bar);
panel.add("Center", textArea);
打开O.addActionListener(this);
新建N.addActionListener(this);
保存S.addActionListener(this);
另存A.addActionListener(this);
剪切T.addActionListener(this);
复制C.addActionListener(this);
粘贴P.addActionListener(this);
关于A.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent event){ //处理事件
if(event.getSource() == 打开O){ //处理打开
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(panel);
tempFile = jfc.getSelectedFile();
FileReader fis;
try {
fis = new FileReader(tempFile);
textArea.read(fis,null);
textArea.setEditable(true);
}catch(Exception ex){ex.printStackTrace();}
}
if(event.getSource() == 新建N){ //处理新建
textArea.setEditable(true);
textArea.setText(null);
}
if(event.getSource() == 保存S){ //处理保存
if(tempFile == null){
JFileChooser jfc = new JFileChooser();
jfc.showSaveDialog(panel);
tempFile = jfc.getSelectedFile();
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
else{
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
}
if(event.getSource() == 另存A){ //处理另存
JFileChooser jfc = new JFileChooser();
jfc.showSaveDialog(panel);
tempFile = jfc.getSelectedFile();
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
if(event.getSource() == 剪切T){ //处理剪切
textArea.cut();
}
if(event.getSource() == 复制C){ //处理复制
textArea.copy();
}
if(event.getSource() == 粘贴P){ //处理粘贴
textArea.paste();
}
if(event.getSource() == 关于A){ //处理关于
textArea.setText("Manifest-Version: 1.0\n" +
"Created-By: Libra_JL\n" +
"QQ: 254791521\n");
textArea.setEditable(false);
panel.validate();
validate();
}
}
public static void main(String []args){ //主函数
new MainClass();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询