编写一个java的应用小程序。

编写一个java的应用小程序。,第1张

public class RandomNum

{

public static void main(String[] args)

{

int num=(int)(Mathrandom()800);//取800以内随机数

Systemoutprintln("所取随机数为:"+num);

Systemoutprintln("它的百位数字为:"+(num/100));//取百位

Systemoutprintln("它的十位数字为:"+((num-(num/100)100))/10);//取十位

Systemoutprintln("它的十位数字为:"+(num%10));//取个位

}

}

package testWrite;

import javaioFile;

import javaioFileInputStream;

import javaioFileOutputStream;

import javaioInputStream;

public class write {

InputStream inStream ;

FileOutputStream fs ;

int bytesum = 0;

int byteread = 0;

String  data ;

StringBuffer dd;

public static   byte[] readdata(String filePath) {

   byte[] data = new byte[2048];// 用于存储读取的文件内容

   try {

   File file = new File(filePath);

   if (fileexists()) {

   FileInputStream fis = new FileInputStream(file);

   fisread(data);

   fisclose();

   }else{       

   Systemoutprintln("文件不存在");

   }

} catch ( Exception e) {  

}

return data;

}

public void  writeFile(String fileCopyPath,byte [] data){

   File file = new File(fileCopyPath);

try {

   if (!fileexists()) {

filecreateNewFile(); //创建文件

FileOutputStream fos = new FileOutputStream(file);

foswrite(data);

fosclose();

}else{          

   Systemoutprintln("文件已经存在");

   }

} catch (Exception e) {

eprintStackTrace();

}

   }

}

package testWrite;

public class writeOne  extends write {

public static void main(String[] args) {

//第一种

// writeOne  a=new writeOne ();

// byte [] data=areaddata("D:\\Users\\workspace\\testWrite\\src\\testWrite\\writejava");

// awriteFile("D:\\Users\\workspace\\testWrite\\src\\testWrite\\writenewjava",data);

//第二种

writeOne  a2=new writeOne ();

Systemoutprintln(SystemgetProperty("userdir"));//userdir指定了当前的路径

String path=SystemgetProperty("userdir");

Systemoutprintln(path);

    String writefile="\\src\\testWrite\\writejava";

    String writenewfile="\\src\\testWrite\\writenewjava";

    String w1=path+writefile;   //读取文件的路径

   String w2=path+writenewfile;  //写入文件路径

   byte [] data=a2readdata(w1);  //读取文件

a2writeFile(w2 ,data); //写入文件

}

}

本地已经调试通过可以复制writejava 文件 ,注意包,和文件的路径 直接运行第二个java文件就可以了,运行后刷新一下就可以看到复制的文件(备注:我是使用myeclipse环境进行调试)

import javaawt;

import javaawtevent;

import javaxswing;

import javatextDateFormat;

import javaxswingevent;

import javaxswingtable;

import javaxswingtree;

import javaxswingfilechooser;

import javaio;

import javautil;

public class WinExplorer extends JFrame implements TreeSelectionListener{

JPanel contentPane=(JPanel)getContentPane();;

JMenuBar menuBar = new JMenuBar();

JMenu menuFile = new JMenu("文件");

JMenuItem menuItemExit = new JMenuItem("退出");

JToolBar toolBar = new JToolBar();

JButton bttUp = new JButton();

Icon iconUp=UIManagergetIcon("FileChooserupFolderIcon");

JLabel statusBar = new JLabel();

DetailTable detailTable = new DetailTable();

JScrollPane sp = new JScrollPane(detailTable);

JSplitPane split = new JSplitPane();

FileSystemView fileSystemView=FileSystemViewgetFileSystemView();

FileNode root=new FileNode(fileSystemViewgetRoots()[0]);

DefaultTreeModel treeModel=new DefaultTreeModel(root);

JTree tree = new JTree(treeModel);

public static void main(String argv[]){

try {

UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName());

Font font=new Font("宋体",FontPLAIN,12);

String names[]={"Label","CheckBox","PopupMenu","TextPane",

"MenuItem","CheckBoxMenuItem","JRadioButtonMenuItem",

"ComboBox","Button","Tree","ScrollPane","TabbedPane",

"EditorPane","TitledBorder","Menu","TextArea","OptionPane",

"MenuBar","ToolBar","ToggleButton","ToolTip","ProgressBar",

"TableHeader","Panel","List","ColorChooser","PasswordField",

"TextField","Table","Label","Viewport","RadioButtonMenuItem",

"RadioButton"};

for(int i=0;i<nameslength;i++)UIManagerput(names[i]+"font",font);

UIManagerput("Labelforeground",Colorblack);

UIManagerput("Borderforeground",Colorblack);

UIManagerput("TitledBordertitleColor",Colorblack);

new WinExplorer()show();

}catch(Exception e){

eprintStackTrace();

}

}

