package swing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
public class MainFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 295941050339534223L;
Container contentPane;
JPanel menuPanel,ctntPanel,labelPanel,centerPanel;
JPanel filePanel,editPanel,outLookPanel,viewPanel,helpPanel;
JMenuBar menuBar;
JMenu fileMenu,editMenu,outLookMenu,viewMenu,helpMenu;
JMenuItem newFile,open,save,anotherSave,pageSetup,print,quit;
JMenuItem undo,cut,copy,paste,del,find,findNext,replace,turnto,selectAll,datetime;
JMenuItem auto,font,status,helptheme,aboutNotepad;
JTextArea textArea;
JTabbedPane pane;
File configFile;
InputStream configIs;
OutputStream configOs;
Integer height;
Integer width;
Integer x;
Integer y;
Properties properties = new Properties();
long clickTime = 0;
public MainFrame() {
super();
configFile = new File("F:/HelloworldFactory/Helloworld/ThinkinJava/j2seTest/swing/config.properties");
try {
configIs = new FileInputStream(configFile);
properties.load(configIs);
System.out.println(properties.get("height"));
System.out.println(properties.get("width"));
height = Integer.parseInt(properties.get("height").toString().replace(".0", ""));
width = Integer.parseInt(properties.get("width").toString().replace(".0", ""));
x = Integer.parseInt(properties.get("X").toString());
y = Integer.parseInt(properties.get("Y").toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean checkClickTime() {
long nowTime = (new Date()).getTime();
if ( (nowTime - clickTime) < 300) {
clickTime = nowTime;
return true;
}
clickTime = nowTime;
return false;
}
public void showContent(){
//fileMenu
newFile = newMenuItem(Name.XINJIAN);
open = newMenuItem(Name.DAKAI);
save = newMenuItem(Name.BAOCUN);
anotherSave = newMenuItem(Name.LINGCUNWEI);
pageSetup = newMenuItem(Name.YEMIANSHEZHI);
print = newMenuItem(Name.DAYIN);
quit = newMenuItem(Name.TUICHU);
//editMenu
undo = newMenuItem(Name.CHEXIAO);
cut = newMenuItem(Name.JIANQIE);
copy = newMenuItem(Name.FUZHI);
paste = newMenuItem(Name.NIANTIE);
del = newMenuItem(Name.SHANCHU);
find = newMenuItem(Name.CHAZHAO);
findNext = newMenuItem(Name.CHAZHAOXIAYIGE);
replace = newMenuItem(Name.TIHUAN);
turnto = newMenuItem(Name.ZHUANDAO);
selectAll = newMenuItem(Name.QUANXUAN);
datetime = newMenuItem(Name.SHIJIANRIQI);
//outLookMenu
auto = newMenuItem(Name.ZIDONGHUANHANG);
font = newMenuItem(Name.ZITI);
//viewMenu
status = newMenuItem(Name.ZHUANGTAILAN);
// status.setEnabled(false);
//helpMenu
helptheme = newMenuItem(Name.BANGZHUZHITI);
aboutNotepad = newMenuItem(Name.GUANYUJISHIBEN);
//tab
pane = new JTabbedPane();
fileMenu = new JMenu(Name.FILE_MENU);
fileMenu.add(newFile);
fileMenu.add(open);
fileMenu.add(save);
fileMenu.add(anotherSave);
fileMenu.add(new JSeparator());
fileMenu.add(pageSetup);
fileMenu.add(print);
fileMenu.add(new JSeparator());
fileMenu.add(quit);
editMenu = new JMenu(Name.EDIT_MENU);
editMenu.add(undo);
editMenu.add(new JSeparator());
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.add(del);
editMenu.add(new JSeparator());
editMenu.add(find);
editMenu.add(findNext);
editMenu.add(replace);
editMenu.add(turnto);
editMenu.add(new JSeparator());
editMenu.add(selectAll);
editMenu.add(datetime);
outLookMenu = new JMenu(Name.OUTLOOK_MENU);
outLookMenu.add(auto);
outLookMenu.add(font);
viewMenu = new JMenu(Name.VIEW_MENU);
viewMenu.add(status);
helpMenu = new JMenu(Name.HELP_MENU);
helpMenu.add(helptheme);
helpMenu.add(new JSeparator());
helpMenu.add(aboutNotepad);
textArea = new JTextArea();
menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(outLookMenu);
menuBar.add(viewMenu);
menuBar.add(helpMenu);
menuPanel = new JPanel(new GridLayout());
menuPanel.add(menuBar);
JScrollPane scrollPane = new JScrollPane(textArea);
ctntPanel = new JPanel(new GridLayout());
ctntPanel.add(scrollPane);
// pane.addTab("编辑1",scrollPane);
pane.addMouseListener(new MouseListener(){
public void mouseReleased(MouseEvent e) {
if(checkClickTime()){
int index = pane.getSelectedIndex();
System.out.println("hello: " + index);
if(index>=0)
pane.removeTabAt(index);
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
});
centerPanel = new JPanel(new GridLayout());
centerPanel.add(pane);
contentPane.add(menuPanel,BorderLayout.NORTH);
contentPane.add(centerPanel,BorderLayout.CENTER);
// contentPane.add(labelPanel,BorderLayout.SOUTH);
Toolkit tk=Toolkit.getDefaultToolkit();
Image image=tk.createImage("F:/HelloworldFactory/Helloworld/ThinkinJava/j2seTest/swing/title.png"); /*image.gif是你的图标*/
this.setIconImage(image);
// contentPane.add(new JScrollPane(textArea),BorderLayout.CENTER);
}
public JMenuItem newMenuItem(String name){
JMenuItem menuItem = new JMenuItem(name);
menuItem.addActionListener(this);
return menuItem;
}
public void init(){
contentPane = getContentPane();
setContentPane(contentPane);
setTitle("CK 记事本 v0.1");
}
public void showFrame(){
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
dispose();
properties.setProperty("height", getSize().getHeight()+"");
properties.setProperty("width", getSize().getWidth()+"");
properties.setProperty("X", getX()+"");
properties.setProperty("Y", getY()+"");
try {
configOs = new FileOutputStream(configFile);
properties.store(configOs, "");
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
}
});
pack();
setSize(width, height);
setLocation(x, y);
setVisible(true);
}
public void showAll(){
init();
showContent();
showFrame();
}
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.showAll();
}
@Override
public void pack() {
super.pack();
}
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
System.out.println(action);
if(action.equals(Name.XINJIAN)){
textArea.setText(null);
pane.addTab("编辑",new JScrollPane(textArea));
System.out.println("text area: "+textArea.getText().length());
}
if(action.equals(Name.DAKAI)){
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(this);
if(fileChooser.getSelectedFile()==null){
System.out.println("未选中文件");
}else{
File file = fileChooser.getSelectedFile();
if(file.getName().endsWith(".txt")==true){
FileOperation fo = new FileOperation();
fo.readFile(file,textArea);
pane.addTab(file.getAbsolutePath(),new JScrollPane(textArea));
}else{
JOptionPane.showMessageDialog(this, "暂只支持.txt");
}
}
}
if(action.equals(Name.BAOCUN)){
}
if(action.equals(Name.LINGCUNWEI)){
JFileChooser fileChooser = new JFileChooser();
fileChooser.showSaveDialog(this);
}
if(action.equals(Name.YEMIANSHEZHI)){
//以后处理
}
if(action.equals(Name.DAYIN)){
//以后处理
}
if(action.equals(Name.TUICHU)){
dispose();
System.exit(0);
}
if(action.equals(Name.ZIDONGHUANHANG)){
auto.setText(Name.ZIDONGHUANHANG2);
textArea.setLineWrap(true);
status.setEnabled(false);
}
if(action.equals(Name.ZIDONGHUANHANG2)){
auto.setText(Name.ZIDONGHUANHANG);
textArea.setLineWrap(false);
status.setEnabled(true);
}
if(action.equals(Name.ZITI)){
System.out.println("1111");
}
}
}