java打开记事本

如何把记事本文件变成JAVA源文件

1、打开电脑,首先点击开始按钮选择打开记事本选项按钮。

2、记事本页面后写好代码之后,点击左上角的文件选项按钮。

3、写好之后点击文件下的保存选项按钮

4、这时候就要文件名的后缀改成java

5、改好之后点击保存,之后就可以变成JAVA源文件了。

用java编写记事本程序,可以实现新建、打开、保存、退出、复制、粘贴、剪切、全选。

import javax.swing.*;

import javax.swing.filechooser.FileFilter;

import java.awt.event.*;

import java.awt.*;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Enumeration;

import java.util.Hashtable;

public class MyText extends JFrame implements ActionListener{

private JLabel lb1;

private JMenuBar mb;

private JMenu 文件, 编辑, 格式, 帮助;

private JMenuItem 新建, 打开, 保存, 退出, 复制, 粘贴, 剪切, 全选, 字体, 关于;

private JTextArea editorArea;

private boolean isDirty = false;

private String strFileName = “未命名”;

private static final String EDITOR_NAME = “MyText”;

public MyText() {

super();

mb = new JMenuBar();

文件 = new JMenu(“文件”);

编辑 = new JMenu(“编辑”);

格式 = new JMenu(“格式”);

帮助 = new JMenu(“帮助”);

新建 = new JMenuItem(“新建”);

新建.addActionListener(this);

打开 = new JMenuItem(“打开”);

打开.addActionListener(this);

保存 = new JMenuItem(“保存”);

保存.addActionListener(this);

退出 = new JMenuItem(“退出”);

退出.addActionListener(this);

复制 = new JMenuItem(“复制”);

复制.addActionListener(this);

粘贴 = new JMenuItem(“粘贴”);

粘贴.addActionListener(this);

剪切 = new JMenuItem(“剪切”);

剪切.addActionListener(this);

全选 = new JMenuItem(“全选”);

全选.addActionListener(this);

字体 = new JMenuItem(“字体”);

字体.addActionListener(this);

关于 = new JMenuItem(“关于”);

关于.addActionListener(this);

mb.add(文件);

mb.add(编辑);

mb.add(格式);

mb.add(帮助);

文件.add(新建);

文件.add(打开);

文件.add(保存);

文件.add(退出);

编辑.add(复制);

编辑.add(粘贴);

编辑.add(剪切);

编辑.add(全选);

格式.add(字体);

帮助.add(关于);

setJMenuBar(mb);

Container container = getContentPane();

editorArea = new JTextArea();

editorArea.setLineWrap(true);

editorArea.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

if(!isDirty()){

setDirty(true);

}

}

});

JScrollPane scrollPane = new JScrollPane(editorArea);

container.add(scrollPane);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){

dispose();

}

});

setTitle(formatEditorTitle());

setSize(600, 400);

setVisible(true);

}

private boolean isDirty() {

return isDirty;

}

private void setDirty(boolean isDirty) {

this.isDirty = isDirty;

setTitle(formatEditorTitle());

}

public static void main(String args[]) {

@SuppressWarnings(“unused”)

MyText app = new MyText();

}

public void actionPerformed(ActionEvent e) {

JMenuItem item = (JMenuItem)e.getSource();

if(item.equals(新建)){

if(isDirty()){

int ret = JOptionPane.showConfirmDialog(getContentPane(), “文件内容已经变动,是否保存?”, “MyText”, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);

if(ret == JOptionPane.OK_OPTION){

saveFile();

}else if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION){

return;

}

}

clearEditorArea();

setDirty(false);

}else if(item.equals(打开)){

if(isDirty()){

int ret = JOptionPane.showConfirmDialog(getContentPane(), “文件内容已经变动,是否保存?”, “MyText”, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);

if(ret == JOptionPane.OK_OPTION){

saveFile();

}else if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION){

return;

}

}

openFile();

}else if(item.equals(保存)){

saveFile();

}else if(item.equals(退出)){

dispose();

}else if(item.equals(复制)){

editorArea.copy();

}else if(item.equals(剪切)){

editorArea.cut();

}else if(item.equals(粘贴)){

editorArea.paste();

}else if(item.equals(全选)){

editorArea.selectAll();

}else if(item.equals(字体)){

FontDialog font = new FontDialog(this, editorArea.getFont());

editorArea.setFont(font.getSelectedFont());

}else if(item.equals(关于)){

AboutDialog about = new AboutDialog(this);

about.setVisible(true);

}

}

