实现文件创建,打开,简单界面设置
恶搞一下,下面进入正题,show you the code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;
import java.awt.event.WindowAdapter ;
import javax.swing.JLabel ;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class MyMenu {
private JFrame frame = null ;
private String fileIcon = "d:"+File.separator+"icon"+File.separator+"file.png" ;
private String newIcon = "d:"+File.separator+"icon"+File.separator+"new.png" ;
private String openIcon = "d:"+File.separator+"icon"+File.separator+"open.png" ;
private String helpIcon = "d:"+File.separator+"icon"+File.separator+"help.png" ;
private String saveIcon = "d:"+File.separator+"icon"+File.separator+"save.png" ;
/*
* 设置字体图标路径
*/
private String fontIcon = "d:"+File.separator+"icon"+File.separator+"font.png" ;
private String yaIcon = "d:"+File.separator+"icon"+File.separator+"yahei.png" ;
private String kaiIcon = "d:"+File.separator+"icon"+File.separator+"kaiti.png" ;
private String songIcon = "d:"+File.separator+"icon"+File.separator+"songti.png" ;
/*
* 设置背景图标路径
*/
private String bIcon = "d:"+File.separator+"icon"+File.separator+"background.png" ;
private String yellowIcon = "d:"+File.separator+"icon"+File.separator+"yellow.png" ;
private String orangeIcon = "d:"+File.separator+"icon"+File.separator+"orange.png" ;
private String greenIcon = "d:"+File.separator+"icon"+File.separator+"green.png" ;
private String blackIcon = "d:"+File.separator+"icon"+File.separator+"black.png" ;
private String whiteIcon = "d:"+File.separator+"icon"+File.separator+"white.png" ;
private JTextArea text = null ;
public MyMenu() {
this.frame = new JFrame("手撸版本记事本") ;
this.text = new JTextArea(20,20) ;
Container con = this.frame.getContentPane() ;
con.add(new JScrollPane(text)) ;
//con.add(text) ;
JMenu file = new JMenu("文件") ;
file.setIcon(new ImageIcon(fileIcon)) ;
JMenu help = new JMenu("帮助") ;
help.setIcon(new ImageIcon(helpIcon)) ;
JMenu font = new JMenu("字体") ;
font.setIcon(new ImageIcon(fontIcon));
JMenuBar menubar = new JMenuBar() ;
menubar.add(file) ;
menubar.add(help) ;
menubar.add(font) ;
//设置字体按钮以及响应事件
JMenuItem songti = new JMenuItem("宋体",new ImageIcon(songIcon)) ;
JMenuItem kaiti = new JMenuItem("楷体",new ImageIcon(kaiIcon)) ;
JMenuItem yahei = new JMenuItem("雅黑",new ImageIcon(yaIcon)) ;
font.add(songti) ;
font.add(kaiti) ;
font.add(yahei) ;
kaiti.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font f = new Font("楷体",Font.BOLD,15) ;
text.setFont(f);
}
}) ;
songti.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font f = new Font("新宋体",Font.PLAIN,15) ;
text.setFont(f);
}
}) ;
yahei.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font f = new Font("微软雅黑",Font.BOLD,15) ;
text.setFont(f);
}
});
/*设置背景颜色
* 及其响应事件
*/
JMenu bcolor = new JMenu("背景色") ;
bcolor.setIcon(new ImageIcon(bIcon)) ;
JMenuItem byellow =new JMenuItem("黄色",new ImageIcon(yellowIcon)) ;
JMenuItem borange = new JMenuItem("橙色",new ImageIcon(orangeIcon)) ;
JMenuItem bgreen = new JMenuItem("绿色",new ImageIcon(greenIcon)) ;
JMenuItem bblack = new JMenuItem("黑色",new ImageIcon(blackIcon)) ;
JMenuItem bwhite = new JMenuItem("白色",new ImageIcon(whiteIcon)) ;
menubar.add(bcolor) ;
bcolor.add(bwhite) ;
bcolor.add(byellow) ;
bcolor.add(borange) ;
bcolor.add(bgreen) ;
bcolor.add(bblack) ;
byellow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setBackground(Color.yellow);
}
}) ;
borange.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setBackground(Color.orange);
}
}) ;
bgreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setBackground(Color.green);
}
}) ;
bblack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setBackground(Color.black);
JOptionPane.showMessageDialog(null, "什么都看不见了吧","逗比",JOptionPane.ERROR_MESSAGE);
}
}) ;
bwhite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setBackground(Color.white);
JOptionPane.showMessageDialog(null, "你一定是刚才设置成黑色背景了","逗比",JOptionPane.WARNING_MESSAGE);
}
});
/*接下来开始设置file按钮
*
*/
JMenuItem openItem = new JMenuItem("打开",new ImageIcon(openIcon)) ;
JMenuItem newItem = new JMenuItem("新建",new ImageIcon(newIcon)) ;
JMenuItem saveItem = new JMenuItem("保存",new ImageIcon(saveIcon)) ;
JMenuItem helpItem = new JMenuItem("联机帮助",new ImageIcon(helpIcon)) ;
openItem.addActionListener(new ActionListener() { //打开文件选项
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser() ;
File file = null ; //等待打开文件对象实例化
jfc.setApproveButtonText("确定");
jfc.setDialogTitle("逗比要打开文件了,呵呵"); //设置打开文件对话框标题
int result = jfc.showOpenDialog(frame) ; //记录打开操作
if(result == JFileChooser.APPROVE_OPTION) {
file = jfc.getSelectedFile() ;
//text.setText("选择的文件为"+file.getName()+"\n") ;
text.append("内容: ") ;
} else if(result ==JFileChooser.CANCEL_OPTION) {
text.setText("没有选择任何文件") ;
} else {
text.setText("没有选择任何文件") ;
}
if(file != null) {
try {
Scanner sc = new Scanner(file) ;
sc.useDelimiter("\n") ;
while(sc.hasNext()) {
text.append(sc.next());
text.append("\n");
}
sc.close() ;
} catch(Exception el){
}
}
}
}
);
saveItem.addActionListener(new ActionListener() { //响应保存按钮
public void actionPerformed(ActionEvent e) {
File file = null ;
JFileChooser jfc = new JFileChooser() ;
int result = jfc.showSaveDialog(frame) ;
if(result == JFileChooser.APPROVE_OPTION) {
file = jfc.getSelectedFile() ;
} else if(result == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog(null,"没有选择存储文件","逗比!",JOptionPane.ERROR_MESSAGE) ;
} else {
JOptionPane.showMessageDialog(null,"逗比,操作错误!","猴子的救兵!",JOptionPane.WARNING_MESSAGE) ;
}
if(file != null) {
try {
PrintStream out = new PrintStream(file) ;
out.println(text.getText()) ;
JOptionPane.showMessageDialog(null,"文件保存成功","反馈",JOptionPane.WARNING_MESSAGE);
out.close() ;
}catch(Exception ell) {
}
}
}
});
helpItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = new Font("楷体",Font.BOLD,20) ;
JLabel labelqq = new JLabel("加帅哥qq:653866417") ;
labelqq.setFont(font) ;
JOptionPane.showMessageDialog(null, labelqq,"哎呦我去!",JOptionPane.WARNING_MESSAGE);
}
}) ;
/**讲所有下拉菜单加入file
*
*/
file.add(openItem) ;
file.add(newItem) ;
file.add(saveItem) ;
help.add (helpItem) ;
frame.setJMenuBar(menubar);
frame.setSize(500,400);
frame.setLocation(200,200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(ActionEvent e) {
System.exit(1) ;
}
});
}
}
class Tester {
public static void main(String args[]) {
new MyMenu() ;
}
}