java Swing 实现记事本

/*   
    Copyright (C) 2017 mzy  <[email protected]>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .
*/

package notepad;

import java.awt.Color;

/**
 *
 * @author 半步疯子
 * @version 1.0
 */

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.filechooser.FileNameExtensionFilter;


public class Notepad extends JFrame implements ActionListener{
	public static void main(String[] args) {
		new Notepad();
		
	}
	
	private static final long serialVersionUID = -2843137103983861960L;
	JTextArea text; // 必须使用JTextArea  不然第二次显示会出问题
	
	
	public Notepad(){
		notepadUI();
		setVisible(true); // setVisible 为true的时候是显示前面添加在JFrame上在组建,所以需要放在最后面
		
	}
	private void notepadUI() {
		// 设置基本属性   
		setTitle("MZY记事本");
		setSize(600, 400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		
		setIconImage(new ImageIcon("icon/notepad.png").getImage());
		// setIconImage(new ImageIcon(getClass().getResource("icon/notepad.png")).getImage());//图标放在源目录的icon文件夹
		Container cp = getContentPane();
		// 怎么将JMenuItem显示在container层上
		text = new JTextArea();
		text.setFont(new Font("新宋体",Font.PLAIN, 24));
		cp.add(new JScrollPane(text));
	
		String[] captions = {"文件(F)","编辑(E)","格式(O)","帮助(H)"};
		String[][] titles = {
				{"新建","打开...","保存","另存为...", "-", "退出"},
				{"复制","剪切","粘贴","全选"},
				{"字体(F)..."},
				{"帮助","-","关于记事本"}
		};
		// JMenuBar ->  
		JMenuBar menuBar = new JMenuBar();
		JMenu menu;
		JMenuItem item;
		for(int i =0; i fontList = new JList(fontName);
	JList StyleList = new JList(fontStyle);
	// SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize) 
	SpinnerModel spinnerModel = new SpinnerNumberModel(bounds, 0, 80, 2);
	JSpinner spinner = new JSpinner(spinnerModel); // 上下的小箭头
	
	// south
	
	JButton ok = new JButton("确定");
	JButton cancel = new JButton("取消");

	
	MyFormat(JTextArea text){
		this.text = text;
		setIconImage(new ImageIcon("icon/notepad.png").getImage());
		
		cp.setPreferredSize(new Dimension(500, 300));
        cp.setLayout(null);
		
		JLabel l2 = new JLabel("字形");
		JLabel l1 = new JLabel("字体");
		JLabel l3 = new JLabel("大小");
		l1.setFont(new Font("华文行楷",Font.BOLD, 28));
		l2.setFont(new Font("华文行楷",Font.BOLD, 28));
		l3.setFont(new Font("华文行楷",Font.BOLD, 28));
	
		l1.setBounds(40, 20, 140, 35);
		l2.setBounds(215, 20, 120, 35);
		l3.setBounds(360, 20, 75, 35);
		cp.add(l1);
		cp.add(l2);
		cp.add(l3);
		
		StyleList.setFont(new Font("新宋体",0 , 18));
		JScrollPane j2 = new JScrollPane(StyleList);
		
		
		fontList.setFont(new Font("新宋体",0 , 12));
		JScrollPane j1 = new JScrollPane(fontList);
		
		
		spinner.setFont(new Font("新宋体",1 , 18));
		
		j1.setBounds(40, 75, 150, 175);
		j2.setBounds(215, 75, 80, 120);
		spinner.setBounds(360, 75, 45, 50);
		cp.add(j1);
		cp.add(j2);
		cp.add(spinner);
		
		ok.addActionListener(this);
		cancel.addActionListener(this);
		cp.add(ok);
		cp.add(cancel);
		
		ok.setBounds(245, 240, 80, 30);
		cancel.setBounds(380, 240, 80, 30);
		
		setTitle("字体");
		setVisible(true);
        setLocationRelativeTo(null);
        setResizable(false); // 设置为大小不可变的
		pack(); // 调整计算全部都可显示的大小  这一句必须放在最后不然没法显示
	}
	
	public void actionPerformed(ActionEvent e) {	
		String s = e.getActionCommand();
		if("确定".equals(s)) {
			style = myGetType();
			bounds = Integer.parseInt(spinner.getValue().toString());
			font = new Font((String)fontList.getSelectedValue(), style, bounds);
			text.setFont(font);
			this.dispose();
		}
		if("取消".equals(s)) {
			dispose();
		}
	}
	
	private int myGetType() {
		if("常规".equals(StyleList.getSelectedValue())) {
			return 0;
		} else if("倾斜".equals(StyleList.getSelectedValue())) {
			return 1;
		} else if("粗体".equals(StyleList.getSelectedValue())) {
			return 2;
		} else if("粗斜".equals(StyleList.getSelectedValue())) {
			return 3;
		}
		return 0;
	}
}

class AboutHelp extends JDialog {// 关于窗口
	
	private static final long serialVersionUID = 1L;

	AboutHelp() {
		
		JTextArea t = new JTextArea();
		t.setFont(new Font("微软雅黑",1 , 16));
		String s = "   作者:半步疯子          "
				+ "实现功能均可点击 :"
				+ "右键点击也可操作";
				
        t.setText(s);
        t.setEditable(false);
        add(t);
        setIconImage(new ImageIcon("icon/notepad.png").getImage());
        setSize(480, 100);
        setVisible(true);
        setLocationRelativeTo(null);
        setResizable(false); // 设置为大小不可变的
  
	}
}

class AboutMyNotepad extends JDialog {// 关于窗口
	
	private static final long serialVersionUID = 1L;

	AboutMyNotepad() {
		
		JTextArea t = new JTextArea();
		t.setFont(new Font("微软雅黑",1 , 16));
		String s = "   作者:半步疯子          "
				+ "版本:mzy1.0     "
				+ " 联系:[email protected]";
        t.setText(s);
        t.setEditable(false);
        add(t);
        setIconImage(new ImageIcon("icon/notepad.png").getImage());
        setSize(550, 100);
        setVisible(true);
        setLocationRelativeTo(null);
        setResizable(false); // 设置为大小不可变的
  
	}
}

你可能感兴趣的:(Java基础篇,作品)