源文件、文档说明、可运行的jar文件下载地址http://download.csdn.net/source/1852333、
/**
* NoteBook
*
* @versionliujunguang
* @version 1.00 09/10/25
*/
public class NoteBook {
public static void main(String[] args) {
// Create application frame.
NoteBookFrame frame = new NoteBookFrame();
// Show frame
frame.setVisible(true);
}
}
import java.awt.*;
/**
* NoteBook-OpenClass.
*
* @liujunguang
* @version 1.00 09/10/28
*/
class SeeClass extends Frame
{
NoteBookFrame notebookframe = null;
SeeClass(NoteBookFrame p)
{
notebookframe = p;
}
public void Modo()
{
System.out.println("状态栏");
}
}
//编辑类中的查找选项的实现
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;//String 类
import javax.swing.JOptionPane;
class SearchFrame extends JFrame implements ActionListener,ItemListener//单选按钮事件接口
{
JLabel label1,label2,label3;
JButton but1,but2,but3,but4;
JTextField textfield,text;
JRadioButton radiobutton1,radiobutton2;
ButtonGroup group1;
JCheckBox box1;
NoteBookFrame notebookframe = null;
String sub1="",sub2="";
int n=-1;
boolean isup = false;//判断是否是最后一个找到的内容时用到
boolean direction = true ;//判断查找方向 向下为 true
SearchFrame(NoteBookFrame p)
{
notebookframe = p;
label1 = new JLabel("查找内容(N):");
label2 = new JLabel("方向");
label3 = new JLabel("替换为(P)");
but1 = new JButton("查找下一个(F)");
but2 = new JButton(" 取 消 ");
but3 = new JButton(" 替 换 ");
but4 = new JButton(" 转 到 ");
textfield = new JTextField(15);
text = new JTextField(15);
radiobutton1 = new JRadioButton("向下(U)",true);
radiobutton2 = new JRadioButton("向上(D)");
group1 = new ButtonGroup();
//创建组件
box1 = new JCheckBox("区分大小写(C)");
Container con = getContentPane();
con.setLayout(null);//设置布局为空
group1.add(radiobutton1);
group1.add(radiobutton2);//将单选按钮加到按钮组中
con.add(label1);//查找
label1.setBounds(10,10,85,25);
con.add(label3);//替换为
label3.setBounds(10,45,85,25);
con.add(textfield);//文本
textfield.setBounds(90,10,180,25);
con.add(text);//文本
text.setBounds(90,45,180,25);
con.add(but1);//查找下一个
but1.setBounds(280,10,120,25);
con.add(label2);//方向
label2.setBounds(135,80,50,25);
con.add(box1);
box1.setBounds(10,115,120,25);
con.add(radiobutton1);//向下
radiobutton1.setBounds(130,115,70,25);
con.add(radiobutton2);//向上
radiobutton2.setBounds(200,115,70,25);
con.add(but2);//取消
but2.setBounds(280,115,120,25);
con.add(but3);//替换
but3.setBounds(280,45,120,25);
con.add(but4);//转到
but4.setBounds(280,80,120,25);
but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
but4.addActionListener(this);
box1.addItemListener(this);
textfield.addActionListener(this);
text.addActionListener(this);
radiobutton1.addItemListener(this);
radiobutton2.addItemListener(this);
//注册监听
setBounds(500,300,420,180);
setVisible(false);
setTitle("查找");
validate();
}
public void actionPerformed(ActionEvent e)
{
sub1 = notebookframe.textarea.getText();//得到文本区中的文本
sub2 = textfield.getText();//得到文本框中的文本
if(!isup) //如果不区分大小写(默认不区分大小写)
{
sub1 = sub1.toLowerCase();//将sub1转换成小写
sub2 = sub2.toLowerCase();//将sub2转换成小写
}
if(!direction) n = sub1.lastIndexOf(sub2);//在文本区中查找文本框中的内容
else n = sub1.indexOf(sub2);//在文本区中查找文本框中的内容
if(e.getSource() == but1||e.getSource() == textfield)//查找下一个
{
if(n!=-1)
{
notebookframe.toFront();//如果此窗口是可见的,则将此窗口置于前端,并可以将其设为焦点 Window
notebookframe.textarea.select(n,n+sub2.length());//选中查找的内容
this.setVisible(false);
}
else
{
JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource() == but2)//取消
{
this.setVisible(false);
}
if(e.getSource() == but4)//转到
{
int i=0,j=1;
char ch[];
try{
int raw = Integer.parseInt(textfield.getText());//得到文本框中的文本
String s = notebookframe.textarea.getText();
ch = new char[s.length()];
s.getChars(0,s.length()-1,ch,0);
while(j<raw)
{
i++;
if(ch[i] == '/n')
{j++;}
if(i == s.length())break;
}
if(raw == 1) //转到第一行
notebookframe.textarea.setCaretPosition(0);
else
notebookframe.textarea.setCaretPosition(i+1);//转到指定行
this.setVisible(false);
}
catch(Exception a){
JOptionPane.showMessageDialog(this,"你输入的位置不对无法到达!","记事本",JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource() == but3)//替换
{
if(n != -1)
{
String sub3 = text.getText();
notebookframe.textarea.select(n,n+sub2.length());//选中查找的内容
notebookframe.textarea.replaceRange(sub3,n,n+sub2.length());//替换选中位置的文本
sub1 = notebookframe.textarea.getText();//得到文本区中的文本
sub2 = textfield.getText();//得到文本框中的文本
if(!isup) //如果不区分大小写(默认不区分大小写)
{
sub1 = sub1.toLowerCase();//将sub1转换成小写
sub2 = sub2.toLowerCase();//将sub2转换成小写
}
if(!direction) n = sub1.lastIndexOf(sub2);//在文本区中查找最后出现的文本框中的内容
else n = sub1.indexOf(sub2);//在文本区中查找最先出现的文本框中的内容
notebookframe.toFront();//如果此窗口是可见的,则将此窗口置于前端,并可以将其设为焦点 Window
if(n!=-1 )
{
notebookframe.textarea.select(n,n+sub2.length());//选中查找内容
}
else
JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(this,"所指定的文本没有找到无法替换!","记事本",JOptionPane.WARNING_MESSAGE);
}
}
}
public void nextShear()//查找下一个 菜单项对应的
{
if(n!=-1)
{
sub1 = notebookframe.textarea.getText().substring(n+sub2.length());//得到文本区的子串
if(!isup) //如果不区分大小写(默认不区分大小写)
{
sub1 = sub1.toLowerCase();//将sub1转换成小写
sub2 = sub2.toLowerCase();//将sub2转换成小写
}
if(sub1.indexOf(sub2)!=-1)
{
n = n+sub2.length()+sub1.indexOf(sub2);//得到查找内容在文本区中的位置
notebookframe.textarea.select(n,n+sub2.length());//选中查找内容
}
else
JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);
}
else
JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);
}
public void itemStateChanged(ItemEvent ee)
{
if(ee.getSource() == box1)
{
if(box1.isSelected())
{
isup = true ;
}
else
isup = false ;
}
if(ee.getSource() == radiobutton1)
{
if(radiobutton1.isSelected())
{
direction = true ;
}
}
if(ee.getSource() == radiobutton2)
{
if(radiobutton2.isSelected())
{
direction = false;
}
}
}
}
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
/**
* NoteBook-OpenClass.
* @liujunguang
* @version 1.00 09/10/24
*/
class OpenClass extends Frame
{
NoteBookFrame notebookframe = null;
FileDialog filedialog_open,filedialog_save;
File file1,file2; //文件字节流对象
FileReader file_reader;//文件字符流对象
FileWriter tofile;
BufferedReader in ;//文件行读取 写入对象
BufferedWriter out;
String s;
boolean issave = false;//用于判断是否是第一次保存
String filesave = null,savename = null; //用于存放文件地址 和文件名
String fileopen = null,filename = null;//用于存放打开文件地址 和文件名
PrintJob p=null;//声明一个要打印的对象
Graphics g=null;//要打印的对象
OpenClass(NoteBookFrame p )
{
notebookframe = p;
filedialog_open = new FileDialog(notebookframe,"打开文件对话框",FileDialog.LOAD);
filedialog_save = new FileDialog(notebookframe,"保存文件对话框",FileDialog.SAVE);
filedialog_open.setVisible(false);
filedialog_save.setVisible(false);
//notebookframe = new NoteBookFrame();
filedialog_open.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ filedialog_open.setVisible(false); }
}
);//打开文件对话框适配器
filedialog_save.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ filedialog_save.setVisible(false); }
}
);//保存文件对话框适配器
}
public void New()
{
if(!notebookframe.textarea.getText().equals(""))
{
int n = JOptionPane.showConfirmDialog(notebookframe,"是否保存原有的信息?","询问对话框",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
Save();
}
}
notebookframe.textarea.setText("");
}
public void Open()
{
if(!notebookframe.textarea.getText().equals(""))
{
int n = JOptionPane.showConfirmDialog(notebookframe,"是否保存原有的信息?","询问对话框",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
Save();
}
if(n == JOptionPane.NO_OPTION)
{
notebookframe.textarea.setText("");
}
}
filedialog_open.setVisible(true);
fileopen = filedialog_open.getDirectory();//返回文件对话框中显示的文件所属的目录
filename = filedialog_open.getFile();//返回当前文件对话框中显示的文件名的字符串表示 如果不存在就返回NULL
if(filename!=null)// 判断打开的文件是否存在
{
try
{
file1 = new File(fileopen,filename);
if(file1.isFile())//判断文件是否是正常的文件而不是目录
{
notebookframe.textarea.setText("");
file_reader = new FileReader(file1);
in = new BufferedReader(file_reader);//每次读取一行
while((s = in.readLine())!=null)
notebookframe.textarea.append(s+'/n');
in.close();
file_reader.close();
}
}
catch (IOException e2){ System.out.println(filename); }
}
}
public void Save()
{
if(!issave&&filename == null)
{
filedialog_save.setVisible(true);
filesave = filedialog_save.getDirectory();
savename = filedialog_save.getFile()+".txt";
issave = true;
}
if(filename != null||!savename.equals(null+".txt"))
{
try{
if(filename!= null){filesave = fileopen; savename = filename;}
file2 = new File(filesave,savename);
tofile = new FileWriter(file2);
out = new BufferedWriter(tofile);
out.write(notebookframe.textarea.getText(),0,notebookframe.textarea.getText().length());
out.close();
tofile.close();
}
catch (IOException e2){ }
}
}
public void AsSave()
{
int n=-2;
filedialog_save.setVisible(true);
String filesave = filedialog_save.getDirectory();
String savename = filedialog_save.getFile()+".txt";
if(!savename.equals(null+".txt"))
{
try{
file2 = new File(filesave,savename);
tofile = new FileWriter(file2);
out = new BufferedWriter(tofile);
out.write(notebookframe.textarea.getText(),0,notebookframe.textarea.getText().length());
out.close();
tofile.close();
}
catch (IOException e2){ }
}
}
public void SetUp()
{
JOptionPane.showMessageDialog(this,"在打印的时候用到!","警告对话框",JOptionPane.WARNING_MESSAGE);
}
public void Print()
{
try{
p = getToolkit().getPrintJob(notebookframe,"ok",null);//创建一个Printfjob 对象 p
g = p.getGraphics();//p 获取一个用于打印的 Graphics 的对象
g.translate(120,200);//改变组建的位置
notebookframe.textarea.printAll(g);
p.end();//释放对象 g
}
catch(Exception a){
}
}
}
import java.awt.*;
import java.awt.event.*;
/**
* NoteBook-BookFrame.
*
* @liujunguang
* @version 1.00 09/10/24
*/
class NoteBookFrame extends Frame implements ActionListener , KeyListener
{
TextArea textarea ;
MenuBar menuBar;
Menu Open,Edit,Formt,See,Help;
MenuItem NewFile,OpenFile,Save,AsSave,SetUp,Print,Exit;
MenuItem Repeal,Cut,Copy,Stick,Delete,Lookup,Lookupnext,Exchange,Goto,All,Timedate;
MenuItem Rand,FontSize,Modo,MainHelp,AboutBook;
OpenClass openclass ;
EditClass editclass ;
FormateClass formateclass;
SeeClass seeclass;
HelpClass helpclass;
public NoteBookFrame() {
textarea = new TextArea(40,500);
openclass = new OpenClass(this);
editclass = new EditClass(this);
formateclass = new FormateClass(this);
seeclass = new SeeClass(this);
helpclass = new HelpClass(this);
menuBar = new MenuBar();
//创建菜单条
Open = new Menu("打开(F)");
Edit = new Menu("编辑(E)");
Formt = new Menu("格式(O)");
See = new Menu("查看(V)");
Help = new Menu("帮助(H)");
//创建菜单
NewFile = new MenuItem("新建");
OpenFile = new MenuItem("打开");
Save = new MenuItem("保存");
AsSave = new MenuItem("另存为(A)... ");
SetUp = new MenuItem("页面设置(U) ");
Print = new MenuItem("打印(P) ...");
Exit = new MenuItem("退出(X)");
//开始菜单中的各菜单项
Repeal = new MenuItem("撤销(U)");
Cut= new MenuItem("剪切(T)");
Copy= new MenuItem("复制(C)");
Stick= new MenuItem("粘贴(P)");
Delete= new MenuItem("删除(L)Del");
Lookup= new MenuItem("查找(F)");
Lookupnext= new MenuItem("查找下一个(N) F3");
Exchange= new MenuItem("替换(R)");
Goto= new MenuItem("转到(G)");
All= new MenuItem("全选(A)");
Timedate= new MenuItem("时间/日期(D) F5");
//编辑菜单中的各菜单项
Rand = new MenuItem("自动换行(W) ");
FontSize = new MenuItem("字体(F)... ");
//格式菜单中的各菜单项
Modo = new MenuItem("状态栏(S)");
//查看菜单中的各菜单项
MainHelp = new MenuItem("帮助主题(H) ");
AboutBook = new MenuItem("关于记事本(A) ");
//帮助菜单中的各菜单项
Open.add(NewFile);
Open.add(OpenFile);
Open.add(Save);
Open.add(AsSave);
Open.addSeparator();
Open.add(SetUp);
Open.add(Print);
Open.addSeparator();//添加分割线
Open.add(Exit);
//在打开菜单中加菜单项
Edit.add(Repeal);
Edit.addSeparator();
Edit.add(Cut);
Edit.add(Copy);
Edit.add(Stick);
Edit.add(Delete);
Edit.addSeparator();
Edit.add(Lookup);
Edit.add(Lookupnext);
Edit.add(Exchange);
Edit.add(Goto);
Edit.addSeparator();
Edit.add(All);
Edit.add(Timedate);
//在编辑菜单中添加菜单项
Formt.add(Rand);
Formt.add(FontSize);
//格式菜单中添加菜单项
See.add(Modo);
//查看菜单中添加菜单项
Help.add(MainHelp);
Help.addSeparator();
Help.add(AboutBook);
//帮助菜单中添加菜单项
menuBar.add(Open);
menuBar.add(Edit);
menuBar.add(Formt);
menuBar.add(See);
menuBar.add(Help);
//菜单条中添加菜单
NewFile.setShortcut(new MenuShortcut(KeyEvent.VK_N));
OpenFile.setShortcut(new MenuShortcut(KeyEvent.VK_O));
Save.setShortcut(new MenuShortcut(KeyEvent.VK_S));
Print.setShortcut(new MenuShortcut(KeyEvent.VK_P));
Repeal.setShortcut(new MenuShortcut(KeyEvent.VK_Z));
Cut.setShortcut(new MenuShortcut(KeyEvent.VK_X));
Copy.setShortcut(new MenuShortcut(KeyEvent.VK_C));
Stick.setShortcut(new MenuShortcut(KeyEvent.VK_V));
Lookup.setShortcut(new MenuShortcut(KeyEvent.VK_F));
Exchange.setShortcut(new MenuShortcut(KeyEvent.VK_H));
Goto.setShortcut(new MenuShortcut(KeyEvent.VK_G));
All.setShortcut(new MenuShortcut(KeyEvent.VK_A));
//添加菜单项中的各项的快捷键
setTitle("记事本");
setMenuBar(menuBar);//窗口中添加菜单条
add(textarea);
setBounds(500,150,400,400);
textarea.addKeyListener(this);
//注册文本域的键盘事件
NewFile.addActionListener(this);
OpenFile.addActionListener(this);
Save.addActionListener(this);
AsSave.addActionListener(this);
SetUp.addActionListener(this);
Print.addActionListener(this);
//注册监听打开菜单中的各项
Repeal.addActionListener(this);
Cut.addActionListener(this);
Copy.addActionListener(this);
Stick.addActionListener(this);
Delete.addActionListener(this);
Lookup.addActionListener(this);
Lookupnext.addActionListener(this);
Exchange.addActionListener(this);
Goto.addActionListener(this);
All.addActionListener(this);
Timedate.addActionListener(this);
//注册监听编辑菜单中的各项
Rand.addActionListener(this);
FontSize.addActionListener(this);
//注册监听格式菜单中的各项
Modo.addActionListener(this);
//注册监听查看菜单中的各项
MainHelp.addActionListener(this);
AboutBook.addActionListener(this);
//注册监听帮助菜单中的各项
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
NoteBookFrame.this.windowClosed();
}
}
);
// Add action listener.for the menu button
Exit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
NoteBookFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == NewFile){openclass.New();}
if(e.getSource() == OpenFile){openclass.Open();}
if(e.getSource() == Save){openclass.Save();}
if(e.getSource() == AsSave){openclass.AsSave();}
if(e.getSource() == SetUp){openclass.SetUp();}
if(e.getSource() == Print){openclass.Print();}
if(e.getSource() == Exit){System.out.println("退出");}
//打开菜单事件的实现
if(e.getSource() == Repeal){editclass.Repeal();}
if(e.getSource() == Cut){editclass.Cut();}
if(e.getSource() == Copy){editclass.Copy();}
if(e.getSource() == Stick){editclass.Stick();}
if(e.getSource() == Delete){editclass.Delete();}
if(e.getSource() == Lookup){editclass.LookUp();}
if(e.getSource() == Lookupnext){editclass.LookUpNext();}
if(e.getSource() == Exchange){editclass.Exchange();}
if(e.getSource() == Goto){editclass.Goto();}
if(e.getSource() == All){editclass.All();}
if(e.getSource() == Timedate){editclass.TimeDate();}
//编辑菜单的实现
if(e.getSource() == Rand){formateclass.Rand();}
if(e.getSource() == FontSize){formateclass.FontSize();}
//格式菜单的实现
if(e.getSource() ==Modo ){seeclass.Modo();}
//查看菜单的实现
if(e.getSource() == MainHelp){helpclass.MainHeip();}
if(e.getSource() == AboutBook){helpclass.AboutBook();}
//帮助菜单的实现
}
public void keyPressed(KeyEvent key)//处理按下某键时
{
// 文本区的键盘事件的实现
}
public void keyTyped(KeyEvent key){}//处理按下后又恢复的事件
public void keyReleased(KeyEvent key){} //恢复时要掉用的事件
}
import java.awt.*;
import javax.swing.JOptionPane;//警告对话框
/**
* NoteBook-OpenClass.
*
* @liujunguang
* @version 1.00 09/10/28
*/
class HelpClass extends Frame
{
NoteBookFrame notebookframe = null;
HelpClass(NoteBookFrame p)
{
notebookframe = p;
}
public void MainHeip()
{
JOptionPane.showMessageDialog(this,"记事本帮助文档!","记事本",JOptionPane.WARNING_MESSAGE);
}
public void AboutBook()
{
JOptionPane.showMessageDialog(notebookframe,"记事本"+"/n"+" 版本: 1.0.0"+"/n"
+" 作者: 米林"+"/n"
+" 时间: 2009/11/8","记事本",JOptionPane.WARNING_MESSAGE);
}
}
import java.awt.*;
/**
* NoteBook-OpenClass.
*
* @liujunguang
* @version 1.00 09/10/28
*/
class FormateClass extends Frame
{
NoteBookFrame notebookframe = null;
FontFrame font =null ;// 定义字体类的对象
Toolkit tool = null;
Dimension dim = null;
boolean ran = true;
int n ;
FormateClass(NoteBookFrame p)
{
notebookframe = p;
tool = getToolkit();
dim = tool.getScreenSize();
font = new FontFrame(notebookframe);
}
public void Rand()
{
if(ran)
{
notebookframe.textarea.setColumns(1);//得到文本区的列数
System.out.println(notebookframe.getWidth());
notebookframe.Rand.setLabel("√自动换行");
ran = false;
}
//notebookframe.textarea.getRows();// 得到文本区的行数
//notebookframe.setBounds(0,0,dim.width,dim.height);
else
{
notebookframe.textarea.setColumns(4);
System.out.println(dim.width);
ran = true;
notebookframe.Rand.setLabel("自动换行");
}
}
public void FontSize()
{
font.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
/**
* NoteBook-OpenClass.
*
* @liujunguang
* @version 1.00 09/10/28
*/
class FontFrame extends JFrame implements ItemListener,ActionListener
{
JLabel label1,label2,label3,label4;
JButton but1,but2;//按钮
JTextField text1,text2,text3,text4;
List list1,list2,list3;//滚动列表
String fontname ;//字体名称
int fontstytle = Font.PLAIN;//字体式样
int fontsize = 12;//字体的大小
NoteBookFrame notebookframe = null;
FontFrame(NoteBookFrame p)
{
notebookframe = p ;
label1 = new JLabel("字体(F):");
label2 = new JLabel("字形(Y):");
label3 = new JLabel("大小(S):");
label4 = new JLabel("示例:");
but1 = new JButton("确 定");
but2 = new JButton("取 消");
text1 = new JTextField("Arial");
text2 = new JTextField("常规");
text3 = new JTextField("12");
text4 = new JTextField("AaBbCc");
text4.setHorizontalAlignment(JTextField.CENTER);
list1 = new List(7,false);//可现 7 个,不可多选
list2 = new List(7,false);
list3 = new List(7,false);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();//计算机上字体可用的名称
String fontName[] = ge.getAvailableFontFamilyNames();
for(int i = 0 ;i<fontName.length;i++)
{
list1.add(fontName[i]);
}
list2.add("常规");
list2.add("粗体");
list2.add("斜体");
list2.add("粗斜体");
String fontSize[]={"8","9","10","11","12","13","14","16","18","20","22","24","26","28","36","48","72","初号",
"小初","一号","小一","二号","小二","三号","小三","四号","小四","五号","小五","六号","小六","七号","八号"};
for(int i = 0 ;i<fontSize.length;i++)
{
list3.add(fontSize[i]);
}
list1.addItemListener(this);
list2.addItemListener(this);
list3.addItemListener(this);
Container con = getContentPane();
con.setLayout(null);//设置布局为空
con.add(label1);//字体
label1.setBounds(20,10,60,20);
con.add(label2);//字形
label2.setBounds(180,10,60,20);
con.add(label3);//大小
label3.setBounds(300,10,60,20);
con.add(label4);//示例
label4.setBounds(180,250,60,20);
//标签的设定
con.add(but1); //确定
but1.addActionListener(this);
but1.setBounds(400,35,70,20);
con.add(but2);//取消
but2.addActionListener(this);
but2.setBounds(400,65,70,20);
//按钮的设定
con.add(text1); //字体
text1.setBounds(20,35,150,20);
con.add(text2); //字形
text2.setBounds(180,35,110,20);
con.add(text3);//大小
text3.setBounds(300,35,80,20);
con.add(text4);//示例
text4.setBounds(180,275,200,60);
text4.setEditable(false);
//文本框的设定
con.add(list1);
list1.setBounds(20,60,150,180);
con.add(list2);
list2.setBounds(180,60,110,180);
con.add(list3);
list3.setBounds(300,60,80,180);
//文本域的设定
this.setVisible(false);
setBounds(100,100,500,400);
setTitle("字体");
validate();
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==list1)
{
fontname = list1.getSelectedItem();
text1.setText(fontname);
Font f = new Font(fontname,fontstytle,fontsize);
text4.setFont(f);
}
if(e.getSource()==list2)
{
int index = list2.getSelectedIndex();
switch(index)
{
case 0:
fontstytle = Font.PLAIN;break;
case 1:
fontstytle = Font.BOLD; break;
case 2:
fontstytle = Font.ITALIC;break;
case 3:
fontstytle = Font.BOLD + Font.ITALIC; break;
}
text2.setText(list2.getSelectedItem());
Font f = new Font(fontname,fontstytle,fontsize);
text4.setFont(f);
}
if(e.getSource()==list3)
{
int index = list3.getSelectedIndex();
if(index<=16)
fontsize = Integer.parseInt(list3.getSelectedItem());
switch(index)
{
case 17: fontsize = 100;break;
case 18: fontsize = 90;break;
case 19: fontsize = 80;break;
case 20: fontsize = 60;break;
case 21: fontsize = 50;break;
case 22: fontsize = 40;break;
case 23: fontsize = 30;break;
case 24: fontsize = 20;break;
case 25: fontsize = 18;break;
case 26: fontsize = 16;break;
case 27: fontsize = 12;break;
case 28: fontsize = 11;break;
case 29: fontsize = 9;break;
case 30: fontsize = 8;break;
case 31: fontsize = 7;
}
text3.setText(list3.getSelectedItem());
Font f = new Font(fontname,fontstytle,fontsize);
text4.setFont(f);
}
}
public void actionPerformed(ActionEvent ee)
{
if(ee.getSource()==but1)
{
Font f = new Font(fontname,fontstytle,fontsize);
notebookframe.textarea.setFont(f);
this.setVisible(false);
}
if(ee.getSource()==but2)
{
this.setVisible(false);
}
}
}
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.datatransfer.*;//剪切板的类
import java.text.SimpleDateFormat;//时间格式
import javax.swing.JOptionPane;//警告对话框
/**
* NoteBook-OpenClass.
*
* @liujunguang
* @version 1.00 09/10/28
*/
class EditClass extends Frame
{
NoteBookFrame notebookframe = null;
Date nowdate;//定义一个时间对象
Clipboard clipboard = null;//定义一个剪切板对象
SimpleDateFormat dateformat;
SearchFrame search=null;
EditClass(NoteBookFrame p)
{
notebookframe = p;
nowdate = new Date();
search= new SearchFrame(notebookframe);
clipboard = getToolkit().getSystemClipboard();//获得系统的剪切板
dateformat = new SimpleDateFormat("'本地时间:' yyyy年MM月dd日 E HH时mm分 ");
}
public void Repeal()
{
JOptionPane.showMessageDialog(this,"由于本人能力有限故还没有实现,请谅解!","记事本",JOptionPane.WARNING_MESSAGE);
}
public void Cut()
{
String temp = notebookframe.textarea.getSelectedText();//获得鼠标拖动选取的文本
StringSelection text = new StringSelection(temp);//把待剪切的文本传递给 text 对象
clipboard.setContents(text,null);//将文本放入剪切板中
int start = notebookframe.textarea.getSelectionStart();//获取选中文本的开始位置
int end = notebookframe.textarea.getSelectionEnd();//获取选中文本的结束位置
notebookframe.textarea.replaceRange("",start,end);//选中的区域用""替换
}
public void Copy()
{
String temp = notebookframe.textarea.getSelectedText();//获得鼠标拖动选取的文本
StringSelection text = new StringSelection(temp);//把待剪切的文本传递给 text 对象
clipboard.setContents(text,null);//将文本放入剪切板中
}
public void Stick()
{
Transferable contexts = clipboard.getContents(notebookframe);//获取剪切板中的内容
DataFlavor flavor = DataFlavor.stringFlavor;//剪切板的风格(系统的标准风格)
if(contexts.isDataFlavorSupported(flavor))//判断风格java是否可用
{
try{
String str = (String)contexts.getTransferData(flavor);
int start = notebookframe.textarea.getSelectionStart();//获取选中文本的开始位置
int end = notebookframe.textarea.getSelectionEnd();//获取选中文本的结束位置
notebookframe.textarea.replaceRange(str,start,end);//替换光标所在位置的文本
}
catch(Exception ee){}
}
}
public void Delete()
{
String temp = notebookframe.textarea.getSelectedText();//获得鼠标拖动选取的文本
int start = notebookframe.textarea.getSelectionStart();
int end = notebookframe.textarea.getSelectionEnd();
notebookframe.textarea.replaceRange("",start,end);//选中的区域用""替换
}
public void LookUp()
{
searchfor();
}
public void LookUpNext()
{
if(!notebookframe.textarea.getText().equals(""))
{
search.nextShear();
}
else
{
JOptionPane.showMessageDialog(this,"文本内容不为空时才能使用该功能!","记事本",JOptionPane.WARNING_MESSAGE);
}
}
public void Exchange()//替换
{
searchfor();
}
public void Goto()//转到
{
searchfor();
}
public void All()//全选
{
notebookframe.textarea.selectAll();
}
public void TimeDate()//时间的获得
{
notebookframe.textarea.append(dateformat.format(nowdate));
}
public void searchfor()//为减少代码编写的 查找函数
{
if(!notebookframe.textarea.getText().equals(""))
{
search.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(this,"文本内容不为空时才能使用该功能!","记事本",JOptionPane.WARNING_MESSAGE);
}
}
}