本文目录
记事本JAVA的源代码
import java.awt.BorderLayout;import java.awt.FileDialog;import java.awt.Font;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;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 javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.border.TitledBorder;/*因为根据个人的电脑路径可能有所偏差,没有源路径的情况下,设置默认保存路径为D盘根目录下 * 若要选择保存其他地方,可以选择 另存为*/public class TestDemo extends JFrame {private static final long serialVersionUID = -5355432125621015300L;private String url = null;//文件路径private String str=null;//复制或剪切 的字符串private StringSelection stringSelection=null;private Clipboard clipboard=new Clipboard(str);private Transferable transferable=null;private DataFlavor flavor=null;public TestDemo() {init();}private void init() {setTitle("我的记事本");setSize(500, 600);setContentPane(createContentPane());//添加主面板} /*创建主面板*/private JPanel createContentPane() {JPanel pane = new JPanel(new BorderLayout());pane.add(BorderLayout.NORTH, createChocePane());//添加菜单栏pane.add(createAreaPane());//添加文本区域return pane;}/*创建菜单栏,以及实现功能*/private JPanel createChocePane() {JPanel pane = new JPanel();JMenuBar menuBar1 = new JMenuBar();JMenu menu = new JMenu("文件");menuBar1.add(menu);JMenuItem menuIt1 = new JMenuItem("新建");JMenuItem menuIt2 = new JMenuItem("打开");JMenuItem menuIt3 = new JMenuItem("保存");JMenuItem menuIt4 = new JMenuItem("另存为");menu.add(menuIt1);menu.add(menuIt2);menu.add(menuIt3);menu.add(menuIt4);JMenuBar menuBar2 = new JMenuBar();JMenu menu2 = new JMenu("");menuBar2.add(menu2);JMenuItem menuIt5 = new JMenuItem("复制");JMenuItem menuIt6 = new JMenuItem("剪切");JMenuItem menuIt7 = new JMenuItem("粘帖");menu2.add(menuIt5);menu2.add(menuIt6);menu2.add(menuIt7);JMenuBar menuBar3 = new JMenuBar();JMenu menu3 = new JMenu("帮助");menuBar3.add(menu3);JMenuItem menuIt8 = new JMenuItem("关于记事本");menu3.add(menuIt8);pane.add(menuBar1);pane.add(menuBar2);pane.add(menuBar3);menuIt1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {testArea.setText(null);}});menuIt2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {final FileDialog fd = new FileDialog(new JFrame(), "查找文件",FileDialog.LOAD);fd.setVisible(true);if (fd.getDirectory() != null && fd.getFile() != null) {testArea.setText(null);url = fd.getDirectory() + fd.getFile();try {BufferedReader in = new BufferedReader(new FileReader(url));for (int i = 0;; i++) {testArea.append(in.readLine());if (in.read() == -1) {break;} elsecontinue;}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}});menuIt3.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (url==null) {url="D:\\新建 文本文档.txt";}File f = new File(url);BufferedWriter out = null;try {out = new BufferedWriter(new FileWriter(url));f.createNewFile();out.append(testArea.getText());out.flush();} catch (IOException e1) {e1.printStackTrace();} finally {try {out.close();} catch (IOException e1) {e1.printStackTrace();}} }});menuIt4.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {FileDialog fd = new FileDialog(new JFrame(), "保存文本",FileDialog.SAVE);fd.setVisible(true);if (url!=null) {File f = new File(url);BufferedWriter out = null;try {f.createNewFile();out = new BufferedWriter(new FileWriter(url));out.append(testArea.getText());out.flush();} catch (IOException e) {e.printStackTrace();} finally {try {out.close();} catch (IOException e) {e.printStackTrace();}}}}}); menuIt5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {str=testArea.getSelectedText();stringSelection=new StringSelection(str); clipboard.setContents(stringSelection, null);}}); menuIt6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {str=testArea.getSelectedText();stringSelection=new StringSelection(str); clipboard.setContents(stringSelection, null); int start=testArea.getSelectionStart(); int end=testArea.getSelectionEnd(); testArea.replaceRange( null,start,end); }}); menuIt7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {transferable=clipboard.getContents(this);flavor=DataFlavor.stringFlavor;if (transferable.isDataFlavorSupported(flavor)) {int start=testArea.getSelectionStart(); int end=testArea.getSelectionEnd(); testArea.replaceRange( null,start,end); int pos=testArea.getCaretPosition(); try {str=(String)transferable.getTransferData(flavor);testArea.insert(str, pos);} catch (UnsupportedFlavorException e1) {e1.printStackTrace();} catch (IOException e1) {e1.printStackTrace();} }}}); menuIt8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {JOptionPane.showMessageDialog(null,"功能简单,绝对原创");}});return pane;}JTextArea testArea;private JScrollPane createAreaPane() {JScrollPane pane = new JScrollPane();pane.setBorder(new TitledBorder("区域"));testArea = new JTextArea();testArea.setFont(new Font("宋体", Font.BOLD, 13));testArea.setLineWrap(true);pane.getViewport().add(testArea);return pane;}public static void main(String args) {TestDemo td = new TestDemo();td.setVisible(true);}}
java求源代码
你是青鸟的吧 这我写过 有源码 这里怎么上传压缩包啊
package ghhh;import java.util.Scanner;public class DvD {public static void main(String args) {int state;String name;int date;int count;name="权利的游戏";name="命运之夜";name="傲慢与偏见";state=1;state=0;state=1;date=13;date=0;date=9;count=23;count=23;count=23;int n;//boolean n=false;do{System.out.println("欢迎使用迷你DVD管理器");System.out.println("1.新增DVD");System.out.println("2.查看DVD");System.out.println("3.删除DVD");System.out.println("4.借出DVD");System.out.println("5.归还DVD");System.out.println("6.退出DVD");Scanner input =new Scanner(System.in);System.out.println("请选择:"); n=input.nextInt();switch(n){case 1:System.out.println("请输入要增加DVD的名称:");String name1=input.next();boolean flag=false;for(int i=0;i《name.length;i++){if(name==null){name=name1;flag=true;break;}}if(flag){System.out.println("新增DVD"+name1+"成功");}else{System.out.println("货架已满!增加失败!");}System.out.println("请输入0返回!");n=input.nextInt();break;case 2:System.out.println("序号\t"+"状态\t"+"名称\t\t"+"借出日期\t"+"借出次数");for(int i=0;i《name.length;i++){if(name!=null){String state1 =((state==0)?"可借":"已借");String date1=((date+"日");String count1=count+"次";System.out.println((i+1)+"\t"+state1+"\t"+name+"\t"+date1+"\t\t"+count1);}}System.out.println("请输入0返回!");n=input.nextInt();break;case 3:System.out.println("请输入要删除的DVD名称:");String name2=input.next();int index=-1;boolean a=false;boolean flag1=false;for(int i=0;i《name.length;i++){if(name2.equals(name==1){System.out.println("此DVD已经借出,无法删除");a=true;break;}else if(name2.equals(name==0){a=true;index=i;flag1=true;System.out.println("删除成功!");break;}}if(a==false){System.out.println("没有找到相同名称的DVD!");}if(flag1){for (int i=index;i《name.length;i++){if(i!=name.length-1){name;state;date;count;}name=null;state=0;date=0;count=0;}} System.out.println("请输入0返回!"); n=input.nextInt();break;case 4:System.out.println("请输入要借出的DVD:"); String name3=input.next(); boolean a3=false; boolean b3=false; for(int i=0;i《name.length;i++){ if(name3.equals(name==1){ System.out.println("该DVD已经借出"); a3=true; }else if(name3.equals(name==0){ do{ System.out.println("请输入借出的日期:"); int m=input.nextInt(); if(m》31||m《1){ System.out.println("请重新输入日期:"); b3=true; }else{ date=m; state=1; count+=1; } }while(b3==true); System.out.println("借出成功!"); a3=true; } } if(a3==false){ System.out.println("没有该DVD"); } System.out.println("请输入0返回!"); n=input.nextInt();break;case 5:System.out.println("请输入要归还的DVD:");String name5=input.next();boolean b5=false;boolean m5=false;for(int i=0;i《name.length;i++){if(name5.equals(name==1){b5=true;do{System.out.println("请输入要归还DVD的日期:(归还日期请输入当月日期 1~31)");int a5=input.nextInt(); if(a5》31){ System.out.println("请重新输入日期:"); m5=true; }else if(a5《date){ System.out.println("借出日期是"+date+"日\t输入的日期不能小于借出的日期,请重新输入日期:"); m5=true; }else{ state=0; System.out.println("归还成功"); System.out.println("借出日期是:"+date)+"元"); date=0; m5=false; } }while(m5==true);}else if (name5.equals(name==0){System.out.println("该DVD未借出,不可归还!");b5=true;}}if(b5==false){System.out.println("没有该名称的DVDV");}System.out.println("请输入0返回!"); n=input.nextInt();break;case 6:n=1;System.out.println("程序退出!");break;default:if(n==0){}else{System.out.println("输入错误!请重新输入!");n=0;}break;}}while(n==0);System.out.println("谢谢使用!");}}
看看有没有问题 好久之前的了
求java程序源代码,要可以运行的!
public class Test{ public static void main(String args){ }}最精简的,holle world 都不输出 ^_^而且就对能运行 ^_^
用JAVA写出源代码运行结果如下
您好:
代码及运行结果如下,供参考:
代码示意图
运行结果示意图
因输出的内容中包含特殊字符,所以在输出的时候,需要做转义,以上代码供参考。