private String getFileName() {

return strFileName;

}

private void setFileName(String strFileName) {

this.strFileName = strFileName;

}

public String formatEditorTitle(){

StringBuffer strFileNm = new StringBuffer(getFileName());

strFileNm.append(isDirty()?”*”:””);

strFileNm.append(” – “);

strFileNm.append(EDITOR_NAME);

return strFileNm.toString();

}

private void clearEditorArea(){

editorArea.selectAll();

editorArea.replaceSelection(“”);

}

private void openFile(){

JFileChooser openDialog = new JFileChooser();

openDialog.setFileFilter(new TxtFileFilter());

if(openDialog.showOpenDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){

File file = openDialog.getSelectedFile();

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(file));

String buff = br.readLine();

clearEditorArea();

while(buff != null){

editorArea.append(buff);

editorArea.append(“\n”);

buff = br.readLine();

}

} catch (FileNotFoundException e1) {

e1.printStackTrace();

} catch (IOException ioe) {

ioe.printStackTrace();

} finally{

try{

if(br != null)

br.close();

} catch (IOException ioe){

ioe.printStackTrace();

}

}

}

}

private void saveFile(){

JFileChooser saveDialog = new JFileChooser();

saveDialog.setFileFilter(new TxtFileFilter());

if(saveDialog.showSaveDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){

File file = saveDialog.getSelectedFile();

BufferedWriter bw = null;

try {

bw = new BufferedWriter(new FileWriter(file));

String buff = editorArea.getText();

bw.write(buff);

} catch (IOException ioe) {

ioe.printStackTrace();

} finally{

try{

if(bw != null)

bw.close();

} catch (IOException ioe){

ioe.printStackTrace();

}

}

}

}

class TxtFileFilter extends FileFilter{

@Override

public boolean accept(File f) {

return f.isDirectory() || f.getName().toLowerCase().endsWith(“.txt”);

}

@Override

public String getDescription() {

return “*.txt(文本文件)”;

}

}

class FontDialog extends JDialog{

private JComboBox cb_FontSize;

private JComboBox cb_FontStyle;

private JComboBox cb_FontNm;

private Font font;

HashtableInteger, String style = new HashtableInteger, String();

public FontDialog(){

this(null, null);

}

public FontDialog(Frame owner, Font font){

super(owner);

this.font = font == null?getFont():font;

setTitle(“字体选择框”);

setModal(true);

setResizable(false);

setSize(326, 164);

getContentPane().setLayout(null);

final JLabel lb_FontNm = new JLabel();

lb_FontNm.setText(“字体名称”);

lb_FontNm.setBounds(10, 10, 66, 16);

getContentPane().add(lb_FontNm);

cb_FontNm = new JComboBox();

cb_FontNm.setBounds(10, 28, 133, 25);

getContentPane().add(cb_FontNm);

cb_FontStyle = new JComboBox();

cb_FontStyle.setBounds(169, 28, 66, 25);

getContentPane().add(cb_FontStyle);

cb_FontSize = new JComboBox();

cb_FontSize.setBounds(258, 28, 53, 25);

getContentPane().add(cb_FontSize);

final JButton btn_OK = new JButton();

btn_OK.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int styleCode = 0;

for(EnumerationInteger i = style.keys();i.hasMoreElements();){

styleCode = i.nextElement();

if(style.get(styleCode).equals(cb_FontStyle.getSelectedItem()))

break;

}

Font font = new Font(cb_FontNm.getSelectedItem().toString(), styleCode, ((Integer)cb_FontSize.getSelectedItem()).intValue());

setSelectedFont(font);

dispose();

}

});

btn_OK.setText(“确定”);

btn_OK.setBounds(58, 83, 76, 26);

getContentPane().add(btn_OK);

final JButton btn_Cancel = new JButton();

btn_Cancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

dispose();

}

});

btn_Cancel.setText(“取消”);

btn_Cancel.setBounds(169, 83, 76, 26);

getContentPane().add(btn_Cancel);

final JLabel lb_FontStyle = new JLabel();

