import java.awt.*
import java.io.*
import java.util.Calendar
import java.util.Date
import javax.swing.*
class Editor extends Frame implements ActionListener,ItemListener
{
// Date date=new Date()
int n1String str=""int k=0String st1,st2
JTextArea ta=new JTextArea(50,50)
Dialog dialog,dialog1
Choice ce1,ce2,ce3
Button btn1,btn2,btn3Panel p1=new Panel()
Panel p2=new Panel()Panel p3=new Panel()Panel p4=new Panel()
public Editor (String s)
{
super ("记事本编辑板")
ImageIconYouImg=newImageIcon("iconimage.jpg")
setIconImage(YouImg.getImage())
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose()
System.exit(0)
}
})
setBounds(50,35,700,500)
MenuBar mb=new MenuBar()
setMenuBar(mb)
Menu file=new Menu("文件(F)")Menu edit=new Menu("编辑(E)")Menu help=new Menu("帮助(H)")
Menu pattern=new Menu("格式(P)")Menu time=new Menu("时间(T)")
MenuItem exit=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_Q))
MenuItem tdate=new MenuItem("日期/时间",new MenuShortcut(KeyEvent.VK_D))
MenuItem renew=new MenuItem("新建", new MenuShortcut(KeyEvent.VK_N))
MenuItem save=new MenuItem("磨闹保存", new MenuShortcut(KeyEvent.VK_S))
MenuItem open=new MenuItem("打祥游兆开谨租",new MenuShortcut(KeyEvent.VK_O))
MenuItem copy=new MenuItem("复制",new MenuShortcut(KeyEvent.VK_C))
MenuItem paste=new MenuItem("粘贴",new MenuShortcut(KeyEvent.VK_V))
MenuItem cut=new MenuItem("剪切",new MenuShortcut(KeyEvent.VK_X))
MenuItem helptopic=new MenuItem("主题",new MenuShortcut(KeyEvent.VK_T))
helptopic.addActionListener(this)
dialog=new Dialog(this, "字体设置")dialog.setSize(300,300)
dialog1=new Dialog(this, "帮助主题")dialog1.setBounds(250,100,300,400)
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dialog.setVisible(false)}
})
dialog1.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dialog1.setVisible(false)}
})
//dialog.setBackground(Color.WHITE)
dialog.setLayout(null)dialog.setBounds(170,150,450,300)
Label L1=new Label("字体(F)")Label L2=new Label("字形(Y)")
Label L3=new Label("大小(S)")TextArea ta1=new TextArea(5,5)
ta1=new TextArea("至于记事本的主题嘛,因为本人的水平有限, 所有不能为大家作过多的指导,望大家见谅!")//在这里我想加一个文本框用来放帮助信息。
ce1=new Choice()ce2=new Choice()
ce3=new Choice()
btn1=new Button("确定")btn2=new Button("取消")btn1.addActionListener(this)btn2.addActionListener(this)
btn3=new Button("颜色")
btn3.addActionListener(this)
ce1.add("华文行楷")ce1.add("华文中宋")ce1.add("华文新魏")ce1.add("华文细黑")ce1.add("宋体")ce1.add("方正姚体")
ce1.add("幼圆")ce1.add("隶书")ce1.add("楷体-GB2312")ce1.add("华文行楷")ce1.add("华文彩云")ce1.add("仿宋-GB2312")
ce2.add("粗体")ce2.add("斜体")ce2.add("常规")
for(int n1=0n1<=100n1++)
{ce3.add(""+n1)}
dialog.add(p1)p1.setBounds(5,30,440,35)p1.setLayout( null)//p1.setBackground(Color.red)
dialog.add(p2)p2.setBounds(5,65,440,27)dialog.add(p3)p3.setBounds(5,90,440,40)//p4.setBounds(5,120,440,138)
p1.setLayout( null)
p1.add(L1)L1.setBounds(5,15,50,25)p1.add(L2)L2.setBounds(150,15,50,25)
p1.add(L3)L3.setBounds(250,15,50,25)p2.setLayout( null)//p2.setBackground(Color.yellow)
p2.add(ce1)ce1.setBounds(5,0,130,25)p2.add(ce2)ce2.setBounds(160,0,90,25)p2.add(ce3)ce3.setBounds(260,0,75,25)
p2.add(btn1)btn1.setBounds(360,0,75,25)
p3.setLayout( null)p3.add(btn2)btn2.setBounds(360,10,75,25)p3.add(btn3)btn3.setBounds(160,10,75,25)
dialog.add(p4)p4.setBackground(Color.yellow)p4.setBounds(5,120,440,138)
dialog1.add(ta1)
//设置字体的下拉情况
MenuItem Font=new MenuItem("字体")//给字体加监听器
Font.addActionListener(this)
MenuItem Replace=new MenuItem("替换")MenuItem Seek=new MenuItem("查找")
//首要添加组件的情况
mb.add(file)mb.add(edit)mb.add(pattern)mb.add(time)mb.add(help)
file.add(renew)file.addSeparator()file.add(open)time.add(tdate)
file.addSeparator()file.add(exit)file.addSeparator()file.add(save)
edit.add(copy)edit.addSeparator()edit.add(paste)edit.addSeparator()edit.add(cut)
pattern.add(Font) pattern.add(Replace)
pattern.add(Seek)help.add(helptopic)
//给必要的组件加上监听器
renew.addActionListener( this)tdate.addActionListener(this)
open.addActionListener(this)save.addActionListener(this)exit.addActionListener(this)
cut.addActionListener(this)copy.addActionListener(this)paste.addActionListener(this)
ce1.addItemListener(this)ce2.addItemListener(this)ce3.addItemListener(this)
//添加必要的组件
add(ta,"Center")ta.setBackground(new Color(249,255,234))
setSize(700,500)
setVisible(true)
this.validate()
}
public void itemStateChanged(ItemEvent e) {
if (ce2.getSelectedItem().equals("粗体"))
{ k=1st1=ce1.getSelectedItem()st2=ce3.getSelectedItem()}
if (ce2.getSelectedItem().equals("斜体"))
{ k=2st1=ce1.getSelectedItem()st2=ce3.getSelectedItem()}
if (ce2.getSelectedItem().equals("常规"))
{ k=3st1=ce1.getSelectedItem()st2=ce3.getSelectedItem()}
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("新建"))
{System.out.println("new ")
ta.setText("")}
if(e.getActionCommand().equals("退出"))
{ System.exit(0)}
try
{
if(e.getActionCommand().equals("打开"))
openText()
if(e.getActionCommand().equals("保存"))
saveText()
}catch(IOException e1){}
if(e.getActionCommand().equals("复制"))
{ str=ta.getSelectedText()}
if(e.getActionCommand().equals("粘贴"))
{ ta.insert(str,ta.getCaretPosition())}
if(e.getActionCommand().equals("剪切"))
{str=ta.getSelectedText()ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd())
}
if(e.getActionCommand().equals("字体"))
{dialog.setVisible(true)}
if(e.getActionCommand().equals("主题"))
{dialog1.setVisible(true)}
if(e.getActionCommand().equals("颜色"))
{
Color clr=JColorChooser.showDialog(this,"颜色对话框",null)
ta.setForeground(clr)
}
if(e.getSource()==btn2)
dialog.setVisible(false)
if(e.getSource()==btn1)
{
if(k==1)
{ ta.setFont(new Font(st1,Font.BOLD,Integer.parseInt(st2)))}
if(k==2)
{ ta.setFont(new Font(st1,Font.ITALIC,Integer.parseInt(st2)))}
if(k==3)
{ ta.setFont(new Font(st1,Font.PLAIN,Integer.parseInt(st2)))}
dialog.setVisible(false)
}
//if (e.getActionCommand().equals("日期/时间"))
//ta.setText(ta.getText()+""+date)
if(e.getActionCommand().equals("日期/时间"))
{
Calendar c1 =Calendar.getInstance()
int y = c1.get(Calendar.YEAR)
int m = c1.get(Calendar.MONTH)
int d = c1.get(Calendar.DATE)
int h = c1.get(Calendar.HOUR)
int m1 = c1.get(Calendar.MINUTE)
int m2 = m+1
ta.setText(ta.getText()+""+(y+"年"+m2+"月"+d+"日"+h+":"+m1))
}
}
public void openText() throws IOException
{
FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.LOAD)
fd.setVisible(true)FileInputStream fis=new FileInputStream(fd.getDirectory()+fd.getFile())
ta.setText("")
int n=0
while((n=fis.read())!=-1)
ta.append(""+(char)n)
fis.close()
}
public void saveText() throws IOException
{
FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.SAVE)
fd.setVisible(true)
FileOutputStream out=new FileOutputStream(fd.getDirectory()+fd.getFile()+".txt")
String str=ta.getText()
String str1=ta.getText()
for(int n=0n<str.length()n++)
out.write((byte)str.charAt(n))
out.close()
}
public static void main(String[] args)
{
Editor f=new Editor(" 记事本程序")
//添加时间代码
Date date=new Date()
System.out.print(date)
/*Calendar now=Calendar.getInstance()
int year=now.get(Calendar.YEAR)
int month=now.get(Calendar.MONTH)+1
int day=now.get(Calendar.DATE)
System.out.print(year+"年"+month+"月"+day+"日")
int week=now.get(Calendar.DAY_OF_WEEK)
String str="日一二三四五六"//星期1-7
int i=2*(week-1)
System.out.println("星期"+str.substring(i,i+2))//对应中文的下标*/
}
class dialog extends Dialog
{
public dialog()
{
super(dialog, "字体设置")
class Mycanvas extends Canvas
{
Toolkit tk
Image img
Mycanvas()
{
setSize(440,138)
tk=getToolkit()
img=tk.getImage("photo.jpg")
}
public void paint(Graphics g)
{
g.drawImage(img,5,120,440,138,this)
}
}
}
}
}
import java.io.*import java.awt.*
import java.awt.event.*
import javax.swing.*
import javax.swing.undo.CannotRedoException
import javax.swing.undo.UndoManager
import java.util.Date
import java.text.SimpleDateFormat
public class Notepad extends JFrame {
// 菜单
JMenuBar menub = new JMenuBar()
// 显示纯文本的多行区域
JTextArea text = new JTextArea()
JMenu files = new JMenu("文件(F)")
JMenu edit = new JMenu("编辑(E)")
JMenu formats = new JMenu("格式(O)")
JMenu help = new JMenu("帮助(H)")
JMenuItem newFile = new JMenuItem("新建(N)")
JMenuItem open = new JMenuItem("打开(O)"纳轮)
JMenuItem save = new JMenuItem("保存(S)")
JMenuItem saveAs = new JMenuItem("另存为(A)")
JMenuItem exit = new JMenuItem("退出(X)")
JMenuItem undo = new JMenuItem("撤销(U)")
JMenuItem cut = new JMenuItem("剪切(T)")
JMenuItem copy = new JMenuItem("复祥含制(C)")
JMenuItem paste = new JMenuItem("粘贴(P)")
JMenuItem selectAll = new JMenuItem("全选(A)")
JMenuItem timeDate = new JMenuItem("时间/日期(D)")
JCheckBoxMenuItem lineWrap = new JCheckBoxMenuItem("自动换行(M)")
JMenuItem fonts = new JMenuItem("字体")
JMenuItem about = new JMenuItem("关于记事本(A)")
JFrame th = this
String name
String openedPath = null
boolean opened = false
boolean reworked = false
UndoManager undoManager = new UndoManager()
// 初始化窗体
public Notepad(String name) {
super(name)
this.name = name
int x, y
// 得到用户屏幕大小
Dimension size = Toolkit.getDefaultToolkit().getScreenSize()
x = (size.width - 600) / 2
y = (size.height - 400) / 2
setSize(600, 400)
// 让程序谨茄笑界面显示在屏幕中央
setLocation(x, y)
// 将此窗口的最小大小设置为一个常量值。
setMinimumSize(new Dimension(250, 150))
setDefaultCloseOperation(EXIT_ON_CLOSE)
}
// 初始化布局
void init() {
files.setMnemonic('F')
edit.setMnemonic('E')
formats.setMnemonic('O')
help.setMnemonic('H')
newFile.setMnemonic('N')
open.setMnemonic('O')
save.setMnemonic('S')
saveAs.setMnemonic('A')
exit.setMnemonic('X')
undo.setMnemonic('U')
cut.setMnemonic('T')
copy.setMnemonic('C')
paste.setMnemonic('P')
selectAll.setMnemonic('A')
timeDate.setMnemonic('D')
lineWrap.setMnemonic('M')
about.setMnemonic('A')
// 为控件添加助记符
newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
InputEvent.CTRL_MASK))
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK))
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK))
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
InputEvent.CTRL_MASK))
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK))
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK))
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
InputEvent.CTRL_MASK))
selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
InputEvent.CTRL_MASK))
// 为控件添加快捷键
timeDate.setAccelerator(KeyStroke.getKeyStroke("F5"))
files.add(newFile)
files.add(open)
files.add(save)
files.add(saveAs)
files.addSeparator()
files.add(exit)
edit.add(undo)
edit.addSeparator()
edit.add(cut)
edit.add(copy)
edit.add(paste)
edit.addSeparator()
edit.add(selectAll)
edit.add(timeDate)
formats.add(lineWrap)
formats.add(fonts)
help.add(about)
menub.add(files)
menub.add(edit)
menub.add(formats)
menub.add(help)
setJMenuBar(menub)
getContentPane().add(new JScrollPane(text))
Listen listen = new Listen()
Listen1 listen1 = new Listen1()
// 为控件添加事件侦听器
newFile.addActionListener(listen)
undo.addActionListener(listen)
open.addActionListener(listen)
save.addActionListener(listen)
saveAs.addActionListener(listen)
exit.addActionListener(listen)
cut.addActionListener(listen)
copy.addActionListener(listen)
paste.addActionListener(listen)
selectAll.addActionListener(listen)
timeDate.addActionListener(listen)
lineWrap.addActionListener(listen)
about.addActionListener(listen)
open.addActionListener(listen1)
save.addActionListener(listen1)
saveAs.addActionListener(listen1)
// 暂时没有实现的功能 :设置字体
//undo功能没有实现
fonts.setEnabled(false)
}
class Listen implements ActionListener {
// 实现用于一般 *** 作的事件侦听器
public void actionPerformed(ActionEvent e) {
Object source = e.getSource()
if (source == newFile) {
text.setText("")
// 设置标题
th.setTitle(name)
openedPath = null
opened = false
} else if (source == exit)
setVisible(false)
else if (source == undo)
try {
//此功能没有实现 撤销要用栈?
// undo.setEnabled(undoManager.canUndo())
undoManager.undo()
} catch (CannotRedoException cre) {
cre.printStackTrace()
}
else if (source == selectAll)
text.selectAll()
else if (source == cut)
text.cut()
else if (source == copy)
text.copy()
else if (source == paste)
text.paste()
else if (source == lineWrap)
// 设置文本区的换行策略(获取文本区的换行策略)
text.setLineWrap(!text.getLineWrap())
else if (source == about) {
String message = "--------\n版本:1.0\n作者:时超" +
"\n撤销功能要用堆栈存贮 *** 作" +
"\n还有字体格式"+
"\n暂时没有实现" +
"\n\n感谢您的使用"
JOptionPane.showMessageDialog(th, message, "关于",
JOptionPane.PLAIN_MESSAGE)
} else if (source == timeDate) {
Date nowTime = new Date()
SimpleDateFormat times = new SimpleDateFormat(
"HH:mm yyyy-MM-dd")
text.insert(times.format(nowTime), text.getCaretPosition())
}
}
}
class Listen1 implements ActionListener {
// 实现用于对文件进行 *** 作的事件侦听器
public void actionPerformed(ActionEvent e) {
Object source = e.getSource()
// 打开文件事件
if (source == open) {
// 显示对话窗口 以便选择文件
FileDialog openFile = new FileDialog(th, "打开文件",
FileDialog.LOAD)
openFile.setVisible(true)
// 获取文件路径
String filePath = openFile.getDirectory() + openFile.getFile()
try {
FileInputStream fis = new FileInputStream(filePath)
byte[] content = new byte[fis.available()]
fis.read(content)
text.setText(new String(content))
// 设置 TextComponent 的文本插入符的位置
text.setCaretPosition(0)
if (openFile.getFile() != null) {
th.setTitle(openFile.getFile() + name)
openedPath = filePath
opened = true
}
fis.close()
} catch (Exception ex) {
ex.printStackTrace()
}
opened = true
}
// 保存及另存为事件
else if (source == save || source == saveAs) {
String savePath = openedPath
if (savePath == null || source == saveAs) {
// 如果 mode 的值为 LOAD,那么文件对话框将查找要读取的文件,所显示的文件是当前目录中的文件
// 如果 mode 的值为 SAVE,则文件对话框将查找要写入文件的位置。
FileDialog saveFile = new FileDialog(th, "保存文件",
FileDialog.SAVE)
saveFile.setVisible(true)
savePath = saveFile.getDirectory() + saveFile.getFile()
}
try {
FileOutputStream fos = new FileOutputStream(savePath)
fos.write(text.getText().getBytes())
fos.close()
} catch (Exception ex) {
ex.printStackTrace()
}
if (source == save)
openedPath = savePath
}
}
}
public static void main(String[] args) {
try {
// 使用当前线程的上下文类加载器加载给定类名称所指定的 LookAndFeel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
} catch (Exception e) {
System.err.println("Couldn't use the system look and feel:" + e)
}
String name = ("--记事本 版本1.0--")
Notepad note = new Notepad(name)
note.init()
note.setVisible(true)
}
}
1、首先在电脑中新建一个记事本,将记事本的后缀改为“.java”,如下图所示。
2、然后使用记事本的方乎做式打开,输入java程序代码,如下图所示。
3、接着在键盘上按“win+R”快捷键键打开消顷信运行,输拿轮入“cmd”,如下图所示。
4、在命令行输入“D:”,按“Enter”键进去D盘,再输入“cd Desktop”进去Desktop文件夹,如下图所示。
5、最后再输入“javac Test.java”,按“Enter”键编译java程序,如下图所示就完成了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)