import java.awt.*;
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、寬城ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的寬城網(wǎng)站制作公司
import java.awt.event.*;
import javax.swing.*;
class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11];
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i 11;i++)
{
for(int j = 0;j 11;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(Color.BLUE);
setBounds(0, 0, 360, 360);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
if(x 25 || x 330 + 25 ||y 25 || y 330+25)
{
return;
}
if(chess[x/30-1][y/30-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = 30;i = 330;i += 30)
{
for(int j = 30;j = 330; j+= 30)
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, 330);
}
}
for(int j = 30;j = 330;j += 30)
{
g.setColor(Color.WHITE);
g.drawLine(30, j, 330, j);
}
}
void Drawchess(Graphics g)
{
for(int i = 0;i 11;i++)
{
for(int j = 0;j 11;j++)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;
for(i = 0;i 11;i++)//橫向判斷
{
for(int j = 0;j 11;j++)
{
if(chess[i][j] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i][j] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i 11;i++)//豎向判斷
{
for(int j = 0;j 11;j++)
{
if(chess[j][i] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[j][i] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i 7;i++)//左向右斜判斷
{
for(int j = 0;j 7;j++)
{
for(int k = 0;k 5;k++)
{
if(chess[i + k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i + k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
for(i = 4;i 11;i++)//右向左斜判斷
{
for(int j = 6;j = 0;j--)
{
for(int k = 0;k 5;k++)
{
if(chess[i - k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i - k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
}
void Clear_Chess()
{
for(int i=0;i11;i++)
{
for(int j=0;j11;j++)
{
chess[i][j]=0;
}
}
repaint();
}
public void paint(Graphics g)
{
Drawline(g);
Drawchess(g);
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}
class myframe extends Frame implements WindowListener
{
mypanel panel;
myframe()
{
setLayout(null);
panel = new mypanel();
add(panel);
panel.setBounds(0,23, 360, 360);
setTitle("單人版五子棋");
setBounds(200, 200, 360, 383);
setVisible(true);
addWindowListener(this);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc [])
{
myframe f = new myframe();
}
}
Java-時(shí)鐘萬年歷
clock.java代碼如下:
/**
* Clock.java
* Summary 數(shù)字時(shí)間顯示
* Created on 2005-8-14
* @author 高?
* remark 如有改動(dòng)請(qǐng)發(fā)一份代碼給我,郵箱gkgklovelove@eyou.com
*/
package Clock;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
class Clock extends Canvas implements Runnable{
MainFrame mf;
Thread t;
String time;
Clock(MainFrame mf){
this.mf=mf;
setSize(400,40);
setBackground(Color.white);
t=new Thread(this); //實(shí)例化線程
t.start(); //調(diào)用線程
}
public void run(){
while(true){
try{
t.sleep(1000); //休眠1秒鐘
}catch(InterruptedException e){
System.out.println("異常");
}
this.repaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋體",Font.BOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化時(shí)間顯示類型
Calendar now=Calendar.getInstance();
time=SDF.format(now.getTime()); //得到當(dāng)前日期和時(shí)間
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,100,25);
}
MainFrame.java 的代碼如下:
/**
* MainFrame.java
* Summary 萬年歷主類
* Created on 2005-8-14
* @author 高?
* remark 如有改動(dòng)請(qǐng)發(fā)一份代碼給我,郵箱gkgklovelove@eyou.com
*/
package Clock;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
class MainFrame extends JFrame{
JPanel panel=new JPanel(new BorderLayout());
JPanel panel1=new JPanel();
JPanel panel2=new JPanel(new GridLayout(7,7));
JPanel panel3=new JPanel();
JLabel []label=new JLabel[49];
JLabel y_label=new JLabel("年份");
JLabel m_label=new JLabel("月份");
JComboBox com1=new JComboBox();
JComboBox com2=new JComboBox();
JButton button=new JButton("查看");
int re_year,re_month;
int x_size,y_size;
String year_num;
Calendar now=Calendar.getInstance(); //實(shí)例化Calendar
MainFrame(){
super("萬年歷");
setSize(300,350);
x_size=(int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y_size=(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x_size-300)/2,(y_size-350)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(y_label);
panel1.add(com1);
panel1.add(m_label);
panel1.add(com2);
panel1.add(button);
for(int i=0;i49;i++){
label[i]=new JLabel("",JLabel.CENTER);//將顯示的字符設(shè)置為居中
panel2.add(label[i]);
}
panel3.add(new Clock(this));
panel.add(panel1,BorderLayout.NORTH);
panel.add(panel2,BorderLayout.CENTER);
panel.add(panel3,BorderLayout.SOUTH);
panel.setBackground(Color.white);
panel1.setBackground(Color.white);
panel2.setBackground(Color.white);
panel3.setBackground(Color.white);
Init();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int c_year,c_month,c_week;
c_year=Integer.parseInt(com1.getSelectedItem().toString()); //得到當(dāng)前所選年份
c_month=Integer.parseInt(com2.getSelectedItem().toString())-1; //得到當(dāng)前月份,并減1,計(jì)算機(jī)中的月為0-11
c_week=use(c_year,c_month); //調(diào)用函數(shù)use,得到星期幾
Resetday(c_week,c_year,c_month); //調(diào)用函數(shù)Resetday
}});
setContentPane(panel);
setVisible(true);
setResizable(false);
}
public void Init(){
int year,month_num,first_day_num;
String log[]={"日","一","二","三","四","五","六"};
for(int i=0;i7;i++){
label[i].setText(log[i]);
}
for(int i=0;i49;i=i+7){
label[i].setForeground(Color.red); //將星期日的日期設(shè)置為紅色
}
for(int i=6;i49;i=i+7){
label[i].setForeground(Color.green);//將星期六的日期設(shè)置為綠色
}
for(int i=1;i10000;i++){
com1.addItem(""+i);
}
for(int i=1;i13;i++){
com2.addItem(""+i);
}
month_num=(int)(now.get(Calendar.MONTH)); //得到當(dāng)前時(shí)間的月份
year=(int)(now.get(Calendar.YEAR)); //得到當(dāng)前時(shí)間的年份
com1.setSelectedIndex(year-1); //設(shè)置下拉列表顯示為當(dāng)前年
com2.setSelectedIndex(month_num); //設(shè)置下拉列表顯示為當(dāng)前月
first_day_num=use(year,month_num);
Resetday(first_day_num,year,month_num);
}
public int use(int reyear,int remonth){
int week_num;
now.set(reyear,remonth,1); //設(shè)置時(shí)間為所要查詢的年月的第一天
week_num= (int)(now.get(Calendar.DAY_OF_WEEK));//得到第一天的星期
return week_num;
}
public void Resetday(int week_log,int year_log,int month_log){
int month_score_log; //判斷是否是閏年的標(biāo)記
int month_day_score; //存儲(chǔ)月份的天數(shù)
int count;
month_score_log=0;
month_day_score=0;
count=1;
if(year_log%4==0year_log%100!=0||year_log%400==0){//判斷是否為閏年
month_score_log=1;
}
month_log=month_log+1; //將傳來的月份數(shù)加1
switch(month_log){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
month_day_score=31;
break;
case 4:
case 6:
case 9:
case 11:
month_day_score=30;
break;
case 2:
if(month_score_log==1){
month_day_score=29;
}
else{
month_day_score=28;
}
break;
}
for(int i=7;i49;i++){ //初始化標(biāo)簽
label[i].setText("");
}
week_log=week_log+6; //將星期數(shù)加6,使顯示正確
month_day_score=month_day_score+week_log;
for(int i=week_log;imonth_day_score;i++,count++){
label[i].setText(count+"");
}
}
public static void main(String [] args){
JFrame.setDefaultLookAndFeelDecorated(true);
MainFrame start=new MainFrame();
}
}
Java生成CSV文件簡(jiǎn)單操作實(shí)例
CSV是逗號(hào)分隔文件(Comma Separated Values)的首字母英文縮寫,是一種用來存儲(chǔ)數(shù)據(jù)的純文本格式,通常用于電子表格或數(shù)據(jù)庫(kù)軟件。在 CSV文件中,數(shù)據(jù)“欄”以逗號(hào)分隔,可允許程序通過讀取文件為數(shù)據(jù)重新創(chuàng)建正確的欄結(jié)構(gòu),并在每次遇到逗號(hào)時(shí)開始新的一欄。如:
123? ?1,張三,男2,李四,男3,小紅,女? ?
Java生成CSV文件(創(chuàng)建與導(dǎo)出封裝類)
package com.yph.omp.common.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
/**
* Java生成CSV文件
*/
public class CSVUtil {
/**
* 生成為CVS文件
*
* @param exportData
*? ? ? ? ? ? 源數(shù)據(jù)List
* @param map
*? ? ? ? ? ? csv文件的列表頭map
* @param outPutPath
*? ? ? ? ? ? 文件路徑
* @param fileName
*? ? ? ? ? ? 文件名稱
* @return
*/
@SuppressWarnings("rawtypes")
public static File createCSVFile(List exportData, LinkedHashMap map,
String outPutPath, String fileName) {
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!file.exists()) {
file.mkdir();
}
// 定義文件名格式并創(chuàng)建
csvFile = File.createTempFile(fileName, ".csv",
new File(outPutPath));
// UTF-8使正確讀取分隔符","
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(csvFile), "GBK"), 1024);
// 寫入文件頭部
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
csvFileOutputStream
.write("\"" + (String) propertyEntry.getValue() != null ? (String) propertyEntry
.getValue() : "" + "\"");
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
csvFileOutputStream.newLine();
// 寫入文件內(nèi)容
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
Object row = (Object) iterator.next();
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
/*-------------------------------*/
//以下部分根據(jù)不同業(yè)務(wù)做出相應(yīng)的更改
StringBuilder sbContext = new StringBuilder("");
if (null != BeanUtils.getProperty(row,(String) propertyEntry.getKey())) {
if("證件號(hào)碼".equals(propertyEntry.getValue())){
//避免:身份證號(hào)碼 ,讀取時(shí)變換為科學(xué)記數(shù) - 解決辦法:加 \t(用Excel打開時(shí),證件號(hào)碼超過15位后會(huì)自動(dòng)默認(rèn)科學(xué)記數(shù))
sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey()) + "\t");
}else{
sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey()));? ? ? ? ? ? ? ? ? ? ? ? ?
}
}
csvFileOutputStream.write(sbContext.toString());
/*-------------------------------*/? ? ? ? ? ? ? ? ?
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
if (iterator.hasNext()) {
csvFileOutputStream.newLine();
}
}
csvFileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 下載文件
*
* @param response
* @param csvFilePath
*? ? ? ? ? ? 文件路徑
* @param fileName
*? ? ? ? ? ? 文件名稱
* @throws IOException
*/
public static void exportFile(HttpServletRequest request,
HttpServletResponse response, String csvFilePath, String fileName)
throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/csv;charset=GBK");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("GB2312"), "ISO8859-1"));
InputStream in = null;
try {
in = new FileInputStream(csvFilePath);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) 0) {
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e1) {
System.out.println(e1);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
}
/**
* 刪除該目錄filePath下的所有文件
*
* @param filePath
*? ? ? ? ? ? 文件目錄路徑
*/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i files.length; i++) {
if (files[i].isFile()) {
files[i].delete();
}
}
}
}
/**
* 刪除單個(gè)文件
*
* @param filePath
*? ? ? ? ? ? 文件目錄路徑
* @param fileName
*? ? ? ? ? ? 文件名稱
*/
public static void deleteFile(String filePath, String fileName) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i files.length; i++) {
if (files[i].isFile()) {
if (files[i].getName().equals(fileName)) {
files[i].delete();
return;
}
}
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void createFileTest() {
List exportData = new ArrayListMap();
Map row1 = new LinkedHashMapString, String();
row1.put("1", "11");
row1.put("2", "12");
row1.put("3", "13");
row1.put("4", "14");
exportData.add(row1);
row1 = new LinkedHashMapString, String();
row1.put("1", "21");
row1.put("2", "22");
row1.put("3", "23");
row1.put("4", "24");
exportData.add(row1);
LinkedHashMap map = new LinkedHashMap();
map.put("1", "第一列");
map.put("2", "第二列");
map.put("3", "第三列");
map.put("4", "第四列");
String path = "d:/export";
String fileName = "文件導(dǎo)出";
File file = CSVUtil.createCSVFile(exportData, map, path, fileName);
String fileNameNew = file.getName();
String pathNew = file.getPath();
System.out.println("文件名稱:" + fileNameNew );
System.out.println("文件路徑:" + pathNew );
}
}
//注:BeanUtils.getProperty(row,(String) propertyEntry.getKey()) + "\t" ,只為解決數(shù)字格式超過15位后,在Excel中打開展示科學(xué)記數(shù)問題。
public?class?test?{
public?static?void?main(String[]?args)?{
System.out.println("666");
…System.out.println("666");復(fù)制494遍…
System.out.println("666");
}
}
正好500行
當(dāng)前名稱:關(guān)于java簡(jiǎn)單代碼500行的信息
本文鏈接:http://aaarwkj.com/article24/doodjje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、網(wǎng)站設(shè)計(jì)公司、面包屑導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、用戶體驗(yàn)
聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)