学java 时基础知识没有学好,学到的现在也忘得差不多了,补补。
(下)不大好就不转了。
[color=brown]转自:[url]http://513500795.qzone.qq.com/blog/1215508623?ptlang=2052[/url][/color]
(下)不大好就不转了。
[color=brown]转自:[url]http://513500795.qzone.qq.com/blog/1215508623?ptlang=2052[/url][/color]
1.文件加密器
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class FileLock extends JFrame implements ActionListener
{
private JMenuBar menuBar;
private JMenu file;
private JLabel fileName;
private JButton replace,newfile;
public FileLock()
{
super("杨明文件加密器");
menuBar=new JMenuBar();
fileName=new JLabel("请选择需要加密的文件");
file=new JMenu("File");
replace=new JButton("覆盖原文件");
newfile=new JButton("创建新文件");
init();
showFrame();
}
public void init()
{
JMenuItem item=null;
file.add(item=new JMenuItem("打开..."));item.addActionListener(this);
file.add(item=new JMenuItem("退出"));item.addActionListener(this);
replace.addActionListener(this);
newfile.addActionListener(this);
menuBar.add(file);
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.add(replace);
this.setJMenuBar(menuBar);
this.add(newfile);
this.add(fileName);
}
public void showFrame()
{
this.setSize(300,120);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
String command=e.getActionCommand();
if(command.equals("打开..."))
{
JFileChooser jfc=new JFileChooser();
jfc.showOpenDialog(this);
try
{
if(!(jfc.getName(jfc.getSelectedFile())).equals(""))
{
fileName.setText(jfc.getSelectedFile().getPath());
}
}
catch(Exception ex)
{
}
}
if(command.equals("覆盖原文件"))
{
RandomAccessFile rafs=null;
if(fileName.getText().equals("请选择需要加密的文件"))
{
JOptionPane.showMessageDialog(this,"请选择需要加密的文件");
}
else
{
try
{
rafs=new RandomAccessFile(fileName.getText(),"rw");
int number=-1,input=0;
while((number=rafs.read())!=-1)
{
fileName.setText("正在加密...");
input=number^10;
rafs.seek(rafs.getFilePointer()-1);
rafs.write(input);
}
JOptionPane.showMessageDialog(this,"文件加密成功!");
fileName.setText("请选择需要加密的文件");
}
catch(Exception ea)
{
}
finally
{
if(rafs!=null)
{
try
{
rafs.close();
}
catch(IOException eb)
{}
}
}
}
}
if(command.equals("创建新文件"))
{
OutputStream os=null;
RandomAccessFile rafs=null;
if(fileName.getText().equals("请选择需要加密的文件"))
{
JOptionPane.showMessageDialog(this,"请选择需要加密的文件");
}
else
{
try
{
rafs=new RandomAccessFile(fileName.getText(),"rw");
os=new FileOutputStream(fileName.getText()+".ym");
int number=-1,input=0;
while((number=rafs.read())!=-1)
{
fileName.setText("正在加密...");
input=number^10;
os.write(input);
}
os.flush();
JOptionPane.showMessageDialog(this,"文件加密成功!");
fileName.setText("请选择需要加密的文件");
}
catch(Exception ea)
{
}
finally
{
if(rafs!=null&&os!=null)
{
try
{
rafs.close();
os.close();
}
catch(IOException eb)
{
}
}
}
}
}
}
public static void main(String[] args)
{
new FileLock();
}
}
2.倒计时牌
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class OlympicTime extends JFrame implements Runnable
{
JLabel[] labels=new JLabel[9];
JPanel jp1,jp2;
public OlympicTime()
{
super("奥运会倒计时");
labels[0]=new JLabel("距离2008年奥运会(这儿可以换别的,下面的时间对应改就OK)还有:");
labels[1]=new JLabel("00");
labels[2]=new JLabel("天");
labels[3]=new JLabel("00");
labels[4]=new JLabel("时");
labels[5]=new JLabel("00");
labels[6]=new JLabel("分");
labels[7]=new JLabel("00");
labels[8]=new JLabel("秒");
init();
showMe();
}
public void init()
{
jp1=new JPanel();
jp2=new JPanel();
jp2.add(labels[0]);
for(int i=1;i<labels.length;i++)
{
jp1.add(labels);
if(i%2!=0)
{
labels.setForeground(Color.red);
}
}
this.add(jp2,BorderLayout.NORTH);
this.add(jp1,BorderLayout.CENTER);
}
public void showMe()
{
this.setSize(300,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
public void run()
{
GregorianCalendar olympic=new GregorianCalendar(2008,7,8,20,8,0);
long olympictime=olympic.getTimeInMillis();
while(true){
Calendar now=Calendar.getInstance();
long nowtime=now.getTimeInMillis();
try{
Thread.sleep(1000);
}catch(Exception e){
}
long day=(olympictime-nowtime)/3600/24/1000;
long hour=((olympictime-nowtime)/3600000)%24;
long minute=((olympictime-nowtime)/60000)%60;
long second=((olympictime-nowtime)/1000)%60;
labels[1].setText(day+"");
labels[3].setText(hour+"");
labels[5].setText(minute+"");
labels[7].setText(second+"");
}
}
public static void main(String[] args)
{
Runnable runnable=new OlympicTime();
Thread time=new Thread(runnable);
time.start();
}
}
3.模拟栈
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class StackFrame extends JFrame implements ActionListener
{
private JButton newProducer,newConsumer,stopButton;
private JProgressBar progressBar;
private JTextArea progressText;
private JPanel jpsouth;
private JScrollPane jpcenter;
public final int MAX_SIZE;
int count;
int[] a;
boolean STOP=false;
public StackFrame()
{
MAX_SIZE=100;
}
public StackFrame(int size)
{
super("添加删除进程演示");
newProducer=new JButton("New Producer");
newConsumer=new JButton("New Consumer");
stopButton=new JButton("Stop");
progressBar=new JProgressBar(1,size);
progressText=new JTextArea();
jpcenter=new JScrollPane(progressText);
jpsouth=new JPanel();
count=0;
MAX_SIZE=size;
a=new int[size];
init();
showFrame();
}
public void init()
{
progressBar.setStringPainted(true);
progressText.append("This is the progress:\n");
jpsouth.setLayout(new FlowLayout(FlowLayout.LEFT));
jpsouth.add(newProducer);
jpsouth.add(newConsumer);
jpsouth.add(progressBar);
jpsouth.add(stopButton);
newProducer.addActionListener(this);
newConsumer.addActionListener(this);
stopButton.addActionListener(this);
this.add(jpcenter,BorderLayout.CENTER);
this.add(jpsouth,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
String command=e.getActionCommand();
if(command.equals("New Producer"))
{
STOP=false;
Runnable push1=this.new Producer();
Thread stackPush1=new Thread(push1);
stackPush1.start();
}
if(command.equals("New Consumer"))
{
STOP=false;
Runnable pop=this.new Consumer();
Thread stackPop=new Thread(pop);
stackPop.start();
}
if(command.equals("Stop")||command.equals("Start"))
{
STOP=true;
}
}
public void showFrame()
{
this.setSize(600,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
}
public synchronized void push()
{
this.progressText.append("Push a data COUNT+"+count+"!\n");
while(count==MAX_SIZE)
{
this.progressText.append("The stack is full!!!("+count+" elements)");
try
{
this.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
a[count++]=count;
this.progressBar.setValue(count);
this.notifyAll();
}
public synchronized int pop()
{
this.progressText.append("Pop a data COUNT+"+count+"!\n");
while(count==0)
{
this.progressText.append("The stack is empty!!!("+count+" elements)");
this.progressBar.setValue(count);
try{
this.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.progressBar.setValue(count);
this.notifyAll();
return a[--count];
}
class Producer implements Runnable
{
public void run()
{
while(true)
{
if(STOP)
{
break;
}
try{
Thread.sleep(300);
}catch(Exception e){
e.printStackTrace();
}
StackFrame.this.push();
}
}
}
class Consumer implements Runnable
{
public void run()
{
while(true)
{
if(STOP)
{
break;
}
try{
Thread.sleep(400);
}catch(Exception e){
e.printStackTrace();
}
StackFrame.this.pop();
}
}
}
public static void main(String[] args)
{
StackFrame stackFrame=new StackFrame(100);
}
}