该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package test526;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintStream;
public class test3 extends JFrame implements ActionListener{
JMenuBar cd; //菜单列最优先
JMenu cd1,cd2,cd3,cd4,cd5,cd6;//菜单含下级菜单
JMenuItem cdx2,cdx3,cdx4,cdx5,cdx6,cdx7;
JMenu ej; JMenuItem ej2,ej3;
JToolBar gjt;
JButton an1,an2,an3,an4,an5,an6;
JTextArea wby;
JScrollPane gdt;
public static void main(String[] args){
test3 aa=new test3();
}
public test3(){
gjt=new JToolBar();
an1=new JButton(new ImageIcon("tp/xj.jpg"));
an1.setToolTipText("新建");
an2=new JButton(new ImageIcon("tp/wj.jpg"));
an2.setToolTipText("打开文件");
an2.addActionListener(this);
an2.setActionCommand("open"); //传参
an3=new JButton(new ImageIcon("tp/bc.jpg"));
an3.setToolTipText("保存");
an3.addActionListener(this);
an3.setActionCommand("save"); //传参
an4=new JButton(new ImageIcon("tp/jq.jpg"));
an4.setToolTipText("剪切");
an5=new JButton(new ImageIcon("tp/fz.jpg"));
an5.setToolTipText("复制");
an6=new JButton(new ImageIcon("tp/nt.jpg"));
an6.setToolTipText("黏贴");
cd=new JMenuBar();
cd1=new JMenu("文件(F)");
cd1.setMnemonic('F');
cd2=new JMenu("编辑(E)");
cd2.setMnemonic('E');
cd3=new JMenu("格式(O)");
cd3.setMnemonic('O');
cd4=new JMenu("查看(V)");
cd4.setMnemonic('V');
cd5=new JMenu("帮助(H)");
cd5.setMnemonic('H');
cd6=new JMenu("施蔓专用(K)");
cd6.setMnemonic('K');
ej=new JMenu("新建");
ej2=new JMenuItem("文件");
ej3=new JMenuItem("模板");
cdx2=new JMenuItem("打开");
cdx2.addActionListener(this);
cdx2.setActionCommand("open"); //传参
cdx3=new JMenuItem("保存");
cdx3.addActionListener(this);
cdx3.setActionCommand("save"); //传参
cdx4=new JMenuItem("另存为");
cdx5=new JMenuItem("打印");
cdx6=new JMenuItem("退出");
cdx7=new JMenuItem("我爱你施蔓",new ImageIcon("tp/QQ图片20150526134828.png"));
wby=new JTextArea();
gdt=new JScrollPane(wby);
gjt.add(an1);gjt.add(an2);gjt.add(an3);
gjt.add(an4);gjt.add(an5);gjt.add(an6);
ej.add(ej2);ej.add(ej3);
cd1.add(ej);cd1.add(cdx2);cd1.add(cdx3);
cd1.add(cdx4);cd1.add(cdx5);cd1.add(cdx6);
cd.add(cd1);cd.add(cd2);cd.add(cd3);
cd.add(cd4);cd.add(cd5);cd.add(cd6);
cd6.add(cdx7);
this.setJMenuBar(cd);
this.add(gjt,BorderLayout.NORTH);
this.add(gdt);
this.setTitle("蔓蔓专用版记事本");
this.setIconImage((new ImageIcon("tp/IMG_1645.JPG")).getImage());
this.setSize(500,600);//界面大小(setSize 语法)
this.setLocation(100,100);//界面位置(setLocation语法)
//this.setResizable(false);//(不可调整界面大小)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("open"))
{JFileChooser wjxz=new JFileChooser();
wjxz.setDialogTitle("文件打开");
wjxz.showOpenDialog(null);
wjxz.setVisible(true);
String wjlj=wjxz.getSelectedFile().getAbsolutePath();
//这行的作用是 得到用户选择的全路径
//System.out.println(wjlj);
FileReader wjl=null; BufferedReader hcl=null;
try
{
wjl=new FileReader(wjlj);
hcl=new BufferedReader(wjl);
String s="",zfc="";
while((s=hcl.readLine())!=null)
{
zfc+=(s+"\n");
}
wby.setText(zfc);
}
catch(Exception aa){}
finally
{
try
{
wjl.close();
hcl.close();
}
catch (Exception e1) {}
}
}
else if(e.getActionCommand().equals("save"))
{
//System.out.println("保存");
JFileChooser ljxz=new JFileChooser();
ljxz.setDialogTitle("另存为");
ljxz.showSaveDialog(null);
ljxz.setVisible(true);
String bclj=ljxz.getSelectedFile().getAbsolutePath();
try
{
PrintStream pl = new PrintStream(bclj);
System.setOut(pl);
System.out.println(this.wby.getText());
}
catch(Exception aa){}
}
}
}