lb_FontStyle.setText(“字体样式”);

lb_FontStyle.setBounds(169, 10, 66, 16);

getContentPane().add(lb_FontStyle);

final JLabel lb_FontSize = new JLabel();

lb_FontSize.setText(“字体大小”);

lb_FontSize.setBounds(258, 10, 66, 16);

getContentPane().add(lb_FontSize);

init();

setVisible(true);

}

private void init(){

GraphicsEnvironment gg=GraphicsEnvironment.getLocalGraphicsEnvironment();

String ss[]=gg.getAvailableFontFamilyNames();

for(String s : ss)

cb_FontNm.addItem(s);

if(font != null)

cb_FontNm.setSelectedItem(font.getFamily());

style.put(Font.PLAIN, “标准”);

style.put(Font.BOLD, “粗体”);

style.put(Font.ITALIC, “斜体”);

style.put(Font.BOLD+Font.ITALIC, “粗体斜体”);

cb_FontStyle.addItem(style.get(Font.PLAIN));

cb_FontStyle.addItem(style.get(Font.BOLD));

cb_FontStyle.addItem(style.get(Font.ITALIC));

cb_FontStyle.addItem(style.get(Font.BOLD+Font.ITALIC));

if(font != null)

cb_FontStyle.setSelectedItem(style.get(font.getStyle()));

for(int i=8;i23;i++)

cb_FontSize.addItem(i);

if(font != null)

cb_FontSize.setSelectedItem(font.getSize());

}

public Font getSelectedFont() {

return font;

}

public void setSelectedFont(Font font) {

this.font = font;

}

}

class AboutDialog extends JDialog{

public AboutDialog(JFrame owner){

super(owner);

setTitle(“关于”);

setSize(new Dimension(322, 163));

getContentPane().setLayout(null);

final JLabel version = new JLabel();

version.setText(“MyText 1.0”);

version.setBounds(74, 37, 66, 16);

getContentPane().add(version);

final JLabel copyright = new JLabel();

copyright.setText(“Copyright (C) 2010”);

copyright.setBounds(74, 59, 188, 16);

getContentPane().add(copyright);

final JSeparator separator = new JSeparator();

separator.setBounds(70, 90, 210, 2);

getContentPane().add(separator);

final JButton okButton = new JButton();

okButton.setBounds(235, 95, 50, 26);

getContentPane().add(okButton);

okButton.setText(“Ok”);

okButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

dispose();

}

});

}

}

}

java什么命令,使运行程序的时候会打开一个记事本?

可以使用 Runtime.getRuntime().exec(命令); 打开一个记事本。

如下面代码:

import java.io.IOException;

/**

* Runtime.getRuntime().exec(windows或linux命令);

*

* 下以打开windows记事本为例

*/

public class NowJava {

public static void main(String[] args) throws IOException {

//以新文件方式,打开windows记事本

Runtime.getRuntime().exec(“notepad”);

//打开已有文件nowjava.txt,打开windows记事本

Runtime.getRuntime().exec(“notepad c:\\nowjava.txt”);

}

}

使用记事本编写JAVA程序,并运行输出结果,具体的实现步骤是什么?

1、首先在电脑中新建一个记事本,将记事本的后缀改为“.java”,如下图所示。

2、然后使用记事本的方式打开,输入java程序代码,如下图所示。

3、接着在键盘上按“win+R”快捷键键打开运行,输入“cmd”,如下图所示。

4、在命令行输入“D:”,按“Enter”键进去D盘,再输入“cd Desktop”进去Desktop文件夹,如下图所示。

5、最后再输入“javac Test.java”,按“Enter”键编译java程序,如下图所示就完成了。

java打开记事本

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月27日 14:06:06
下一篇 2024年3月27日 14:17:26

