网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java书架代码 java书单

java记事本源代码

给你个做好了的Java的源程序的记事本,自己看看就行了的,不怎么难的···

创新互联专注于怀来企业网站建设,响应式网站开发,商城建设。怀来网站建设公司,为怀来等地区提供建站服务。全流程按需规划网站,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.*;

public class MyNotepad implements ActionListener{

private JFrame frame=new JFrame("新记事本");

private JTextArea jta=new JTextArea();

private String result="";

private boolean flag=true;

private File f;

private JButton jb=new JButton("开始");

private JTextField jtf=new JTextField(15);

private JTextField jt=new JTextField(15);

private JButton jbt=new JButton("替换为");

private JButton jba=new JButton("全部替换");

private Icon ic=new ImageIcon("D:\\java课堂笔记\\GUI\\11.gif");

private String value;

private int start=0;

private JFrame jf=new JFrame("查找");

private JFrame jfc=new JFrame("替换");

@Override

public void actionPerformed(ActionEvent e) {

String comm=e.getActionCommand();

if("新建".equals(comm)){

if(!(frame.getTitle().equals("新记事本"))){

if(!flag){

write();

newNew();

}else{

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

int returnVal = jfc.showDialog(null,"保存为");

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

flag=false;

write();

}

}

}else if(!(jta.getText().isEmpty())){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

int returnVal = jfc.showDialog(null,"保存为");

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

flag=false;

write();

newNew();

}

}else{

newNew();

}

}else if("打开".equals(comm)){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

jfc.setDialogType(JFileChooser.OPEN_DIALOG);

int returnVal = jfc.showOpenDialog(null);

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

frame.setTitle(f.getName());

result=read();

flag=false;

value=result;

jta.setText(result);

}

}else if("保存".equals(comm)){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

if(flag){

int returnVal = jfc.showDialog(null,"保存为");

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

flag=false;

write();

}

}else{

write();

}

}else if("另存".equals(comm)){

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

int returnVal = jfc.showDialog(null,"另存");

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

write();

}

}else if("退出".equals(comm)){

System.exit(0);

}else if("撤销".equals(comm)){

jta.setText(value);

}else if("剪切".equals(comm)){

value=jta.getText();

jta.cut();

}else if("复制".equals(comm)){

jta.copy();

}else if("粘贴".equals(comm)){

value=jta.getText();

jta.paste();

}else if("删除".equals(comm)){

value=jta.getText();

jta.replaceSelection(null);

}else if("全选".equals(comm)){

jta.selectAll();

}else if("查找".equals(comm)){

value=jta.getText();

jf.add(jtf,BorderLayout.CENTER);

jf.add(jb,BorderLayout.SOUTH);

jf.setLocation(300,300);

jf.pack();

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}else if("替换".equals(comm)){

value=jta.getText();

GridLayout gl=new GridLayout(3,3);

JLabel jl1=new JLabel("查找内容:");

JLabel jl2=new JLabel("替换为:");

jfc.setLayout(gl);

jfc.add(jl1);

jfc.add(jtf);

jfc.add(jb);

jfc.add(jl2);

jfc.add(jt);

jfc.add(jbt);

JLabel jl3=new JLabel();

JLabel jl4=new JLabel();

jfc.add(jl3);

jfc.add(jl4);

jfc.add(jba);

jfc.setLocation(300,300);

jfc.pack();

jfc.setVisible(true);

jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}else if("版本".equals(comm)){

JDialog jd=new JDialog(frame,"关于对话框");

jd.setSize(200,200);

JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");

jd.add(l,BorderLayout.CENTER);

jd.setLocation(100,200);

jd.setSize(300,300);

jd.setVisible(true);

// jd.pack();

jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

}else if("开始".equals(comm)||"下一个".equals(comm)){

String temp=jtf.getText();

int s=value.indexOf(temp,start);

if(value.indexOf(temp,start)!=-1){

jta.setSelectionStart(s);

jta.setSelectionEnd(s+temp.length());

jta.setSelectedTextColor(Color.GREEN);

start=s+1;

jb.setText("下一个");

// value=value.substring(s+temp.length());//不能截取字串

}else {

JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);

jf.dispose();

}

}else if("替换为".equals(comm)){

String temp=jtf.getText();

int s=value.indexOf(temp,start);

if(value.indexOf(temp,start)!=-1){

jta.setSelectionStart(s);

jta.setSelectionEnd(s+temp.length());

jta.setSelectedTextColor(Color.GREEN);

start=s+1;

jta.replaceSelection(jt.getText());

}else {

JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);

jf.dispose();

}

}else if("全部替换".equals(comm)){

String temp=jta.getText();

temp=temp.replaceAll(jtf.getText(), jt.getText());

jta.setText(temp);

}

}

public String read(){

String temp="";

try {

FileInputStream fis = new FileInputStream(f.getAbsolutePath());

byte[] b=new byte[1024];

while(true){

int num=fis.read(b);

if(num==-1)break;

temp=temp+new String(b,0,num);

}

fis.close();

} catch (Exception e1) {

e1.printStackTrace();

}

return temp;

}

