「记事本java课程设计」基于java的记事本程序设计
今天给各位分享记事本java课程设计的知识,其中也会对基于java的记事本程序设计进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、如何用JAVA编写简单的记事本程序?
- 2、使用记事本开发java程序的步骤
- 3、用java编辑日历记事本
- 4、java程序设计,编写一个记事本程序
- 5、使用记事本编写JAVA程序,并运行输出结果,具体的实现步骤是什么?
如何用JAVA编写简单的记事本程序?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.datatransfer.*;public class NewEdit
{
public static void main(String args[])
{
MyFrame EditFrame=new MyFrame();
EditFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EditFrame.setVisible(true);
}
}class MyFrame extends JFrame
{
public MyFrame()
{ Dimension screenSize=toolKit.getScreenSize();
int screenHeight=screenSize.height;
int screenWidth=screenSize.width;
setSize(screenWidth/2,screenHeight/2);
setLocation(screenWidth/4,screenHeight/4);
Image img=toolKit.getImage("icon.gif");
setIconImage(img);
setTitle("MyEdit");
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu=new JMenu("文件");
JMenu editMenu=new JMenu("编辑");
JMenu helpMenu=new JMenu("帮助");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newItem=new JMenuItem("新建");
newItem.addActionListener(new NewListener());
JMenuItem openItem=new JMenuItem("打开");
openItem.addActionListener(new OpenListener());
JMenuItem saveItem=new JMenuItem("保存");
saveItem.addActionListener(new SaveListener());
JMenuItem escItem=new JMenuItem("退出");
escItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent myE)
{
System.exit(0);
}
});
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(escItem);
JMenuItem allItem=new JMenuItem("全选");
//JMenuItem copyItem=new JMenuItem("复制");
//CopyAction copyAction=new CopyAction();
//copyItem.addActionListener(copyAction);
JMenuItem cutItem=new JMenuItem("剪切");
//JMenuItem pasteItem=new JMenuItem("粘贴");
//Action pasteAction=new PasteAction();
//pasteItem.addActionListener(pasteAction);
editMenu.add(allItem);
allItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent myE)
{
textArea.selectAll();
}
});
CopyAction copyAction=new CopyAction("剪切");
editMenu.add(copyAction);
CutAction cutAction=new CutAction("复制");
editMenu.add(cutAction);
Action pasteAction=new PasteAction("粘贴");
editMenu.add(pasteAction);
popup=new JPopupMenu();
popup.add(copyAction);
popup.add(pasteAction);
popup.add(cutAction);
textArea = new JTextArea();
textArea.add(popup);
textArea.addMouseListener(new
MouseListener(){
public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3)
{
popup.show(textArea, e.getX(), e.getY());
}
} public void mouseReleased(MouseEvent e) {} });//可尝试用MouseAdapter,代码可能更简单
JScrollPane scroller = new JScrollPane(textArea);
add(scroller);
//OR--this.getContentPane().add(scroller)
//scroller.setComponentPopupMenu(popup);
//textArea.setInheritsPopupMenu(true);
//scroller.addMouseListener(new MouseAdapter(){});
}
public void writeFile(String fileName)
{
try
{
File file = new File(fileName);
FileWriter write = new FileWriter(file);
write.write(textArea.getText());
write.close();
}
catch(Exception e){
System.out.println("Error closing file!");
}
}
public void openFile(String fileName)
{
try
{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content,charsRead,size-charsRead);
readIn.close();
textArea.setText(new String(content,0,charsRead));
}
catch(IOException e)
{
System.out.println("Error opening file!");
}
}
private class NewListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
textArea.setText("");
}
}
private class OpenListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName != null)
{
openFile(fileName);
}
}
}
private class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
saveFileDialog.setVisible(true);
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
if(fileName !=null)
{
writeFile(fileName);
}
}
}
private class CutAction extends AbstractAction
{
public CutAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection,null);
textArea.replaceRange("",textArea.getSelectionStart(),
textArea.getSelectionEnd());
}
}
private class CopyAction extends AbstractAction
{
public CopyAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
String text = textArea.getSelectedText();
StringSelection selection= new StringSelection(text);
clipboard.setContents(selection,null);
}
}
private class PasteAction extends AbstractAction
{
public PasteAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
Transferable contents = clipboard.getContents(this);
if(contents==null)
return;
String text;
text="";
try
{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception ex){}
textArea.replaceRange(text,
textArea.getSelectionStart(),textArea.getSelectionEnd());
}
}
private JTextArea textArea;
private JPopupMenu popup;
private String fileName="";
private FileDialog openFileDialog=new FileDialog(this,"OpenFile",FileDialog.LOAD); private FileDialog saveFileDialog=new FileDialog(this,"SaveFile",FileDialog.SAVE);
Toolkit toolKit = Toolkit.getDefaultToolkit();
private Clipboard clipboard = toolKit.getSystemClipboard();}
使用记事本开发java程序的步骤
1.首先,先看看电脑是否设置为显示已知文件扩展名。
如果没有,要先设置一下。点击计算机,工具(T),文件夹选项(O),
查看,在高级设置一栏里找到隐藏已知文件扩展名,把前面的钩钩去掉。
2.点击鼠标右键,新建,文本文档,命名为:MyFirstJava.java
然后编写第一个程序代码,注意类名和文件名要一致,
还有划线部分首字母要大写。
3.写好程序之后,点击右上角的文件,另存为,出现下面的页面。
然后,要看你的jdk安装在哪(我的安装在D盘)。就可以把文件保存在D盘,还有文件名设置为MyFirstJava.java,保存类型设置为:所有文件。
]
4.保存好之后,关闭文件。点开时,在搜索程序和文件中输入cmd,
然后,Enter,Enter,就会出现下面的页面。
5.在控制台先输入d:(因为我的是在D盘,若是在C盘就输入c:,F盘就输入f:);
回车,再输入javac MyFirstJava.java(注意javac后空一格);
回车,在输入java MyFirstJava(java后面也有一个空格)。
这时,就会出来结果了!:)
6.运行结束都在D盘中(安装jdk的地方)
会出现一个MyFirstJave.Class 文件。
用java编辑日历记事本
html
head
titleHTML/title
style type="text/css"
#calender{
width:150;
height:170;
}
#currentTime{
border:1px dotted #999999;
width:80px;
text-align:center;
}
button{
border:1px solid #7baddd;
font:9pt Verdana,"宋体";
cursor: pointer;
}
table{
width:280px;
text-align:center;
}
/style
script type="text/javascript"
var daysOfMonth = new Array();
daysOfMonth[0] = 31;
daysOfMonth[1] = 28;
daysOfMonth[2] = 31;
daysOfMonth[3] = 30;
daysOfMonth[4] = 31;
daysOfMonth[5] = 30;
daysOfMonth[6] = 31;
daysOfMonth[7] = 31;
daysOfMonth[8] = 30;
daysOfMonth[9] = 31;
daysOfMonth[10] = 30;
daysOfMonth[11] = 31;
var monthchange=0;
var nowTime = new Date();
var nowYear =nowTime.getFullYear();
var nowMonth =nowTime.getMonth();
var nowDate =nowTime.getDate();
function genernate(year,month)
{
if(year==null||month==null)
{
year = nowYear;
month = nowMonth;
}
nowTime.setFullYear(year,month,1); if((nowYear%400==0)||((nowYear%4==0)((nowYear%100)!=0)))
daysOfMonth[1]=29;
else
daysOfMonth[1]=28;
var firstDay = nowTime.getDay();
var day = 1;
var result = "table";
result+="theadtrth日/thth一/thth二/thth三/thth四/thth五/thth六/th/tr/thead";
result+="tbody";
for(var i=0;i6;i++)
{
result+="tr";
if(i!=0) firstDay=-1;
for(var j=0;j7day= daysOfMonth[month];j++)
{
if(jfirstDay)
result+="td /td";
else
{
if((day==nowDate)(monthchange==0))
result+="tdspan style=\"color:red;border:1px dotted #666666\""+day+"/span/td";
else
result+="td"+day+"/td";
day++;
}
}
result+="tr";
}
result+="/tbody";
result+="/table"; document.getElementById("calender").innerHTML = result;
setContent();
}
function preyear()
{
monthchange-=12;
genernate(--nowYear,nowMonth);
}
function nextyear()
{
monthchange+=12;
genernate(++nowYear,nowMonth);
}
function premonth()
{
--monthchange;
if(nowMonth==0)
{
nowMonth=11;
nowYear-=1;
}
else
nowMonth-=1;
genernate(nowYear,nowMonth);
}
function nextmonth()
{
++monthchange;
if(nowMonth==11)
{
nowMonth=0;
nowYear+=1;
}
else
nowMonth+=1;
genernate(nowYear,nowMonth);
}
function setContent()
{
document.getElementById("contentYear").innerHTML = nowYear;
document.getElementById("contentMonth").innerHTML = nowMonth+1;
}
function getCurrentTime()
{
var time = new Date();
var hours = time.getHours();
var minutes = time.getMinutes();
var second = time.getSeconds();
if((hours==0)(minutes==0)(second==0))
{
nowDate = time.getDate();
genernate(time.getFullYear(),time.getMonth());
}
document.getElementById("currentTime").innerHTML = time.toLocaleTimeString();
setTimeout("getCurrentTime()",500);
}
function currentDate()
{
var todayTime = new Date();
var todayYear =todayTime.getFullYear();
var todayMonth =todayTime.getMonth();
nowDate = todayTime.getDate();
monthchange=0;
nowYear = todayYear;
nowMonth = todayMonth;
genernate(todayYear,todayMonth);
}
/script
/head
body onload="genernate();getCurrentTime();"
h2自己做的小日历/h2
div
divspanspan id="contentYear"/span 年 span id="contentMonth"/span 月/span/div
divbutton onclick="preyear();"上一年/buttonbutton onclick="nextyear();"下一年/buttonbutton onclick="premonth();"上一月/buttonbutton onclick="nextmonth();"下一月/buttonbutton onclick="currentDate();"回到今天/button/div
div id="calender"/div
/div
h3当前时间/h3
div id="currentTime"div
/body
/html
java程序设计,编写一个记事本程序
全实现,程序太长,发不上去,先实现基本的 图形用户界面
import java.awt.*;
import java.awt.event.*;
public class TestMenu {
public static void main (String[] args) {
new MenuFrame("新建"+" "+"文本文档"+".txt"+" "+"-"+" "+"记事本").launchFrame();
}
}
class MenuFrame extends Frame {
MenuBar mb = null;
MenuFrame (String s) {
super (s);
}
public void launchFrame() {
Menu file = new Menu ("文件");
Menu edit = new Menu ("编辑");
Menu format = new Menu ("格式");
Menu help = new Menu ("帮助");
MenuItem newItem = new MenuItem ("新建");
newItem.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
final Frame ff = new Frame ("记事本");
ff.setMenuBar(mb);
ff.setBounds (300,300,400,200);
ff.setVisible (true);
ff. addWindowListener (new WindowAdapter () {
public void windowClosing(WindowEvent e) {
ff.setVisible (false);
}
} );
}
});
MenuItem saveItem = new MenuItem ("保存");
MenuItem exitItem = new MenuItem ("退出");
MenuItem helpTitle = new MenuItem ("帮助主题");
MenuItem line = new MenuItem ("-");
MenuItem about = new MenuItem ("关于记事本");
MenuItem copy = new MenuItem ("粘贴");
MenuItem serach = new MenuItem ("查找");
edit.add (copy);
edit.add (serach);
help.add (helpTitle);
help.add (line);
help.add (about);
exitItem.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
System.exit (0);
}
} );
file.add (newItem);
file.add (saveItem);
file.add (exitItem);
mb = new MenuBar();
mb.add (file);
mb.add (edit);
mb.add (format);
mb.add (help);
addWindowListener (new WindowAdapter () {
public void windowClosing(WindowEvent e) {
System.exit (0);
}
} );
setLayout (new FlowLayout());
setMenuBar (mb);
setBounds (300,300,400,200);
setVisible (true);
}
}
使用记事本编写JAVA程序,并运行输出结果,具体的实现步骤是什么?
1、首先在电脑中新建一个记事本,将记事本的后缀改为“.java”,如下图所示。
2、然后使用记事本的方式打开,输入java程序代码,如下图所示。
3、接着在键盘上按“win+R”快捷键键打开运行,输入“cmd”,如下图所示。
4、在命令行输入“D:”,按“Enter”键进去D盘,再输入“cd Desktop”进去Desktop文件夹,如下图所示。
5、最后再输入“javac Test.java”,按“Enter”键编译java程序,如下图所示就完成了。
关于记事本java课程设计和基于java的记事本程序设计的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-25,除非注明,否则均为
原创文章,转载请注明出处。