package com.regex;
公司主營業務:做網站、網站設計、移動網站開發等業務。幫助企業客戶真正實現互聯網宣傳,提高企業的競爭能力。創新互聯是一支青春激揚、勤奮敬業、活力青春激揚、勤奮敬業、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰,讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創新互聯推出確山免費做網站回饋大家。
import java.io.*;
import java.net.URLDecoder;
import java.util.regex.*;
public class Regex {
private int REMARK=0;
private int LOGIC=0;
private int PHYSIC=0;
boolean start=false;
/**
* @param args
*/
public static void main(String[] args) { //測試方法
// TODO Auto-generated method stub
Regex re=new Regex();
re.regCount("Regex.java");
System.out.println("remark Line: "+re.REMARK);
System.out.println("logic Line: "+re.LOGIC);
System.out.println("physic Line: "+re.PHYSIC);
}/**
* @author BlueDance
* @param s
* @deprecated count
*/
public void regCount(String s){
String url=null;
try {
url=URLDecoder.decode(this.getClass().getResource(s).getPath(),"UTF-8");
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
try {
BufferedReader br=new BufferedReader(new FileReader(new File(url)));
String s1=null;
while((s1=br.readLine())!=null){
PHYSIC++;
if(CheckChar(s1)==1){
REMARK++;
System.out.println("純注釋行:"+s1);
}
if(CheckChar(s1)==2){
LOGIC++;
REMARK++;
System.out.println("非純注釋行:"+s1);
}
if(CheckChar(s1)==3)
LOGIC++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
/**
*
* @param s
* @return int
* @version check s
*/
public int CheckChar(String s){
String s1=null;
if(s!=null)
s1=s.trim();
//System.out.println(regCheck(s1,re));
if(regCheck(s1,"(//.*)")) //判斷//開頭的為純注釋行
return 1;
if(regCheck(s1,"(.*[;{})] *//.*)")) //判斷不是//開頭的非純注釋行
return 2;
if(regCheck(s1,"(//*.*)")){ //判斷/*開頭的純注釋行
start=true;
return 1;
}
if(regCheck(s1,"(.*[;{})]//*.*)")){ //判斷不是/*開頭的非純注釋行
start=true;
return 2;
}
if(regCheck(s1,"(.* */*/)")){ //判斷*/結尾的純注釋行
start=false;
return 1;
}
if(regCheck(s1,"(.* */*/.*)")!strCheck(s1)){ //判斷不是*/結尾的非純注釋行
if(strCheck(s1)){
start=false;
return 2;
}
}
if(start==true) //狀態代碼,start即/*開始時start=true*/結束時為false
return 1;
return 3;//ssssllll
}//aeee
/**
*
* @param s
* @param re
* @return boolean
*/
public boolean regCheck(String s,String re){ //正則表達試判斷方法
return Pattern.matches(re,s);
}
public boolean strCheck(String s){ //中間有*/的字符判斷 此方法最關鍵
if(s.indexOf("*/")0){
int count=0;
String y[]=s.split("/*/");
boolean boo[]=new boolean[y.length];
for (int i = 0; i y.length-1; i++) {
char c[]=y[i].toCharArray();
for (int j = 0; j c.length; j++) {
if(c[j]=='\\'c[j+1]=='"'){
count++;
}
}
if(count%2==0){
if(countNumber("\"",y[i])%2!=0){
boo[i]=true;
}else{
boo[i]=false;
}
}else{
if(countNumber("\"",y[i])%2==0){
boo[i]=true;
}else{
boo[i]=false;
}
}
}
for(int i=0;iboo.length;i++){
if(!boo[i])
return false;
}
return true;
}
return false;
}
public int countNumber(String s,String y){ //此方法為我前面寫的字符串出現次數統計方法,不懂的可以看我前面的文章
int count=0;
String [] k=y.split(s);
if(y.lastIndexOf(s)==(y.length()-s.length()))
count=k.length;
else
count=k.length-1;
if(count==0)
System.out.println ("字符串\""+s+"\"在字符串\""+y+"\"沒有出現過");
else
return count;
return -1;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GoodLucky extends JFrame implements ActionListener{
JTextField tf = new JTextField(); //實例化一個文本域
//設置兩個按鈕
JButton b1 = new JButton("開始");
JButton b2 = new JButton("停止");
boolean isGo = false;
//構造函數
public GoodLucky(){
b1.setActionCommand("start");//在開始按鈕上設置一個動作監聽 start
JPanel p = new JPanel(); //實例化一個可視化容器
//將兩個按鈕添加到可視化容器上面,用add方法
p.add(b1);
p.add(b2);
//在兩個按鈕上增加監聽的屬性,自動調用下面的監聽處理方法actionPerformed(ActionEvent e),如果要代碼有更好的可讀性,可用內部類實現動作
//監聽處理。
b1.addActionListener(this);
b2.addActionListener(this);
//將停止按鈕設置為不可編輯(即不可按的狀態)
b2.setEnabled(false);
this.getContentPane().add(tf,"North"); //將上面的文本域放在面板的北方,也就是上面(上北下南左西右東)
this.getContentPane().add(p,"South"); //將可視化容器pannel放在南邊,也就是下面
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置用戶在此窗體上發起 "close" 時默認執行的操作,參數EXIT_ON_CLOSE是使用 System exit 方法退出應用程序。僅在應用程序中使用
this.setSize(300,200); //設置面板大小,寬和高
this.setLocation(300,300); //設置面板剛開始的出現的位置
Cursor cu = new Cursor(Cursor.HAND_CURSOR); //用指定名稱創建一個新的定制光標對象,參數表示手狀光標類型
this.setCursor(cu); //為指定的光標設置光標圖像,即設置光標圖像為上面所創建的手狀光標類型
this.setVisible(true); //將面板可視化設置為true,即可視,如果為false,即程序運行時面板會隱藏
tf.setText("welcome you! "); //設置面板的標題為歡迎
this.go(); //調用go方法
}
public void go(){
while(true){ //這里是死循環,也就是說用戶不點擊停止按鈕的話他一直循環出現隨機數,直到用戶點擊停止按鈕循環才能推出,具體流程在actionPerformed方法中控制。
if(isGo == true){ //上面所定義的isGo的初始值為false,所以程序第一次到此會跳過
String s = ""; //設置空字符串
for(int j = 1; j = 7;j++){ //產生7個隨機數
int i = (int)(Math.random() * 36) + 1;//每個隨機數產生方式,這里定義靈活,可以自由定義隨機數產生的方式
if(i 10){
s = s + " 0" + i; //如果產生的隨機數小于10的話做處理:這里就牽扯到一個重要的概念,簡單敘述一下:
/*
當一個字符串與一個整型數項相加的意思是連接,上面的s = s + " 0" + i的意思是字符串s鏈接0再連接整型i值,而不會導致0和整型的i相加,
產生的效果為s0i,由于s為空字符串(上面定義過的),所以當i小于零時,在個位數前面加上0,比如產生的隨機數i為7的話,顯示效果為 07.
*/
}else{
s = s + " " + i; //如果產生的隨機數比10打的話,那么加上空格顯示,即數字和數字之間有個空格
}
//以上循環循環七次,以保證能出現7個隨機數
}
tf.setText(s); //將產生的隨機數全部顯示在文本域上,用文本域對象tf調用它的設置文本的方法setText(String)實現。
}
//以下為線程延遲
try{
Thread.sleep(10); //線程類同步方法sleep,睡眠方法,括號里的單位為ms。
}catch(java.lang.InterruptedException e){
e.printStackTrace(); //異常捕獲,不用多說。
}
}
}
//以下是上面設置的事件監聽的具體處理辦法,即監聽時間處理方法,自動調用
public void actionPerformed(ActionEvent e){ //傳入一個動作事件的參數e
String s = e.getActionCommand(); //設置字符串s來存儲獲得動作監聽,上面的start
/*
以下這個條件語句塊的作用為:用戶點擊開始后(捕獲start,用方法getActionCommand()),將命令觸發設置為true,從而執行上面的go方法中的循環體(因為循環體中要求isGo參數為true,而初始為false)。
執行循環快產生隨機數,并將開始按鈕不可編輯化,而用戶只可以使用停止按鈕去停止。如果用戶按下停止時,也就是沒有傳入參數“start”的時候,
執行else語句塊中的語句,isGo設置為false,將不執行上面go中的循環語句塊,從而停止產生隨機數,并顯示,并且把開始按鈕設置為可用,而把
停止按鈕設置為不可用,等待用戶按下開始再去開始新一輪循環產生隨機數。
*/
if(s.equals("start")){ //如果捕獲到start,也就是用戶觸發了動作監聽器,那么下面處理
isGo = true; //設置isGo為true
b1.setEnabled(false); //將開始按鈕設置為不可用
b2.setEnabled(true); //將停止按鈕設置為可用
}else{
isGo = false; //將isGo設置為false,isGo為循環標志位
b2.setEnabled(false); //設置停止按鈕為不可用(注意看是b2,b2是停止按鈕)
b1.setEnabled(true); //設置開始按鈕為可用
}
}
public static void main(String[] args){
new GoodLucky(); //產生類的實例,執行方法
}
}
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
public class Counter extends Frame
{
//聲明三個面板的布局
GridLayout gl1,gl2,gl3;
Panel p0,p1,p2,p3;
JTextField tf1;
TextField tf2;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26;
StringBuffer str;//顯示屏所顯示的字符串
double x,y;//x和y都是運算數
int z;//Z表示單擊了那一個運算符.0表示"+",1表示"-",2表示"*",3表示"/"
static double m;//記憶的數字
public Counter()
{
gl1=new GridLayout(1,4,10,0);//實例化三個面板的布局
gl2=new GridLayout(4,1,0,15);
gl3=new GridLayout(4,5,10,15);
tf1=new JTextField(27);//顯示屏
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setEnabled(false);
tf1.setText("0");
tf2=new TextField(10);//顯示記憶的索引值
tf2.setEditable(false);
//實例化所有按鈕、設置其前景色并注冊監聽器
b0=new Button("Backspace");
b0.setForeground(Color.red);
b0.addActionListener(new Bt());
b1=new Button("CE");
b1.setForeground(Color.red);
b1.addActionListener(new Bt());
b2=new Button("C");
b2.setForeground(Color.red);
b2.addActionListener(new Bt());
b3=new Button("MC");
b3.setForeground(Color.red);
b3.addActionListener(new Bt());
b4=new Button("MR");
b4.setForeground(Color.red);
b4.addActionListener(new Bt());
b5=new Button("MS");
b5.setForeground(Color.red);
b5.addActionListener(new Bt());
b6=new Button("M+");
b6.setForeground(Color.red);
b6.addActionListener(new Bt());
b7=new Button("7");
b7.setForeground(Color.blue);
b7.addActionListener(new Bt());
b8=new Button("8");
b8.setForeground(Color.blue);
b8.addActionListener(new Bt());
b9=new Button("9");
b9.setForeground(Color.blue);
b9.addActionListener(new Bt());
b10=new Button("/");
b10.setForeground(Color.red);
b10.addActionListener(new Bt());
b11=new Button("sqrt");
b11.setForeground(Color.blue);
b11.addActionListener(new Bt());
b12=new Button("4");
b12.setForeground(Color.blue);
b12.addActionListener(new Bt());
b13=new Button("5");
b13.setForeground(Color.blue);
b13.addActionListener(new Bt());
b14=new Button("6");
b14.setForeground(Color.blue);
b14.addActionListener(new Bt());
b15=new Button("*");
b15.setForeground(Color.red);
b15.addActionListener(new Bt());
b16=new Button("%");
b16.setForeground(Color.blue);
b16.addActionListener(new Bt());
b17=new Button("1");
b17.setForeground(Color.blue);
b17.addActionListener(new Bt());
b18=new Button("2");
b18.setForeground(Color.blue);
b18.addActionListener(new Bt());
b19=new Button("3");
b19.setForeground(Color.blue);
b19.addActionListener(new Bt());
b20=new Button("-");
b20.setForeground(Color.red);
b20.addActionListener(new Bt());
b21=new Button("1/X");
b21.setForeground(Color.blue);
b21.addActionListener(new Bt());
b22=new Button("0");
b22.setForeground(Color.blue);
b22.addActionListener(new Bt());
b23=new Button("+/-");
b23.setForeground(Color.blue);
b23.addActionListener(new Bt());
b24=new Button(".");
b24.setForeground(Color.blue);
b24.addActionListener(new Bt());
b25=new Button("+");
b25.setForeground(Color.red);
b25.addActionListener(new Bt());
b26=new Button("=");
b26.setForeground(Color.red);
b26.addActionListener(new Bt());
//實例化四個面板
p0=new Panel();
p1=new Panel();
p2=new Panel();
p3=new Panel();
//創建一個空字符串緩沖區
str=new StringBuffer();
//添加面板p0中的組件和設置其在框架中的位置和大小
p0.add(tf1);
p0.setBounds(10,25,300,40);
//添加面板p1中的組件和設置其在框架中的位置和大小
p1.setLayout(gl1);
p1.add(tf2);
p1.add(b0);
p1.add(b1);
p1.add(b2);
p1.setBounds(10,65,300,25);
//添加面板p2中的組件并設置其的框架中的位置和大小
p2.setLayout(gl2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.setBounds(10,110,40,150);
//添加面板p3中的組件并設置其在框架中的位置和大小
p3.setLayout(gl3);//設置p3的布局
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(b10);
p3.add(b11);
p3.add(b12);
p3.add(b13);
p3.add(b14);
p3.add(b15);
p3.add(b16);
p3.add(b17);
p3.add(b18);
p3.add(b19);
p3.add(b20);
p3.add(b21);
p3.add(b22);
p3.add(b23);
p3.add(b24);
p3.add(b25);
p3.add(b26);
p3.setBounds(60,110,250,150);
//設置框架中的布局為空布局并添加4個面板
setLayout(null);
add(p0);
add(p1);
add(p2);
add(p3);
setResizable(false);//禁止調整框架的大小
//匿名類關閉窗口
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1)
{
System.exit(0);
}
});
setBackground(Color.lightGray);
setBounds(100,100,320,280);
setVisible(true);
}
//構造監聽器
class Bt implements ActionListener
{
public void actionPerformed(ActionEvent e2)
{
try{
if(e2.getSource()==b1)//選擇"CE"清零
{
tf1.setText("0");//把顯示屏清零
str.setLength(0);//清空字符串緩沖區以準備接收新的輸入運算數
}
else if(e2.getSource()==b2)//選擇"C"清零
{
tf1.setText("0");//把顯示屏清零
str.setLength(0);
}
else if(e2.getSource()==b23)//單擊"+/-"選擇輸入的運算數是正數還是負數
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(-x));
}
else if(e2.getSource()==b25)//單擊加號按鈕獲得x的值和z的值并清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);//清空緩沖區以便接收新的另一個運算數
y=0d;
z=0;
}
else if(e2.getSource()==b20)//單擊減號按鈕獲得x的值和z的值并清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=1;
}
else if(e2.getSource()==b15)//單擊乘號按鈕獲得x的值和z的值并清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=2;
}
else if(e2.getSource()==b10)//單擊除號按鈕獲得x的值和z的值并空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=3;
}
else if(e2.getSource()==b26)//單擊等號按鈕輸出計算結果
{
str.setLength(0);
switch(z)
{
case 0 : tf1.setText(""+(x+y));break;
case 1 : tf1.setText(""+(x-y));break;
case 2 : tf1.setText(""+(x*y));break;
case 3 : tf1.setText(""+(x/y));break;
}
}
else if(e2.getSource()==b24)//單擊"."按鈕輸入小數
{
if(tf1.getText().trim().indexOf(′.′)!=-1)//判斷字符串中是否已經包含了小數點
{
}
else//如果沒數點有小
{
if(tf1.getText().trim().equals("0"))//如果初時顯示為0
{
str.setLength(0);
tf1.setText((str.append("0"+e2.getActionCommand())).toString());
}
else if(tf1.getText().trim().equals(""))//如果初時顯示為空則不做任何操作
{
}
else
{
tf1.setText(str.append(e2.getActionCommand()).toString());
}
}
y=0d;
}
else if(e2.getSource()==b11)//求平方根
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText("數字格式異常");
if(x0)
tf1.setText("負數沒有平方根");
else
tf1.setText(""+Math.sqrt(x));
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b16)//單擊了"%"按鈕
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(0.01*x));
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b21)//單擊了"1/X"按鈕
{
x=Double.parseDouble(tf1.getText().trim());
if(x==0)
{
tf1.setText("除數不能為零");
}
else
{
tf1.setText(""+(1/x));
}
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b3)//MC為清除內存
{
m=0d;
tf2.setText("");
str.setLength(0);
}
else if(e2.getSource()==b4)//MR為重新調用存儲的數據
{
if(tf2.getText().trim()!="")//有記憶數字
{
tf1.setText(""+m);
}
}
else if(e2.getSource()==b5)//MS為存儲顯示的數據
{
m=Double.parseDouble(tf1.getText().trim());
tf2.setText("M");
tf1.setText("0");
str.setLength(0);
}
else if(e2.getSource()==b6)//M+為將顯示的數字與已經存儲的數據相加要查看新的數字單擊MR
{
m=m+Double.parseDouble(tf1.getText().trim());
}
else//選擇的是其他的按鈕
{
if(e2.getSource()==b22)//如果選擇的是"0"這個數字鍵
{
if(tf1.getText().trim().equals("0"))//如果顯示屏顯示的為零不做操作
{
}
else
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf1.getText().trim());
}
}
else if(e2.getSource()==b0)//選擇的是“BackSpace”按鈕
{
if(!tf1.getText().trim().equals("0"))//如果顯示屏顯示的不是零
{
if(str.length()!=1)
{
tf1.setText(str.delete(str.length()-1,str.length()).toString());//可能拋出字符串越界異常
}
else
{
tf1.setText("0");
str.setLength(0);
}
}
y=Double.parseDouble(tf1.getText().trim());
}
else//其他的數字鍵
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf1.getText().trim());
}
}
}
catch(NumberFormatException e){
tf1.setText("數字格式異常");
}
catch(StringIndexOutOfBoundsException e){
tf1.setText("字符串索引越界");
}
}
}
public static void main(String args[])
{
new Counter();
SessionFactory sessionFactory = cfg.buildSessionFactory();
你沒有理解這行代碼的意義,這行代碼cfg.buildSessionFactory()返回的不是SessionFactory,而是實現了SessionFactory 這個接口的一個類,這個具體實現了SessionFactory接口的類是在hibernate的配置文件中配置的
你說的java源代碼是指編譯成的class文件前的java文件。
當我們運行.java文件時,它會被系統編譯成.class文件,例如Test.java編譯之后就是Test.class,
源文件就是指Test.java文件,
一般部署項目時,有.class文件就可以發布運行了,但是如果想修改這個系統,.class是不能修改的,要有.java文件才能修改
也可以上網去下反編譯軟件,就是能把.class文件大部分還原成.java文件的工具,但不是100%還原,而且如果不是正版的,小心有毒啊,什么的。
我用了半個小時 幫你寫了一個簡單的驗證用戶名和密碼登陸問題 別辜負我的好意 下面是代碼!(建好包和類 代碼粘過去就能用)
實體類 包entity
-------------------------------------------------------------
package entity;
/**
* 用戶實體類
* @author new
*
*/
public class Users {
private String name;//用戶名
private String pass;//用戶密碼
/**
* 空的構造函數 用戶實力化 此類對象
*/
public Users(){
}
/**
* 構造函數 接收用戶名和密碼
* @param name
* @param pass
*/
public Users(String name, String pass) {
this.name = name;
this.pass = pass;
}
/**
* 下面set和get方法就不用解釋了吧
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
數據庫類 包dao(我是模擬一下數據庫 沒有用到數據庫)
--------------------------------------------------------------
package dao;
import java.util.*;
import entity.Users;//導入實體類
/**
* 模擬數據庫 用戶DAO
* @author new
*
*/
public class UsersDAO {
private static Users users=new Users();
static
{
users.setName("tom");
users.setPass("jerry");
}
/**
* 根據姓名查找這個用戶 (模擬一下數據庫)
* @param name
* @return
*/
public Users findUserByName(String name)
{
if(name.equals(this.users.getName()))
{
return this.users;
}
return null;
}
}
業務類 包service (驗證用戶名和密碼)
------------------------------------------------------------
package service;
import dao.UsersDAO;
import entity.Users;
/**
* 驗證密碼 業務類
* @author new
*
*/
public class validatePass {
//實力化DAO對象
private UsersDAO us=new UsersDAO();
/**
* 驗證輸入的密碼是否正確
* @param name
* @param pass
* @return
*/
public Users validate(String name,String pass)
{
Users user=null;
user=us.findUserByName(name);
//如果不為空 說明查到了
if(user!=null)
{
//用查詢出來對象的密碼和傳過來的密碼比較
if(user.getPass().equals(pass))
{
return user;
}
}
return null;
}
}
最后是測試test類 包test
----------------------------------------------------------
package test;
import entity.Users;
import service.validatePass;
/**
* 測試類
* @author new
*
*/
public class test {
/**
* main方法 用于測試
* @param args
*/
public static void main(String[] args)
{
//實例化業務類對象
validatePass v=new validatePass();
//用戶名和密碼
String name="tom";
String pass="jerry";
//開始驗證
Users user=v.validate(name, pass);
if(user==null)
{
System.out.println("你輸入的用戶名或密碼錯誤!");
}else
{
System.out.println("你已經通過驗證,成功登陸!");
}
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;public class CaculatorA {
private JFrame jf;
private JButton[] jbs;
private JTextField jtf;
private JButton clear;
private double num1,num2,jieguo;
private char c;
/**
* 構造方法實例化屬性
*
*/
public CaculatorA(){
jf=new JFrame("我的計算器v1.0");
jtf=new JTextField(20);
clear=new JButton("clear");
jbs=new JButton[16];
String str="123+456-789*0./=";
for(int i=0; istr.length(); i++){
jbs[i]=new JButton(str.charAt(i)+"");
}
init();
addEventHandler();
// setFont();
// setColor();
showMe();
}
/**