public WinExplorer() {

contentPanesetLayout(new BorderLayout());

Dimension dimension = getToolkit()getScreenSize();

int i = (dimensionwidth - 640) / 2;

int j = (dimensionheight - 480) / 2;

setBounds(i,j,640,480);

setTitle("资源管理器");

setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

setJMenuBar(menuBar);

statusBarsetText(" ");

menuItemExitaddActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {Systemexit(0);}

});

bttUpsetIcon(iconUp);

bttUpaddActionListener(new javaawteventActionListener() {

public void actionPerformed(ActionEvent e) {

try{

TreePath upPath=treegetSelectionPath()getParentPath();

if(upPath!=null){

treesetSelectionPath(upPath);

treescrollPathToVisible(upPath);

}

}catch(Exception ex){}

}

});

splitsetDividerSize(6);

splitsetLeftComponent(new JScrollPane(tree));

splitsetRightComponent(sp);

splitsetDividerLocation(180);

spgetViewport()setBackground(Colorwhite);

menuFileadd(menuItemExit);

menuBaradd(menuFile);

contentPaneadd(toolBar, BorderLayoutNORTH);

toolBaradd(bttUp, null);

contentPaneadd(statusBar, BorderLayoutSOUTH);

contentPaneadd(split, BorderLayoutCENTER);

treeaddTreeExpansionListener(new MyExpandsionListener());

treesetCellRenderer(new MyTreeCellRenderer());

treeaddTreeSelectionListener(this);

treesetSelectionRow(0);

treesetComponentOrientation(ComponentOrientationUNKNOWN);

}

public void valueChanged(TreeSelectionEvent e){

Object obj=treegetLastSelectedPathComponent();

if(obj==null)return;

else detailTablesetParent(((FileNode)obj)getFile());

}

class MyTreeCellRenderer extends DefaultTreeCellRenderer {

public MyTreeCellRenderer() {}

public Component getTreeCellRendererComponent(JTree tree,Object value,

boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus) {

supergetTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);

setIcon(fileSystemViewgetSystemIcon(((FileNode)value)getFile()));

return this;

}

}

class MyExpandsionListener implements TreeExpansionListener {

public MyExpandsionListener() {}

public void treeExpanded(TreeExpansionEvent event) {

if(eventgetPath() == null){

return;

}

treesetCursor(new Cursor(CursorWAIT_CURSOR));

TreePath path = eventgetPath();

FileNode node = (FileNode)pathgetLastPathComponent();

nodeexplore();

treeModelnodeStructureChanged(node);

treesetCursor(new Cursor(CursorDEFAULT_CURSOR));

}

public void treeCollapsed(TreeExpansionEvent event) {}

}

class FileNode extends DefaultMutableTreeNode {

private boolean explored = false;

public FileNode(File file) { setUserObject(file); }

public boolean getAllowsChildren() { return isDirectory(); }

public boolean isLeaf() { return !isDirectory();}

public File getFile() { return (File)getUserObject(); }

public boolean isExplored() { return explored; }

public void setExplored(boolean b){ explored=b;}

public boolean isDirectory() { return getFile()isDirectory();}

public String toString() {

File file = (File)getUserObject();

String filename = filetoString();

int index = filenamelastIndexOf(Fileseparator);

return (index != -1 && index != filenamelength()-1)

filenamesubstring(index+1) : filename;

}

public void explore() {

if(!isExplored()) {

File file = getFile();

File[] children = filelistFiles();

if(children==null||childrenlength==0)return;

for(int i=0; i < childrenlength; ++i)

{

File f=children[i];

if(fisDirectory())add(new FileNode(children[i]));

}

explored = true;

}

}

}