public void write(){

try {

FileOutputStream fos=new FileOutputStream(f);

fos.write(jta.getText().getBytes());

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public void newNew(){

frame.dispose();

new MyNotepad();

flag=true;

}

public MyNotepad(){

JMenuBar jmb=new JMenuBar();

String[] menuLab={"文件","编辑","帮助"};

String[][] menuItemLab={{"新建","打开","保存","另存","退出"},

{"撤销","剪切","复制","粘贴","删除","全选","查找","替换"},

{"版本"}};

for(int i=0;imenuLab.length;i++){

JMenu menu=new JMenu(menuLab[i]);

jmb.add(menu);

for(int j=0;jmenuItemLab[i].length;j++){

JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);

menu.add(jmi);

jmi.addActionListener(this);

}

}

frame.setJMenuBar(jmb);

jta.setLineWrap(true);//自动换行

JScrollPane jsp=new JScrollPane(jta);//滚动窗口面板

frame.add(jsp);

jb.addActionListener(this);

jbt.addActionListener(this);

jba.addActionListener(this);

frame.setLocation(200,50);

frame.setSize(620,660);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new MyNotepad();

}

}

java多层list实现图书的增删改查?

import java.util.ArrayList;

import java.util.List;

/**

* 测试类

*

* @author 时间在流

*

*/

public class Test {

/**

* 这个属性用来模仿书房

*/

private ListListString room = new ArrayListListString();

/**

* 向书房里添加一个书架

*

* @return 新添加书架的索引

*/

public int addBookShelf() {

room.add(new ArrayListString());

return room.size() - 1;

}

/**

* 向书架里添加一本书

*

* @param bookShelfIndex 书架索引

* @param book 书。这里就用一个字符串代表一本书了,你要愿意也可以单独创建一个书类

* @return 新添加书籍的索引

*/

public int addBook(int bookShelfIndex, String book) {

ListString shelf = room.get(bookShelfIndex);

shelf.add(book);

return shelf.size() - 1;

}

// 更新书架我就不写了。

/**

* 更新书架上的某本书

*

* @param bookShelfIndex 书架索引

* @param bookIndex 旧书索引

* @param newBook 新书

*/

public void setBook(int bookShelfIndex, int bookIndex, String newBook) {

ListString shelf = room.get(bookShelfIndex);

shelf.set(bookIndex, newBook);

}

/**

* 移除一个书架

*

* @param bookShelfIndex 书架索引

* @return 剩余书架的个数

*/

public int removeBookShelf(int bookShelfIndex) {

room.remove(bookShelfIndex);

return room.size();

}

/**

* 移除书架上的某本书

*

* @param bookShelfIndex 书架索引

* @param bookIndex 书籍索引

* @return 该书架剩余书的数量

*/

public int removeBook(int bookShelfIndex, int bookIndex) {

ListString shelf = room.get(bookShelfIndex);

shelf.remove(bookIndex);

return shelf.size();

}

/**

* 获得一个书架

*

* @param bookShelfIndex 书架索引

* @return 书架

*/

public ListString getBookShelf(int bookShelfIndex) {

return room.get(bookShelfIndex);

}

/**

* 获得书架上的某本书。在现实生活中拿走了一本书,书架上应该会少一本

*

* @param bookShelfIndex 书架索引

* @param bookIndex 书籍索引

* @return 书籍

*/

public String getBook(int bookShelfIndex, int bookIndex) {

ListString shelf = room.get(bookShelfIndex);

return shelf.get(bookIndex);

}

public static void main(String[] args) {

Test test = new Test();

int shelf1Index = test.addBookShelf();

int shelf2Index = test.addBookShelf();

int book1Index = test.addBook(shelf1Index, "鬼吹灯");

int book2Index = test.addBook(shelf1Index, "盗墓笔记");

int book3Index = test.addBook(shelf2Index, "斗破苍穹");

int book4Index = test.addBook(shelf2Index, "斗罗大陆");

test.setBook(shelf2Index, book4Index, "吞噬星空");

System.out.println("2号书架的第2本书是" + test.getBook(shelf2Index, book4Index));

System.out.println("移除了2号书架的第2本书,2号书架还剩" + test.removeBook(shelf2Index, book4Index) + "本书");

System.out.println("移除了第2个书架,现在还剩" + test.removeBookShelf(shelf2Index) + "个书架");

ListString shelf = test.getBookShelf(shelf1Index);

System.out.println("1号书架书籍列表:");

for (String book : shelf) {

System.out.println(book);

}

}

}

在Java工程下,用java代码创建文件夹

参考下面代码,说明已在代码中注释:

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class WriteFile {

public static void main(String[] args) {

writeFile();

}

public static void writeFile(){

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String content = sdf.format(new Date());

System.out.println("现在时间:" + content);

FileOutputStream out = null;

File file;

try {

String rootFile = "D:\\tests\\license";

file = new File(rootFile);

if (!file.exists()) {

/*

file.mkdirs():创建没有存在的所有文件夹

file.mkdir():创建没有存在的最后一层文件夹

例如:在硬盘上有D://test 文件夹,但是现在需要创建D://test//license//save,这个时候就需要使用file.mkdirs()而不能使用file.mkdir(),另外这两个方法都是仅仅能创建文件夹,不能创建文件,即使创建D://test//license//save//systemTime.dat如果使用该方法创建的SystemTime.dat也是一个文件夹 ,而不是文件

*/

file.mkdirs();

}

File fileDat = new File(rootFile + "\\systemFile.dat");

/*

if(!fileDat.exists()){

//创建文件 不是文件夹,在程序中这这一步没有必要,因为

new FileOutputStream(fileDat);该语句有创建文件的功能

fileDat.createNewFile();//

}

*/

out = new FileOutputStream(fileDat);

byte[] contentInBytes = content.getBytes();

out.write(contentInBytes);

out.flush();

out.close();

System.out.println("Done");

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (out != null) {

out.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}


本文题目:java书架代码 java书单
当前地址:http://bjjierui.cn/article/dogoopo.html

其他资讯