??xml version="1.0" encoding="utf-8" standalone="yes"?>
* 文g的各U操?br /> * 杨彩 http://blog.sina.com.cn/m/yangcai
* 文g操作 1.0
*/
//package common;
import java.io.*;
public class FileOperate
{
static boolean exitnow=false;
static String aa,bb;
public FileOperate() {
}
/**
* 新徏目录
*/
public void newFolder(String folderPath) {
try
{
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if(!myFilePath.exists())
{
myFilePath.mkdir();
}
System.out.println("新徏目录操作 成功执行");
}
catch(Exception e)
{
System.out.println("新徏目录操作出错");
e.printStackTrace();
}
}
/**
* 新徏文g
*/
public void newFile(String filePathAndName, String fileContent)
{
try
{
String filePath = filePathAndName;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (!myFilePath.exists())
{
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = fileContent;
myFile.println(strContent);
resultFile.close();
System.out.println("新徏文g操作 成功执行");
}
catch (Exception e) {
System.out.println("新徏目录操作出错");
e.printStackTrace();
}
}
/**
* 删除文g
*/
public void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myDelFile = new File(filePath);
myDelFile.delete();
System.out.println("删除文g操作 成功执行");
}
catch (Exception e) {
System.out.println("删除文g操作出错");
e.printStackTrace();
}
}
/**
* 删除文g?
*/
public void delFolder(String folderPath)
{
try
{
delAllFile(folderPath); //删除完里面所有内?
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
myFilePath.delete(); //删除I文件夹
System.out.println("删除文gҎ?成功执行");
}
catch (Exception e)
{
System.out.println("删除文gҎ作出?);
e.printStackTrace();
}
}
/**
* 删除文g多w面的所有文?
* @param path String 文g夹\??c:/fqf
*/
public void delAllFile(String path)
{
File file = new File(path);
if(!file.exists())
{
return;
}
if(!file.isDirectory())
{
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++)
{
if(path.endsWith(File.separator))
{
temp = new File(path + tempList[i]);
}
else
{
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile())
{
temp.delete();
}
if (temp.isDirectory())
{
delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文?
delFolder(path+"/"+ tempList[i]);//再删除空文g?
}
}
System.out.println("删除文g操作 成功执行");
}
/**
* 复制单个文g
* @param oldPath String 原文件\?如:c:/fqf.txt
* @param newPath String 复制后\?如:f:/fqf.txt
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists())
{ //文g存在?
InputStream inStream = new FileInputStream(oldPath); //d原文?
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节?文g大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
System.out.println("删除文gҎ?成功执行");
}
catch (Exception e) {
System.out.println("复制单个文g操作出错");
e.printStackTrace();
}
}
/**
* 复制整个文g夹内?
* @param oldPath String 原文件\?如:c:/fqf
* @param newPath String 复制后\?如:f:/fqf/ff
*/
public void copyFolder(String oldPath, String newPath) {
try
{
(new File(newPath)).mkdirs(); //如果文g夹不存在 则徏立新文g?
File a=new File(oldPath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++)
{
if(oldPath.endsWith(File.separator))
{
temp=new File(oldPath+file[i]);
}
else{
temp=new File(oldPath+File.separator+file[i]);
}
if(temp.isFile())
{
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ( (len = input.read(b)) != -1)
{
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory())
{//如果是子文g?
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
System.out.println("复制文gҎ?成功执行");
}
catch (Exception e) {
System.out.println("复制整个文g夹内Ҏ作出?);
e.printStackTrace();
}
}
/**
* Ud文g到指定目?
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/**
* Ud文g到指定目?
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
public static void main(String args[])
{
System.out.println("使用此功能请按[1] 功能一Q新建目?);
System.out.println("使用此功能请按[2] 功能二:新徏文g");
System.out.println("使用此功能请按[3] 功能三:删除文g");
System.out.println("使用此功能请按[4] 功能四:删除文g?);
System.out.println("使用此功能请按[5] 功能五:删除文g多w面的所有文?);
System.out.println("使用此功能请按[6] 功能六:复制文g");
System.out.println("使用此功能请按[7] 功能七:复制文g夹的所有内?);
System.out.println("使用此功能请按[8] 功能八:Ud文g到指定目?);
System.out.println("使用此功能请按[9] 功能九:Ud文g夹到指定目录");
System.out.println("使用此功能请按[10] 退出程?);
while(!exitnow)
{
FileOperate fo=new FileOperate();
try
{
BufferedReader Bin=new BufferedReader(new InputStreamReader(System.in));
String a=Bin.readLine();
int b=Integer.parseInt(a);
switch(b)
{
case 1:System.out.println("你选择了功能一 误入目录名");
aa=Bin.readLine();
fo.newFolder(aa);
break;
case 2:System.out.println("你选择了功能二 误入文件名");
aa=Bin.readLine();
System.out.println("误入在"+aa+"中的内容");
bb=Bin.readLine();
fo.newFile(aa,bb);
break;
case 3:System.out.println("你选择了功能三 误入文件名");
aa=Bin.readLine();
fo.delFile(aa);
break;
case 4:System.out.println("你选择了功能四 误入文件名");
aa=Bin.readLine();
fo.delFolder(aa);
break;
case 5:System.out.println("你选择了功能五 误入文件名");
aa=Bin.readLine();
fo.delAllFile(aa);
break;
case 6:System.out.println("你选择了功能六 误入文件名");
aa=Bin.readLine();
System.out.println("误入目标文件名");
bb=Bin.readLine();
fo.copyFile(aa,bb);
break;
case 7:System.out.println("你选择了功能七 误入源文g?);
aa=Bin.readLine();
System.out.println("误入目标文件名");
bb=Bin.readLine();
fo.copyFolder(aa,bb);
break;
case 8:System.out.println("你选择了功能八 误入源文g?);
aa=Bin.readLine();
System.out.println("误入目标文件名");
bb=Bin.readLine();
fo.moveFile(aa,bb);
break;
case 9:System.out.println("你选择了功能九 误入源文g?);
aa=Bin.readLine();
System.out.println("误入目标文件名");
bb=Bin.readLine();
fo.moveFolder(aa,bb);
break;
case 10:exitnow=true;
System.out.println("E序l束Q请退?);
break;
default:System.out.println("输入错误.误?-10之间的数");
}
System.out.println("请重新选择功能");
}
catch(Exception e)
{
System.out.println("输入错误字符或程序出?);
}
}
}
}
http://www.aygfsteel.com/Files/yangcai/FileOperate_java.rar
]]>
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.util.*;
public class show extends JFrame implements Runnable
{
static JFrame jf;
JLabel jl;
public show()
{
jf=new JFrame("旉昄");
jl=new JLabel();
jf.getContentPane().add(jl);
jf.setSize(200,100);
}
public void run()
{
while(true)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
jl.setText(sdf.format(new Date()));
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
jl.setText("出错错误Q请重启E序");
}
}
}
public static void main(String arg[])
{
show t=new show();
Thread thread1=new Thread(t);
thread1.start();
jf.setVisible(true);
}
}
]]>
* MyAccess.java
* 杨彩 http://blog.sina.com.cn/m/yangcai
* 最后修改于2007.1.15
*
*/
import java.util.*;
import java.sql.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class MyAccess extends JFrame implements ActionListener,ListSelectionListener
{
private static Connection conn;
private static Statement comm;
private static ResultSet rs;
private static int total=0,k=10,row=0,i=0;
JLabel jl,jl2;
JTable jt;
JTextField jid,jname,jscore,se;
static JButton go,add,drop,del,jbse,flush;
JFrame jf;
JScrollPane s;
Vector vect=new Vector();
String[] data = {"学号=","学号>","学号<", "姓名=", "分数=","分数>=","分数<"};
JComboBox dataList = new JComboBox(data);
String[] columnNames = {"学号","姓名","分数"};
AbstractTableModel tm = new AbstractTableModel()
{
public int getColumnCount(){ return 3;}
public int getRowCount(){ return k;}
public Object getValueAt(int row, int col){ return ((Vector)vect.get(row)).get(col); }
public String getColumnName(int column){ return columnNames[column]; }
};
public MyAccess()
{
jf=new JFrame("考感学院04UJAVA成W理pȝ");
jf.setSize(490,570);
jf.locate(200,200);
jf.setResizable(false);
jf.getContentPane().setLayout(new FlowLayout());
jl=new JLabel("考感学院04UJAVA成W");
jid=new JTextField(6);
jname=new JTextField(5);
jscore=new JTextField(3);
se=new JTextField(9);
go=new JButton("分数(?>?");
go.addActionListener(this);
add=new JButton("d");
add.addActionListener(this);
drop=new JButton("修改");
drop.addActionListener(this);
del=new JButton("删除");
del.addActionListener(this);
jbse=new JButton("搜烦");
jbse.addActionListener(this);
flush=new JButton("h");
flush.addActionListener(this);
jl2=new JLabel("");
jl2.setForeground(Color.red);
jf.getContentPane().add(jl);
jt=new JTable(tm);
jt.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.jt.getSelectionModel().addListSelectionListener(this);
s = new JScrollPane(jt);
jf.getContentPane().add(s);
jf.getContentPane().add(new JLabel("学号:"));
jf.getContentPane().add(jid);
jf.getContentPane().add(new JLabel("姓名:"));
jf.getContentPane().add(jname);
jf.getContentPane().add(new JLabel("分数:"));
jf.getContentPane().add(jscore);
jf.getContentPane().add(add);
jf.getContentPane().add(drop);
jf.getContentPane().add(del);
jf.getContentPane().add(new JLabel("搜烦学生:"));
jf.getContentPane().add(dataList);
jf.getContentPane().add(se);
jf.getContentPane().add(jbse);
jf.getContentPane().add(flush);
jf.getContentPane().add(go);
jf.getContentPane().add(jl2);
}
public void exce(String exce)
{
try
{ this.jt.getSelectionModel().removeListSelectionListener(this);
rs = execQuery(exce);
vect.removeAllElements();
tm.fireTableDataChanged();
total=0;
while(rs.next())
{
Vector rec_vector=new Vector();
rec_vector.addElement(rs.getString(1));
rec_vector.addElement(rs.getString(2));
rec_vector.addElement(rs.getString(3));
vect.addElement(rec_vector);
total++;
}
k=total;
this.closeDB();
System.out.println("vect是面?+total+"记录执行操作");
System.out.println("成功执行:"+exce);
this.jt.getSelectionModel().addListSelectionListener(this);
}
catch(Exception ee)
{
jl2.setText("无法执行,请填入正的数据");
System.out.println("执行p|,可能查询为空");
k=0;
}
}
public void excesql(String sql)
{
try
{
conBuild();
Statement stm=conn.createStatement();
stm.executeUpdate(sql);
jf.repaint();
jl2.setText("操作已执?);
System.out.println("成功执行:"+sql);
this.closeDB();
}
catch(Exception ee)
{
jl2.setText("无法执行,请填入正的数据");
System.out.println(sql+"无法执行");
}
}
public static void regDriver()//register JDBC 桥接 driver
{
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();//关键?
System.out.println("驱动注册成功");
}
catch(Exception e)
{
System.out.println("无法创徏驱动E序实体!");
}
}
//建立数据库连?br />public static void conBuild()//建立JDBCq接
{
try{
MyAccess.regDriver();
conn=DriverManager.getConnection("jdbc:odbc:myDSN","","");//关键?
conn.setAutoCommit(true);
System.out.println("成功q接数据?);
}
catch(Exception e)
{
System.out.println(e.getMessage()) ;
System.out.println("无法q接数据库Connection!Q运行之前请先设|数据源 MyDSN");
}
}
public static ResultSet execQuery(String stmt1)//执行查询语句
{
try{
conBuild();
comm=conn.createStatement();
rs=comm.executeQuery(stmt1);
return rs;
}
catch(Exception e)
{
System.out.println("无法创徏Statement!");return null;
}
}
public static void closeDB()
{
try{
comm.close();
conn.close();
System.out.println("关闭记录集,断开数据?);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public void actionPerformed(ActionEvent ee)// 实现ActionListener中的唯一Ҏ
{
if(ee.getSource()==go)
{
try
{
if(i==0)
{
exce("SELECT * FROM 成W order by 分数");
i=1;
go.setText("分数(?>?");
}
else
{
exce("SELECT * FROM 成W order by 分数 desc");
i=0;
go.setText("分数(?>?");
}
jl2.setText("已按分数排序");
}
catch(Exception go)
{
System.out.println("出现错误");
jl2.setText("提示:出现错误");
}
}
if(ee.getSource()==add)
{
this.excesql("insert into 成W values('"+jid.getText()+"','"+jname.getText()+"','"+jscore.getText()+"')");
exce("SELECT * FROM 成W order by 学号");
jid.setText("");
jname.setText("");
jscore.setText("");
jf.repaint();
System.out.println("执行d操作");
}
if(ee.getSource()==drop)
{
this.excesql("update 成W set 姓名='"+jname.getText()+"' ,分数="+jscore.getText()+" where 学号='"+jid.getText()+"'" );
exce("SELECT * FROM 成W order by 学号");
}
if(ee.getSource()==del)
{
this.excesql("delete from 成W where 学号='"+jid.getText()+"'");
exce("SELECT * FROM 成W order by 学号");
jl2.setText("提示:成功删除ID?+jid.getText()+"的数?);
System.out.println("执行删除操作");
}
if(ee.getSource()==jbse)
{
if(dataList.getSelectedIndex()>=4)
{
String sql="SELECT * FROM 成W WHERE "+dataList.getSelectedItem()+se.getText();
exce(sql);
}
else
{
String sql="SELECT * FROM 成W WHERE "+dataList.getSelectedItem()+"'"+se.getText()+"'";
exce(sql);
}
jl2.setText("搜烦l果:?+k+"条数?);
System.out.println(k+"执行搜烦操作");
jf.repaint();
}
if(ee.getSource()==flush)
{
exce("SELECT * FROM 成W order by 学号");
jid.setText("");
jname.setText("");
jscore.setText("");
se.setText("");
jf.repaint();
jl2.setText("提示:h成功");
}
}
public void valueChanged(ListSelectionEvent el) //每当选择值发生更Ҏ调用?
{
row=0;
row=jt.getSelectedRow();
if(row<0) row=0;
Object row_id=jt.getValueAt(row,0);
Object row_name=jt.getValueAt(row,1);
Object row_score=jt.getValueAt(row,2);
jid.setText(row_id.toString());
jname.setText(row_name.toString());
jscore.setText(row_score.toString());
System.out.println("选择已改?+k+" "+total);
}
public static void main(String s[])
{
MyAccess ma=new MyAccess();
ma.exce("SELECT * FROM 成W order by 学号");
ma.jf.setVisible(true);
}
}
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class yc extends JFrame
{
public static JLabel jl1,jl2;
public static TextArea ta1,ta2;
public static JButton jb1,jb2;
public static Container cp1;
static Socket svr;
public static PrintWriter out;
public static void main(String arg[])
{
JFrame jf=new JFrame("与小新聊?-VIP 客户?);
jf.setSize(500,450);
jf.setResizable(false);
jf.locate(100,100);
// cp1=getContentPane();
jl1=new JLabel("q接?...");
ta1=new TextArea();
ta2=new TextArea();
jb1=new JButton("发?);
jb1.addActionListener(new Listener());
jb2=new JButton("清空");
jb2.addActionListener(new Listener());
jf.getContentPane().setLayout(new FlowLayout());
jf.getContentPane().add(jl1);
jf.getContentPane().add(ta1);
jf.getContentPane().add(ta2);
jf.getContentPane().add(jb1);
jf.getContentPane().add(jb2);
jf.setVisible(true);
try
{
target2 outmsg2=new target2();
Thread outthread2=new Thread(outmsg2);
outthread2.start();
QQ();
}
catch(Exception e)
{
}
}
static void QQ()throws Exception
{
System.out.println("正在q接服务?L?..");
//与指定地址的服务器相连?br /> svr=new Socket("127.0.0.1",3300);//要连接远E主填IP
//svr=new Socket(InetAddress.getLocalHost(),3300)
jl1.setText("?+svr.getInetAddress()+"q接成功!误传送的信息...");
}
}
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==yc.jb2)
{
yc.ta1.setText("");
}
else
{
try
{
yc.out=new PrintWriter(yc.svr.getOutputStream());
yc.out.println(yc.ta2.getText());
yc.out.flush();
yc.ta1.append("[自己]?\n"+yc.ta2.getText()+"\n");
yc.ta2.setText("");
}
catch(Exception ee)
{
}
}
}
}
class target2 implements Runnable
{
public void run()
{
while(true)
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(yc.svr.getInputStream()));
String str=in.readLine();
str="[服务?+yc.svr.getInetAddress()+"]?\n"+str;
yc.ta1.append(str+"\n");
}
catch(Exception ee)
{
;
}
}
}
}
———————————————————————————————?/p>
//QQ服务?/p>
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ycserver extends JFrame
{
public static JLabel jl1;
public static TextArea ta1,ta2;
public static JButton jb1,jb2;
public static Container cp1;
public static ServerSocket svr;
public static Socket clt;
public static Thread thread;
public static int i=0;
public static void main(String arg[])
{
JFrame jf=new JFrame("与杨彩聊?-VIP 服务?);
jf.setSize(500,450);
jf.locate(200,200);
jf.setResizable(false);
ta1=new TextArea();
ta2=new TextArea();
jb1=new JButton("发?);
jb2=new JButton("清空");
jl1=new JLabel("{待q接");
jb1.addActionListener(new jb1Listener());
jb2.addActionListener(new jb1Listener());
jf.getContentPane().setLayout(new FlowLayout());
jf.getContentPane().add(jl1);
jf.getContentPane().add(ta1);
jf.getContentPane().add(ta2);
jf.getContentPane().add(jb1);
jf.getContentPane().add(jb2);
jf.setVisible(true);
try
{
target outmsg=new target();
Thread outthread=new Thread(outmsg);
outthread.start();
wait wait1=new wait();
Thread waitthread=new Thread(wait1);
waitthread.start();
QQ();
}
catch(Exception e)
{
}
}
static void QQ()throws Exception
{/*
//建立服务器套节字
svr=new ServerSocket(3300);
System.out.println("{待q接....");
//{待客户机接?br /> clt=svr.accept();
i++;
ta1.setText(i+"");
//获得客户IP地址
System.out.println("q接h来自:"+clt.getInetAddress());
jl1.setText("q接h来自:"+clt.getInetAddress());
//建立I/O?br /> */
}
}
class jb1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ycserver.jb2)
{
ycserver.ta1.setText("");
}
else
{
try
{
PrintWriter out=new PrintWriter(ycserver.clt.getOutputStream());
out.println(ycserver.ta2.getText());
out.flush();
ycserver.ta1.append("[自己]?\n"+ycserver.ta2.getText()+"\n");
ycserver.ta2.setText("");
}
catch(Exception ee)
{
}
}
}
}
class target implements Runnable
{
public void run()
{
while(true)
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(ycserver.clt.getInputStream()));
String str=in.readLine();
str="[客户端在"+ycserver.clt.getInetAddress()+"]?\n"+str;
ycserver.ta1.append(str+"\n");
}
catch(Exception ee)
{
;
}
}
}
}
class wait implements Runnable
{
public void run()
{
while(true)
{
try
{
//建立服务器套节字
ycserver.svr=new ServerSocket(3300);
System.out.println("{待q接....");
//{待客户机接?br /> ycserver.clt=ycserver.svr.accept();
ycserver.i+=2;
ycserver.ta1.setText(ycserver.i+"");
//获得客户IP地址
System.out.println("q接h来自:"+ycserver.clt.getInetAddress());
ycserver.jl1.setText("q接h来自:"+ycserver.clt.getInetAddress());
//建立I/O?br /> }
catch(Exception ee)
{
;
}
}
}
}
public class calculator extends JFrame implements ActionListener
{//q里我把JFrame写成FrameQ这个错误找了好?br /> JFrame frame;
private JButton jia=new JButton("+");
private JButton jian=new JButton("-");
private JButton cheng=new JButton("*");
private JButton chu=new JButton("/");
private JButton qiuyi=new JButton("%");
private JButton deng=new JButton("=");
private JButton fu=new JButton("+/-");
private JButton dian=new JButton(".");
private JButton kai=new JButton("sqrt");
private JButton diao=new JButton("1/x");
private JButton aa=new JButton("A");
private JButton bb=new JButton("B");
private JButton cc=new JButton("C");
private JButton dd=new JButton("D");
private JButton ee=new JButton("E");
private JButton ff=new JButton("F");
private TextField k1=new TextField();
private objConversion convert = new objConversion();
JMenuItem copy,paste,s,t,help,about,me;
JRadioButton sixteen,ten,eight,two;
JButton backspace,ce,c,num0,num1,num2,num3,num4,num5,num6,num7,num8,num9;
Container cp;
JTextField text;
String copycontent="";
boolean clickable=true,clear=true;
int all=0;
double qian;
String fuhao;
int jin=10,first=1;
public calculator()
{
setTitle("计算器-杨彩制作");
setSize(400,300);
setLocation(250,200);
text=new JTextField(25);
// text.setEnabled(false);
text.setText("0.");
text.setHorizontalAlignment(JTextField.RIGHT);//从右到左
JPanel cp1=new JPanel();
JPanel cp2=new JPanel();
JPanel cp3=new JPanel();
cp=getContentPane();
cp.add(cp1,"North");
cp.add(cp2,"Center");
cp.add(cp3,"South");
cp1.setLayout(new GridLayout(1,6));
cp2.setLayout(new GridLayout(2,4));
cp3.setLayout(new GridLayout(6,6));
sixteen=new JRadioButton("十六q制");
sixteen.setVisible(false);
ten=new JRadioButton("十进?,true);
ten.setVisible(false);
eight=new JRadioButton("八进?);
eight.setVisible(false);
two=new JRadioButton("二进?);
two.setVisible(false);
sixteen.addActionListener(this);
ten.addActionListener(this);
eight.addActionListener(this);
two.addActionListener(this);
ButtonGroup btg=new ButtonGroup();
btg.add(sixteen);
btg.add(ten);
btg.add(eight);
btg.add(two);
JTextField t3=new JTextField(25);
cp1.add(text);
// text.setEnabled(false);
text.setEditable(false);
text.setBackground(new Color(255, 255, 255));
cp2.add(sixteen);
cp2.add(ten);
cp2.add(eight);
cp2.add(two);
backspace=new JButton("Backspace");
backspace.setForeground(new Color(255,0,0));
backspace.addActionListener(this);
ce=new JButton("CE");
ce.setForeground(new Color(255,0,0));
ce.addActionListener(this);
c=new JButton("C");
c.setForeground(new Color(255,0,0));
c.addActionListener(this);
k1.setVisible(false);
cp2.add(k1);
cp2.add(backspace);
cp2.add(ce);
cp2.add(c);
num0=new JButton("0");
num1=new JButton("1");
num2=new JButton("2");
num3=new JButton("3");
num4=new JButton("4");
num5=new JButton("5");
num6=new JButton("6");
num7=new JButton("7");
num8=new JButton("8");
num9=new JButton("9");
cp3.add(num7);
num7.addActionListener(this);
cp3.add(num8);
num8.addActionListener(this);
cp3.add(num9);
num9.addActionListener(this);
cp3.add(chu);
chu.setForeground(new Color(255,0,0));
chu.addActionListener(this);
cp3.add(kai);
kai.addActionListener(this);
cp3.add(num4);
num4.addActionListener(this);
cp3.add(num5);
num5.addActionListener(this);
cp3.add(num6);
num6.addActionListener(this);
cp3.add(cheng);
cheng.setForeground(new Color(255,0,0));
cheng.addActionListener(this);
cp3.add(qiuyi);
qiuyi.addActionListener(this);
cp3.add(num1);
num1.addActionListener(this);
cp3.add(num2);
num2.addActionListener(this);
cp3.add(num3);
num3.addActionListener(this);
cp3.add(jian);
jian.setForeground(new Color(255,0,0));
jian.addActionListener(this);
cp3.add(diao);
diao.addActionListener(this);
cp3.add(num0);
num0.addActionListener(this);
cp3.add(fu);
fu.addActionListener(this);
cp3.add(dian);
dian.addActionListener(this);
cp3.add(jia);
jia.setForeground(new Color(255,0,0));
jia.addActionListener(this);
cp3.add(deng);
deng.setForeground(new Color(255,0,0));
deng.addActionListener(this);
cp3.add(aa);
aa.addActionListener(this);
cp3.add(bb);
bb.addActionListener(this);
cp3.add(cc);
cc.addActionListener(this);
cp3.add(dd);
dd.addActionListener(this);
cp3.add(ee);
ee.addActionListener(this);
cp3.add(ff);
ff.addActionListener(this);
aa.setVisible(false);
bb.setVisible(false);
cc.setVisible(false);
dd.setVisible(false);
ee.setVisible(false);
ff.setVisible(false);
JMenuBar mainMenu = new JMenuBar();
setJMenuBar(mainMenu);
JMenu editMenu = new JMenu("~辑");
JMenu viewMenu = new JMenu("查看");
JMenu helpMenu = new JMenu("帮助");
mainMenu.add(editMenu);
mainMenu.add(viewMenu);
mainMenu.add(helpMenu);
copy = new JMenuItem(" 复制");
paste = new JMenuItem(" _脓");
KeyStroke copyks=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK);
copy.setAccelerator(copyks);//讄退单选项加上快捷?br /> KeyStroke pasteks=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK);
paste.setAccelerator(pasteks);//讄退单选项加上快捷?br /> editMenu.add(copy);
editMenu.add(paste);
copy.addActionListener(this);
paste.addActionListener(this);
t = new JMenuItem("●标准型");
s = new JMenuItem(" U学?);
viewMenu.add(t);
viewMenu.add(s);
t.addActionListener(this);
s.addActionListener(this);
help = new JMenuItem(" 帮助主题");
about = new JMenuItem(" 关于计算?);
me = new JMenuItem(" 作者主?);
helpMenu.add(help);
helpMenu.add(about);
helpMenu.add(me);
help.addActionListener(this);
about.addActionListener(this);
me.addActionListener(this);
addWindowListener(new WindowDestroyer());//l束H口
}
public void actionPerformed(ActionEvent e)
{//响应动作代码
if(first==1)
text.setText("");
first=0;//W一ơ把文本?.清空
Object temp = e.getSource();
if(temp==copy)
{
copycontent = text.getText();
}
if(temp==paste)
{
text.setText(text.getText()+copycontent);
}
if(temp==t)
{//标准
sixteen.setVisible(false);
ten.setVisible(false);
eight.setVisible(false);
two.setVisible(false);
t.setText("●标准型");
s.setText(" U学?);
aa.setVisible(false);
bb.setVisible(false);
cc.setVisible(false);
dd.setVisible(false);
ee.setVisible(false);
ff.setVisible(false);
}
if(temp==s)
{//U学
sixteen.setVisible(true);
ten.setVisible(true);
eight.setVisible(true);
two.setVisible(true);
t.setText(" 标准?);
s.setText("●科学型");
aa.setVisible(true);
bb.setVisible(true);
cc.setVisible(true);
dd.setVisible(true);
ee.setVisible(true);
ff.setVisible(true);
aa.setEnabled(false);
bb.setEnabled(false);
cc.setEnabled(false);
dd.setEnabled(false);
ee.setEnabled(false);
ff.setEnabled(false);
}
if(temp==help)
{ //打开pȝ帮助文g 要查资料
try
{
String filePath = "C:/WINDOWS/Help/calc.chm";
Runtime.getRuntime().exec("cmd.exe /c "+filePath);
}
catch(Exception eeee)
{
System.out.println("打开pȝ的计器出错");
}
}
if(temp==about)
{
JOptionPane.showMessageDialog(frame," Java计算器\n 杨彩 制作\n\n }
if(temp==me)
{
try
{
Process p = Runtime.getRuntime().exec("explorer }
catch(Exception eeee)
{
System.out.println("打开指定|页出错");
}
}
try
{
if(temp==sixteen)
{
String hex=text.getText();
int inthex=Integer.parseInt(hex,jin);//先把数变?0q制
text.setText(convert.decHex(inthex)) ;
jin=16;
aa.setEnabled(true);
bb.setEnabled(true);
cc.setEnabled(true);
dd.setEnabled(true);
ee.setEnabled(true);
ff.setEnabled(true);
num2.setEnabled(true);
num3.setEnabled(true);
num4.setEnabled(true);
num5.setEnabled(true);
num6.setEnabled(true);
num7.setEnabled(true);
num8.setEnabled(true);
num9.setEnabled(true);
}
if(temp==eight)
{
String oct =text.getText();
int intoct=Integer.parseInt(oct,jin);
text.setText(convert.decOct(intoct)) ;
jin=8;
aa.setEnabled(false);
bb.setEnabled(false);
cc.setEnabled(false);
dd.setEnabled(false);
ee.setEnabled(false);
ff.setEnabled(false);
num2.setEnabled(true);
num3.setEnabled(true);
num4.setEnabled(true);
num5.setEnabled(true);
num6.setEnabled(true);
num7.setEnabled(true);
num8.setEnabled(false);
num9.setEnabled(false);
}
if(temp==two)
{
String bin=text.getText();
int intbin=Integer.parseInt(bin,jin);
text.setText(convert.decBin(intbin));
jin=2;
aa.setEnabled(false);
bb.setEnabled(false);
cc.setEnabled(false);
dd.setEnabled(false);
ee.setEnabled(false);
ff.setEnabled(false);
num2.setEnabled(false);
num3.setEnabled(false);
num4.setEnabled(false);
num5.setEnabled(false);
num6.setEnabled(false);
num7.setEnabled(false);
num8.setEnabled(false);
num9.setEnabled(false);
}
if(temp==ten)
{
String dec=text.getText();
int intdec=Integer.parseInt(dec,jin);
// text.setText(convert.decDec(intdec)); //本句会把123变成321
text.setText(intdec+"");
jin=10;
aa.setEnabled(false);
bb.setEnabled(false);
cc.setEnabled(false);
dd.setEnabled(false);
ee.setEnabled(false);
ff.setEnabled(false);
num2.setEnabled(true);
num3.setEnabled(true);
num4.setEnabled(true);
num5.setEnabled(true);
num6.setEnabled(true);
num7.setEnabled(true);
num8.setEnabled(true);
num9.setEnabled(true);
}
}
catch(Exception ee)
{
System.out.println("转换出错,可能你没有输入Q何字W?);
text.setText("转换出错");
clear=false;
}
if(temp==backspace)
{//退?br /> String s = text.getText();
text.setText("");
for (int i = 0; i < s.length() - 1; i++)
{
char a = s.charAt(i);
text.setText(text.getText() + a);
}
}
if (temp==ce)
{
text.setText("0.");
clear=true;
first=1;
}
if (temp==c)
{
text.setText("0."); ;
clear=true;
first=1;
}
if(temp==num0)
{
if(clear==false)//判断是否点击了符号位
text.setText("");
text.setText(text.getText()+"0");
}
if(temp==num1)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"1");
clear=true;//W二ơ不在清I(前二句)
}
if(temp==num2)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"2");
clear=true;
}
if(temp==num3)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"3");
clear=true;
}
if(temp==num4)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"4");
clear=true;
}
if(temp==num5)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"5");
clear=true;
}
if(temp==num6)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"6");
clear=true;
}
if(temp==num7)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"7");
clear=true;
}
if(temp==num8)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"8");
clear=true;
}
if(temp==num9)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"9");
clear=true;
}
if(temp==aa)
{
text.setText(text.getText()+"A");
}
if(temp==bb)
{
text.setText(text.getText()+"B");
}
if(temp==cc)
{
text.setText(text.getText()+"C");
}
if(temp==dd)
{
text.setText(text.getText()+"D");
}
if(temp==ee)
{
text.setText(text.getText()+"E");
}
if(temp==ff)
{
text.setText(text.getText()+"F");
}
if(temp==dian)
{
clickable=true;
for (int i = 0; i < text.getText().length(); i++)
if ('.' == text.getText().charAt(i))
{
clickable=false;
break;
} //W一层判断是否里面含有小数点;
if(clickable==true)//W二坛判?br /> text.setText(text.getText()+".");
}
try
{
if(temp==jia)
{//加法
qian=Double.parseDouble(text.getText());
fuhao="+";
clear=false;
}
if(temp==jian)
{
qian=Double.parseDouble(text.getText());
fuhao="-";
clear=false;
;
}
if(temp==cheng)
{
qian=Double.parseDouble(text.getText());
fuhao="*";
clear=false;
}
if(temp==chu)
{
qian=Double.parseDouble(text.getText());
fuhao="/";
clear=false;
}
if(temp==deng)
{
double ss=Double.parseDouble(text.getText());
text.setText("");
if(fuhao=="+")
text.setText(qian+ss+"");
if(fuhao=="-")
text.setText(qian-ss+"");
if(fuhao=="*")
text.setText(qian*ss+"");
if(fuhao=="/")
text.setText(qian/ss+"");
clear=false;//要清I前一ơ的数据
;
}
if(temp==kai)
{
String s = text.getText();
if (s.charAt(0) == '-')
{
text.setText("负数不能开根号");
}
else
text.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText()))));
clear=false;
}
if(temp==diao)
{
if (text.getText().charAt(0) == '0'&&text.getText().length() == 1)
{
text.setText("除数不能为零");
}
else
{
boolean isDec = true;
int i, j, k;
String s = Double.toString(1 / Double.parseDouble(text.getText()));
for (i = 0; i < s.length(); i++)
if (s.charAt(i) == '.')
break;
for (j = i + 1; j < s.length(); j++)
if (s.charAt(j) != '0')
{
isDec = false;
break;
}
if (isDec == true)
{
String stemp = "";
for (k = 0; k < i; k++)
stemp += s.charAt(k);
text.setText(stemp);
}
else
text.setText(s);
}
clear=false;
}
if(temp==qiuyi)
{
text.setText("0");
clear=false;
}
if (temp == fu)
{ //导师Q此Ҏ参考书中例?br /> boolean isNumber = true;
String s = text.getText();
for (int i = 0; i < s.length(); i++)
if (! (s.charAt(i) >= '0' && s.charAt(i) <= '9' || s.charAt(i) == '.' ||
s.charAt(i) == '-'))
{
isNumber = false;
break;
}
if (isNumber == true)
{
//如果当前字符串首字母?-'?代表现在是个负数,再按下时,则将首符号去?
if (s.charAt(0) == '-')
{
text.setText("");
for (int i = 1; i < s.length(); i++)
{
char a = s.charAt(i);
text.setText(text.getText() + a);
}
}
//如果当前字符串第一个字W不是符P则添加一个符号在首字母处
else
text.setText('-' + s);
}
}
}
catch(Exception eee)
{
System.out.println("q算?首先输入数字或字W?);
text.setText("q算出错");
clear=false;
}
}
class WindowDestroyer extends WindowAdapter
{//退出窗口动?br /> public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class objConversion
{//导师,本进制类参考了CSMDc{换例?br />
public void objConversion ()
{
}
public String decDec (int decNum)
{//10
String strDecNum = Integer.toString(decNum);
for (int i = strDecNum.length(); i < 3; i++)
{
strDecNum = "0" + strDecNum;
}
// return strDecNum;
return invert (strDecNum, 5);
}
public String decHex (int decNum)
{//10 to 16
String strHexNum = "";
int currentNum = 0;
while (decNum != 0)
{
if (decNum > 15)
{
currentNum = decNum % 16;
decNum /= 16;
}
else
{
currentNum = decNum;
decNum = 0;
}
switch (currentNum)
{
case 15: strHexNum += "F";
break;
case 14: strHexNum += "E";
break;
case 13: strHexNum += "D";
break;
case 12: strHexNum += "C";
break;
case 11: strHexNum += "B";
break;
case 10: strHexNum += "A";
break;
default: strHexNum += Integer.toString(currentNum);
break;
}
}
return invert (strHexNum, 2);
}
public String decOct (int decNum)
{//10 to 8
String strOctNum = "";
while (decNum != 0)
{
if (decNum > 7)
{
strOctNum += Integer.toString(decNum % 8);
decNum /= 8;
}
else
{
strOctNum += Integer.toString(decNum);
decNum = 0;
}
}
return invert (strOctNum, 3);
}
public String decBin (int decNum)
{//10 to 2
String strBinNum = "";
while (decNum != 0)
{
if (decNum > 1)
{
strBinNum += Integer.toString(decNum % 2);
decNum /= 2;
}
else
{
strBinNum += Integer.toString(decNum);
decNum = 0;
}
}
return invert (strBinNum, 8);
}
private String invert (String strNum, int minLength) //转换长度
{
String answer = "";
int length = strNum.length();
if (length < minLength)
{
for (int padding = (minLength - length); padding > 0; padding--)
{
answer += "0";
}
}
for (int i = length; i > 0; i--)
{
answer += strNum.charAt (i - 1);
}
return answer;
}
}
public static void main(String arg[])//产生H口
{
calculator win = new calculator();
win.setVisible(true);
}
}