相关推荐

  • 深入java虚拟机pdf,深入java虚拟机 中村成洋 pdf

    在linux环境下,java怎么实现从word格式转换为pdf格式 //设置当前使用的打印机,我的Adobe Distiller打印机名字为 Adobe PDF wordCom.setProperty( ActivePrinter , new Variant( Adobe PDF ));//设置printout的参数,将word文档打印为postscript…

    2024年5月23日
    4400
  • java截取指定长度字符串,java截取指定字符串之后的

    java中如何截取字符串中的指定一部分 第一个参数是开始截取的字符位置。(从0开始)第二个参数是结束字符的位置+1。(从0开始)indexof函数的作用是查找该字符串中的某个字的位置,并且返回。 int end);截取s中从begin开始至end结束时的字符串,并将其赋值给s;split讲解:java.lang.string.split split 方法 将…

    2024年5月23日
    4200
  • java绑定一个端口,java使用端口

    java如何多个service共用一个端口 你如果有多个项目的话,你可以把多个项目放到一个tomcat里面,这样端口相同使用项目名称来进行区分项目。你如果非要使用同一个,你也可以配置不同的域名导向不同的项目。就是访问的域名不同转接到的项目不同。 如果需要同时启动多个程序,要么修改tomcat的配置文件中的监听端口。要么修改jar包程序的监听端口。不能在一台服…

    2024年5月23日
    3500
  • java多线程并发编程基础,Java多线程并发执行返回

    电脑培训分享Java并发编程:核心理论 电脑培训发现本系列会从线程间协调的方式(wait、notify、notifyAll)、Synchronized及Volatile的本质入手,详细解释JDK为我们提供的每种并发工具和底层实现机制。 人们开始意识到了继承的众多缺点,开始努力用聚合代替继承。软件工程解决扩展性的重要原则就是抽象描述,直接使用的工具就是接口。接…

    2024年5月23日
    4700
  • 自学java找工作,自学java找工作需要包装简历吗

    自学java学多久可以找到工作 1、自学Java至少需要一年以上的时间才能达到找工作的水平。报班培训四到六个月的时间就可以找到一份不错的工作。 2、自学Java至少需要一年以上的时间才能达到找工作的水平。 3、如果要想找到一份Java相关的工作,需要至少学习5-6个月时间才能就业。Java开发需要掌握一些基础的编程语言知识,比如掌握面向对象的编程思想、基本的…

    2024年5月23日
    4300
  • java左移右移,java 左移

    java位移问题 1、思路:直接用Integer类的bit运算操作。 2、移位操作:左移:向左移位,符号后面的数字是移了多少位,移的位用0补齐,例如2进制数01111111左移一位后变为11111110,移位是字节操作。 3、Java 位运算 Java 位运算[转]一,Java 位运算表示方法: 在Java语言中,二进制数使用补码表示,最高位为符号位,正数的…

    2024年5月23日
    4200
  • java技术规范,java规范性要求

    现在主流的JAVA技术是什么? java最流行开发技术程序员必看 1 、Git Git一直是世界上最受欢迎的Java工具之一,也是Java开发人员最杰出的工具之一。Git是一个开源工具,是-种出色的分布式版本控制解决方案。 (1).Java基础语法、数组、类与对象、继承与多态、异常、范型、集合、流与文件、反射、枚举、自动装箱和注解。(2).Java面向对象编…

    2024年5月23日
    4000
  • javasocket编程,Java socket编程中,禁用nagle算法的参数

    Java进行并发多连接socket编程 1、Java可利用ServerSocket类对外部客户端提供多个socket接口。基本的做法是先创建一个ServerSocket实例,并绑定一个指定的端口,然后在这个实例上调用accept()方法等待客户端的连接请求。 2、Socket socket=server.accept(0;Thread handleThrea…

    2024年5月23日
    4600
  • java死亡,java死代码是什么意思

    我的世界传送回死亡点指令是什么? 1、下面就让我们一起来了解一下吧:我的世界回到死的地方的指令是输入/back,就可以回到死亡地点了,当然也可以看信标,因为死亡后会有一道光集中在死亡点,只要循着光就可以找到目的地了。 2、在服务器中的指令 首先打开指令台,在指令行输入“/back”就可以回到自己的死亡地点了。在单人游戏中的指令 在单人游戏中,您无法直接返回到…

    2024年5月23日
    4800
  • myeclipse能部署java工程么,myeclipse支持jdk18

    myeclipse如何建java文件 1、点击【File】—【New】–【Class】在如下界面,输入Class的名字,如Test,点击【Finish】。Test.java文件创建成功。 2、点击【File】—【New】–【Class】 在如下界面,输入Class的名字,如Test,点击【Finish】。 Te…

    2024年5月23日
    3900

发表回复

登录后才能评论



关注微信