class DetailTable extends JTable{

DetailTableModel model=new DetailTableModel();

public DetailTable(){

setModel(model);

setShowGrid(false);

TableColumnModel colModel = getColumnModel();

for (int i=0;i<3;i++)

colModelgetColumn(i)setCellRenderer(new DetailsTableCellRenderer());

setRowHeight(18);

thisaddMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e) {

if(egetClickCount()==2){

for (int i = 0; i <getRowCount(); i++) {

if(getCellRect(i,0,true)contains(egetPoint())){

openSelect();

break;

}

}

}

}

});

}

public void openSelect(){

Object obj=modelgetValueAt(getSelectedRow(),0);

if(obj==null)return;

File f=(File)obj;

if(fisDirectory()){

//expand tree

}else{

//open select file

}

}

public void setParent(File parent){

modelremoveAllRows();

File list[]=parentlistFiles();

if(list==null)return;

Vector vDir=new Vector(),vFile=new Vector();

for (int i = 0; i < listlength; i++) {

if(list[i]isDirectory())vDiradd(list[i]);

else vFileadd(list[i]);

}

sortElements(vFile);

sortElements(vDir);

for (int i = 0; i < vDirsize(); i++)modeladdFile((File)vDirelementAt(i));

for (int i = 0; i < vFilesize(); i++)modeladdFile((File)vFileelementAt(i));

}

public void sortElements(Vector v)

{

for(int i=0;i<vsize();i++)

{

int k=i;

for(int j=i+1;j<vsize();j++){

File fa=(File)velementAt(j);

File fb=(File)velementAt(k);

if(fileSystemViewgetSystemDisplayName(fa)toLowerCase()compareTo(

fileSystemViewgetSystemDisplayName(fb)toLowerCase())<0)k=j;

}

if(k!=i)swap(k,i,v);

}

}

private void swap(int loc1,int loc2,Vector v){

Object tmp=velementAt(loc1);

vsetElementAt(velementAt(loc2),loc1);

vsetElementAt(tmp,loc2);

}

class DetailTableModel extends DefaultTableModel {

public DetailTableModel() {

addColumn("名称");

addColumn("大小");

addColumn("修改时间");

}

public void addFile(File f){

addRow(new Object[]{f,new Double(flength()/1024),

new javasqlDate(flastModified())});

}

public void removeAllRows(){

while(getRowCount()!=0) removeRow(0);

}

public boolean isCellEditable(int row, int column) {return false;}

}

class DetailsTableCellRenderer extends DefaultTableCellRenderer {

DetailsTableCellRenderer() {}

public Component getTableCellRendererComponent(JTable table, Object value,

boolean isSelected, boolean hasFocus, int row, int column) {

if (column == 1){

setHorizontalAlignment(SwingConstantsTRAILING);

isSelected=hasFocus=false;

}else if(column==2){

setHorizontalAlignment(SwingConstantsCENTER);

isSelected=hasFocus=false;

}else setHorizontalAlignment(SwingConstantsLEADING);

return supergetTableCellRendererComponent(

table, value, isSelected, hasFocus, row, column);

}

public void setValue(Object value) {

setIcon(null);

if (value instanceof File) {

File file = (File)value;

setText(fileSystemViewgetSystemDisplayName(file));

setIcon(fileSystemViewgetSystemIcon(file));

}else{

supersetValue(value);

}

}

}

}

}

你双击左面的桌面图标就可以浏览了,别双击字。

以上就是关于编写一个java的应用小程序。全部的内容,包括:编写一个java的应用小程序。、用java语言,编写一个小程序。、急需一段java小程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9287865.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存