import java.awt.BorderLayout;

成都創(chuàng)新互聯(lián)擁有10余年成都網(wǎng)站建設(shè)工作經(jīng)驗(yàn),為各大企業(yè)提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)服務(wù),對(duì)于網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、手機(jī)APP定制開發(fā)、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、申請(qǐng)域名等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營(yíng)銷、管理等網(wǎng)站化運(yùn)作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項(xiàng)目的能力。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
/**
* pTitle: Swing日歷褲拿/p
* pDescription: 操作日期/p
* @author duxu2004
* @version 1.0.1
*/
class JCalendar extends JPanel{
//動(dòng)態(tài)表示年月日
private int year=0;
private int month=0;
private int day=0;
//主面板
private JPanel Main = new JPanel();
//日面板
private JPanel jPanelDay = new JPanel();
//月面板
private JPanel jPanelMonth = new JPanel();
//年的輸入位置
private JTextField Year = new JTextField();
//月鋒純握的輸入位置
private JTextField Month = new JTextField();
//減少月份
private JButton MonthDown = new JButton();
//增加月份
private JButton MonthUp = new JButton();
private JPanel jPanelButton = new JPanel();
//減少年份
private JButton YearDown = new JButton();
//增加年份
private JButton YearUp = new JButton();
//顯示日期的位置
private JLabel Out = new JLabel();
//中國(guó)時(shí)區(qū),以后可以從這里擴(kuò)展可以設(shè)置時(shí)區(qū)的功能
private Locale l=Locale.CHINESE;
//主日歷
private GregorianCalendar cal=new GregorianCalendar(l);
//星期面板
private JPanel weekPanel=new JPanel();
//天按鈕組
private JToggleButton[] days=new JToggleButton[42];
//天面板
private JPanel Days = new JPanel();
//銀慶標(biāo)示
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JLabel jLabel5 = new JLabel();
private JLabel jLabel6 = new JLabel();
private JLabel jLabel7 = new JLabel();
//當(dāng)前選擇的天數(shù)按鈕
private JToggleButton cur=null;
//月份天數(shù)數(shù)組,用來(lái)取得當(dāng)月有多少天
// 1 2 3 4 5 6 7 8 9 10 11 12
private int[] mm={31,28,31,30,31,30,31,31,30,31,30,31};
//空日期構(gòu)造函數(shù)
public JCalendar() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//帶日期設(shè)置的構(gòu)造函數(shù)
public JCalendar(int year, int month, int day) {
cal.set(year, month, day);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//帶日歷輸入的構(gòu)造函數(shù)
public JCalendar(GregorianCalendar calendar) {
cal=calendar;
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//帶日期輸入的構(gòu)造函數(shù)
public JCalendar(Date date) {
cal.setTime(date);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//初始化組件
private void jbInit() throws Exception {
//初始化年、月、日
iniCalender();
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createRaisedBevelBorder());
this.setMaximumSize(new Dimension(200, 200));
this.setMinimumSize(new Dimension(200, 200));
this.setPreferredSize(new Dimension(200, 200));
Main.setLayout(new BorderLayout());
Main.setBackground(SystemColor.info);
Main.setBorder(null);
Out.setBackground(Color.lightGray);
Out.setHorizontalAlignment(SwingConstants.CENTER);
Out.setMaximumSize(new Dimension(100, 19));
Out.setMinimumSize(new Dimension(100, 19));
Out.setPreferredSize(new Dimension(100, 19));
jLabel1.setForeground(Color.red);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setText("日");
jLabel2.setForeground(Color.blue);
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel2.setText("六");
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel3.setText("五");
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel4.setText("四");
jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
jLabel5.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel5.setText("三");
jLabel6.setBorder(null);
jLabel6.setHorizontalAlignment(SwingConstants.CENTER);
jLabel6.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel6.setText("二");
jLabel7.setBackground(Color.lightGray);
jLabel7.setForeground(Color.black);
jLabel7.setBorder(null);
jLabel7.setHorizontalAlignment(SwingConstants.CENTER);
jLabel7.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel7.setText("一");
weekPanel.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));
weekPanel.setBorder(BorderFactory.createEtchedBorder());
weekPanel.setLayout(new GridLayout(1,7));
weekPanel.add(jLabel1, null);
weekPanel.add(jLabel7, null);
weekPanel.add(jLabel6, null);
weekPanel.add(jLabel5, null);
weekPanel.add(jLabel4, null);
weekPanel.add(jLabel3, null);
weekPanel.add(jLabel2, null);
MonthUp.setAlignmentX((float) 0.0);
MonthUp.setActionMap(null);
jPanelMonth.setBackground(SystemColor.info);
jPanelMonth.setLayout(new BorderLayout());
jPanelMonth.setBorder(BorderFactory.createEtchedBorder());
Month.setBorder(null);
Month.setHorizontalAlignment(SwingConstants.CENTER);
Month.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Month_mouseClicked(e);
}
});
Month.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
Month_keyPressed(e);
}
});
MonthDown.setBorder(null);
MonthDown.setText("\u25C4");
MonthDown.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
MonthDown_actionPerformed(e);
}
});
MonthUp.setBorder(null);
MonthUp.setText("\u25BA");
MonthUp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
MonthUp_actionPerformed(e);
}
});
jPanelButton.setLayout(null);
jPanelButton.setBorder(null);
jPanelButton.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
jPanelButtonComponentResized(evt);
}
});
Year.setBorder(BorderFactory.createEtchedBorder());
Year.setMaximumSize(new Dimension(80, 25));
Year.setMinimumSize(new Dimension(80, 25));
Year.setPreferredSize(new Dimension(80, 25));
Year.setHorizontalAlignment(SwingConstants.CENTER);
Year.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Year_mouseClicked(e);
}
});
Year.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
Year_keyPressed(e);
}
});
YearDown.setBorder(null);
YearDown.setMaximumSize(new Dimension(16, 16));
YearDown.setMinimumSize(new Dimension(16, 16));
YearDown.setPreferredSize(new Dimension(16, 16));
YearDown.setSize(new Dimension(16, 16));
YearDown.setText("▼");
YearDown.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
YearDown_actionPerformed(e);
}
});
YearUp.setBorder(null);
YearUp.setMaximumSize(new Dimension(16, 16));
YearUp.setMinimumSize(new Dimension(16, 16));
YearUp.setPreferredSize(new Dimension(16, 16));
YearUp.setSize(new Dimension(16, 16));
YearUp.setText("▲");
YearUp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
YearUp_actionPerformed(e);
}
});
jPanelDay.setLayout(new BorderLayout());
Days.setLayout(new GridLayout(6,7));
Days.setBackground(SystemColor.info);
for(int i=0;i42;i++){
days[i]=new JToggleButton();
days[i].setBorder(null);
days[i].setBackground(SystemColor.info);
days[i].setHorizontalAlignment(SwingConstants.CENTER);
days[i].setHorizontalTextPosition(SwingConstants.CENTER);
//days[i].setSize(l,l);
days[i].addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
day=Integer.parseInt(((JToggleButton)e.getSource()).getText());
showDate();
showDays();
}
});
Days.add(days[i]);
}
this.add(Main, BorderLayout.NORTH);
this.add(jPanelDay, BorderLayout.CENTER);
this.add(jPanelMonth, BorderLayout.SOUTH);
Main.add(Year, BorderLayout.CENTER);
Main.add(Out, BorderLayout.WEST);
Main.add(jPanelButton, BorderLayout.EAST);
jPanelButton.add(YearUp);
jPanelButton.add(YearDown);
jPanelDay.add(weekPanel,BorderLayout.NORTH);
jPanelDay.add(Days, BorderLayout.CENTER);
jPanelMonth.add(Month, BorderLayout.CENTER);
jPanelMonth.add(MonthDown, BorderLayout.WEST);
jPanelMonth.add(MonthUp, BorderLayout.EAST);
showMonth();
showYear();
showDate();
showDays();
}
//自定義重畫年選擇面板
void jPanelButtonComponentResized(java.awt.event.ComponentEvent evt){
YearUp.setLocation(0,0);
YearDown.setLocation(0,YearUp.getHeight());
jPanelButton.setSize(YearUp.getWidth(),YearUp.getHeight()*2);
jPanelButton.setPreferredSize(new Dimension(YearUp.getWidth(),YearUp.getHeight()*2));
jPanelButton.updateUI();
}
//測(cè)試用
public static void main(String[] args){
JFrame f=new JFrame();
f.setContentPane(new JCalendar());
f.pack();
//f.setResizable(false);
f.show();
}
//增加年份
void YearUp_actionPerformed(ActionEvent e) {
year++;
showYear();
showDate();
showDays();
}
//減少年份
void YearDown_actionPerformed(ActionEvent e) {
year--;
showYear();
showDate();
showDays();
}
//減少月份
void MonthDown_actionPerformed(ActionEvent e) {
month--;
if(month0) {
month = 11;
year--;
showYear();
}
showMonth();
showDate();
showDays();
}
//增加月份
void MonthUp_actionPerformed(ActionEvent e) {
month++;
if(month==12) {
month=0;
year++;
showYear();
}
showMonth();
showDate();
showDays();
}
//初始化年月日
void iniCalender(){
year=cal.get(Calendar.YEAR);
month=cal.get(Calendar.MONTH);
day=cal.get(Calendar.DAY_OF_MONTH);
}
//刷新月份
void showMonth(){
Month.setText(Integer.toString(month+1)+"月");
}
//刷新年份
void showYear(){
Year.setText(Integer.toString(year)+"年");
}
//刷新日期
void showDate(){
Out.setText(Integer.toString(year)+"-"+Integer.toString(month+1)+"-"+Integer.toString(day));
}
//重畫天數(shù)選擇面板
void showDays() {
cal.set(year,month,1);
int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int n=mm[month];
if(cal.isLeapYear(year)month==1) n++;
int i=0;
for(;ifirstDayOfWeek-1;i++){
days[i].setEnabled(false);
days[i].setSelected(false);
days[i].setText("");
}
int d=1;
for(;d=n;d++){
days[i].setText(Integer.toString(d));
days[i].setEnabled(true);
if(d==day) days[i].setSelected(true);
else days[i].setSelected(false)
i++;
}
for(;i42;i++){
days[i].setEnabled(false);
days[i].setSelected(false);
days[i].setText("");
}
}
//單擊年份面板選擇整個(gè)年份字符串
void SelectionYear(){
Year.setSelectionStart(0);
Year.setSelectionEnd(Year.getText().length());
}
//單擊月份面板選擇整個(gè)月份字符串
void SelectionMonth(){
Month.setSelectionStart(0);
Month.setSelectionEnd(Month.getText().length());
}
//月份面板響應(yīng)鼠標(biāo)單擊事件
void Month_mouseClicked(MouseEvent e) {
//SelectionMonth();
inputMonth();
}
//檢驗(yàn)輸入的月份
void inputMonth(){
String s;
if(Month.getText().endsWith("月"))
{
s=Month.getText().substring(0,Month.getText().length()-1);
}
else s=Month.getText();
month=Integer.parseInt(s)-1;
this.showMe();
}
//月份面板鍵盤敲擊事件響應(yīng)
void Month_keyPressed(KeyEvent e) {
if(e.getKeyChar()==10)
inputMonth();
}
//年份面板響應(yīng)鼠標(biāo)單擊事件
void Year_mouseClicked(MouseEvent e) {
//SelectionYear();
inputYear();
}
//年份鍵盤敲擊事件響應(yīng)
void Year_keyPressed(KeyEvent e) {
//System.out.print(new Integer(e.getKeyChar()).byteValue());
if(e.getKeyChar()==10)
inputYear();
}
//檢驗(yàn)輸入的年份字符串
void inputYear() {
String s;
if(Year.getText().endsWith("年"))
{
s=Year.getText().substring(0,Year.getText().length()-1);
}
else s=Year.getText();
year=Integer.parseInt(s);
this.showMe();
}
//以字符串形式返回日期,yyyy-mm-dd
public String getDate(){return Out.getText();}
//以字符串形式輸入日期,yyyy-mm-dd
public void setDate(String date){
if(date!=null){
StringTokenizer f = new StringTokenizer(date, "-");
if(f.hasMoreTokens())
year = Integer.parseInt(f.nextToken());
if(f.hasMoreTokens())
month = Integer.parseInt(f.nextToken());
if(f.hasMoreTokens())
day = Integer.parseInt(f.nextToken());
cal.set(year,month,day);
}
this.showMe();
}
//以日期對(duì)象形式輸入日期
public void setTime(Date date){
cal.setTime(date);
this.iniCalender();
this.showMe();
}
//返回日期對(duì)象
public Date getTime(){return cal.getTime();}
//返回當(dāng)前的日
public int getDay() {
return day;
}
//設(shè)置當(dāng)前的日
public void setDay(int day) {
this.day = day;
cal.set(this.year,this.month,this.day);
this.showMe();
}
//設(shè)置當(dāng)前的年
public void setYear(int year) {
this.year = year;
cal.set(this.year,this.month,this.day);
this.showMe();
}
//返回當(dāng)前的年
public int getYear() {
return year;
}
//返回當(dāng)前的月
public int getMonth() {
return month;
}
//設(shè)置當(dāng)前的月
public void setMonth(int month) {
this.month = month;
cal.set(this.year,this.month,this.day);
this.showMe();
}
//刷新
public void showMe(){
this.showDays();
this.showMonth();
this.showYear();
this.showDate();
}
}
public class TestJCalendar {
public static void main(String[] args) {
JFrame f=new JFrame();
f.setContentPane(new JCalendar());
f.pack();
//f.setResizable(false);
f.show();
}
}
package com.regex;
import java.io.*;
import java點(diǎn)虐 .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) { //測(cè)試方法
//磨御 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,"(.* */*/)")){ //判斷*/結(jié)尾的純注釋行
start=false;
return 1;
}
if(regCheck(s1,"(.* */*/.*)")!strCheck(s1)){ //判斷不是*/結(jié)尾的非純注釋行
if(strCheck(s1)){
start=false;
return 2;
}
}
if(start==true) //狀態(tài)代碼,start即/*開始時(shí)start=true*/結(jié)束時(shí)為false
return 1;
return 3;//ssssllll
}//aeee
/**
*
* @param s
* @param re
* @return boolean
*/
public boolean regCheck(String s,String re){ //正則表達(dá)試判斷方法
return Pattern.matches(re,s);
}
public boolean strCheck(String s){ //中間有*/的字符判斷 此方法最關(guān)鍵
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){ //此方法為我前面寫的字符串出現(xiàn)次數(shù)統(tǒng)計(jì)方法,不懂的可以看我前面的文章
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+"\"沒有出現(xiàn)過(guò)");
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(); //實(shí)例化一個(gè)文本域
//設(shè)置兩個(gè)按鈕
JButton b1 = new JButton("開始");
JButton b2 = new JButton("停止");
boolean isGo = false;
//構(gòu)造函數(shù)
public GoodLucky(){
b1.setActionCommand("start");//在開始按鈕上設(shè)置一個(gè)動(dòng)作監(jiān)聽 start
JPanel p = new JPanel(); //實(shí)例化一個(gè)可視化容器
//將兩個(gè)按鈕添加到可視化容器上面,用add方法
p.add(b1);
p.add(b2);
//在兩個(gè)按鈕上增加監(jiān)聽的屬性,自動(dòng)調(diào)用下面的監(jiān)聽處理方法actionPerformed(ActionEvent e),如果要代碼有更好的可讀性,可用內(nèi)部類實(shí)現(xiàn)動(dòng)作
//監(jiān)聽處理。
b1.addActionListener(this);
b2.addActionListener(this);
//將停止按鈕設(shè)置為不可編輯(即不可按的狀態(tài))
b2.setEnabled(false);
this.getContentPane().add(tf,"North"); //將上面的文本域放在面板的北方,也就是上面(上北下南左西右東)
this.getContentPane().add(p,"South"); //將可視化容器pannel放在南邊,也就是下面
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置用戶在此窗體上發(fā)起 "close" 時(shí)默認(rèn)執(zhí)行的操作,參數(shù)EXIT_ON_CLOSE是使用 System exit 方法退出應(yīng)用程序。僅在應(yīng)用程序中使用
this.setSize(300,200); //設(shè)置面板大小,寬和高
this.setLocation(300,300); //設(shè)置面板剛開始的出現(xiàn)的位置
Cursor cu = new Cursor(Cursor.HAND_CURSOR); //用指定名稱創(chuàng)建一個(gè)新的定制光標(biāo)對(duì)象,參數(shù)表示手狀光標(biāo)類型
this.setCursor(cu); //為指定的光標(biāo)設(shè)置光標(biāo)圖像,即設(shè)置光標(biāo)圖像為上面所創(chuàng)建的手狀光標(biāo)類型
this.setVisible(true); //將面板可視化設(shè)置為true,即可視,如果為false,即程序運(yùn)行時(shí)面板會(huì)隱藏
tf.setText("welcome you! "); //設(shè)置面板的標(biāo)題為歡迎
this.go(); //調(diào)用go方法
}
public void go(){
while(true){ //這里是死循環(huán),也就是說(shuō)用戶不點(diǎn)擊停止按鈕的話他一直循環(huán)出現(xiàn)隨機(jī)數(shù),直到用戶點(diǎn)擊停止按鈕循環(huán)才能推出,具體流程在actionPerformed方法中控制。
if(isGo == true){ //上面所定義的isGo的初始值為false,所以程序第一次到此會(huì)跳過(guò)
String s = ""; //設(shè)置空字符串
for(int j = 1; j = 7;j++){ //產(chǎn)生7個(gè)隨機(jī)數(shù)
int i = (int)(Math.random() * 36) + 1;//每個(gè)隨機(jī)數(shù)產(chǎn)生方式,這里定義靈活,可以自由定義隨機(jī)數(shù)產(chǎn)生的方式
if(i 10){
s = s + " 0" + i; //如果產(chǎn)生的隨機(jī)數(shù)小于10的話做處理:這里就牽扯到一個(gè)重要的概念,簡(jiǎn)單敘述一下:
/*
當(dāng)一個(gè)字符串與一個(gè)整型數(shù)項(xiàng)相加的意思是連接,上面的s = s + " 0" + i的意思是字符串s鏈接0再連接整型i值,而不會(huì)導(dǎo)致0和整型的i相加,
產(chǎn)生的效果為s0i,由于s為空字符串(上面定義過(guò)的),所以當(dāng)i小于零時(shí),在個(gè)位數(shù)前面加上0,比如產(chǎn)生的隨機(jī)數(shù)i為7的話,顯示效果為 07.
*/
}else{
s = s + " " + i; //如果產(chǎn)生的隨機(jī)數(shù)比10打的話,那么加上空格顯示,即數(shù)字和數(shù)字之間有個(gè)空格
}
//以上循環(huán)循環(huán)七次,以保證能出現(xiàn)7個(gè)隨機(jī)數(shù)
}
tf.setText(s); //將產(chǎn)生的隨機(jī)數(shù)全部顯示在文本域上,用文本域?qū)ο髏f調(diào)用它的設(shè)置文本的方法setText(String)實(shí)現(xiàn)。
}
//以下為線程延遲
try{
Thread.sleep(10); //線程類同步方法sleep,睡眠方法,括號(hào)里的單位為ms。
}catch(java.lang.InterruptedException e){
e.printStackTrace(); //異常捕獲,不用多說(shuō)。
}
}
}
//以下是上面設(shè)置的事件監(jiān)聽的具體處理辦法,即監(jiān)聽時(shí)間處理方法,自動(dòng)調(diào)用
public void actionPerformed(ActionEvent e){ //傳入一個(gè)動(dòng)作事件的參數(shù)e
String s = e.getActionCommand(); //設(shè)置字符串s來(lái)存儲(chǔ)獲得動(dòng)作監(jiān)聽,上面的start
/*
以下這個(gè)條件語(yǔ)句塊的作用為:用戶點(diǎn)擊開始后(捕獲start,用方法getActionCommand()),將命令觸發(fā)設(shè)置為true,從而執(zhí)行上面的go方法中的循環(huán)體(因?yàn)檠h(huán)體中要求isGo參數(shù)為true,而初始為false)。
執(zhí)行循環(huán)快產(chǎn)生隨機(jī)數(shù),并將開始按鈕不可編輯化,而用戶只可以使用停止按鈕去停止。如果用戶按下停止時(shí),也就是沒有傳入?yún)?shù)“start”的時(shí)候,
執(zhí)行else語(yǔ)句塊中的語(yǔ)句,isGo設(shè)置為false,將不執(zhí)行上面go中的循環(huán)語(yǔ)句塊,從而停止產(chǎn)生隨機(jī)數(shù),并顯示,并且把開始按鈕設(shè)置為可用,而把
停止按鈕設(shè)置為不可用,等待用戶按下開始再去開始新一輪循環(huán)產(chǎn)生隨機(jī)數(shù)。
*/
if(s.equals("start")){ //如果捕獲到start,也就是用戶觸發(fā)了動(dòng)作監(jiān)聽器,那么下面處理
isGo = true; //設(shè)置isGo為true
b1.setEnabled(false); //將開始按鈕設(shè)置為不可用
b2.setEnabled(true); //將停止按鈕設(shè)置為可用
}else{
isGo = false; //將isGo設(shè)置為false,isGo為循環(huán)標(biāo)志位
b2.setEnabled(false); //設(shè)置停止按鈕為不可用(注意看是b2,b2是停止按鈕)
b1.setEnabled(true); //設(shè)置開始按鈕為可用
}
}
public static void main(String[] args){
new GoodLucky(); //產(chǎn)生類的實(shí)例,執(zhí)行方法
}
}
網(wǎng)站欄目:日歷java源代碼 日歷java程序
網(wǎng)址分享:http://www.chinadenli.net/article34/dccgise.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站導(dǎo)航、品牌網(wǎng)站制作、網(wǎng)站排名、商城網(wǎng)站、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)