java小项目代码 java项目代码大全

谁给个java 小项目做做!B/S 还是C/S的?
先给你个小的看看基础 能做出来再给你个难点的
十二、计算星期
要求:编写一个Applet小程序,使用Swing组件设计界面,通过下拉组合框选择日期的不同部分,计算设定日期是星期几,初始日期为系统当前日期
功能:
1、 编写一个Applet,使用Swing组件,继承Japplet类
2、 在Applet顶部显示标签和下拉组合框,分别选择年、月、日,年的范围从1990年到2050年
3、 用一个“计算星期”按钮,点击按钮计算星期,并在下方的标签上显信息:××××年××月××日是星期×
4、 创建一个HTML文档显示Applet
实现步骤:
1、 创建Applet
1) 继承javax.swing.JApplet
2) 定义组件和变量,如标签、组合框,保存日期的变量等 。日期变量使用java.util.Calendar类
3) 在初始化方法中 , 初始化和布局组件:
用合适的容器和布局构造图示的界面,使用JcomboBox构造下拉组合框
读取系统当前日期,创建包含当前日期的Calendar类变量 。参考代码是:
calendar=Calendar.getInstance();
初始化年、月、日下拉组合框的值,参考代码是:
for(int k=1990;k=2050;k)
cbxYear.addItem(new Integer(k));
将当前年月日设置为下拉框的当前值,如设置年下拉框的代码是:
cbxYear.setSelectedItem(new Integer(calendar.get(Calendar.YEAR)));
提示:
JcomboBox方法
setSelectedItem(Object anObject) 设置要选中的项
setSelectedIdex(int anIndex) 通过索引设置要选中的项,索引从0开始
Object getSelectedItem() 获得选中的项
intgetSelectedIndex() 获得选中的索引位置
Calendar类的用法
intget(intfield)读取日期不同域的值
void set(intfield,int value) 设置日期不同域的值
域可以是以下值:
Calendar.YEAR 年份
Calendar.MONTH 月份,月份从0开始,即0表示1月,11表示12月 。
Calendar.DAY_OF_MONTH 日
Calendar.DAY_OF_WEEK 星期,注意从星期日到星期六对应整数是1到7
编写事件处理程序
从下拉框中读取年、月、日的值,并设置Calendar变量的对应域
跪求java小项目,经典小游戏代码import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class GreedSnake implements KeyListener{
JFrame mainFrame;
Canvas paintCanvas;
JLabel labelScore;
SnakeModel snakeModel = null;
public static final int canvasWidth = 200;
public static final int canvasHeight = 300;
public static final int nodeWidth = 10;
public static final int nodeHeight = 10;
public GreedSnake() {
mainFrame = new JFrame("GreedSnake");
Container cp = mainFrame.getContentPane();
labelScore = new JLabel("Score:");
cp.add(labelScore, BorderLayout.NORTH);
paintCanvas = new Canvas();
paintCanvas.setSize(canvasWidth 1,canvasHeight 1);
paintCanvas.addKeyListener(this);
cp.add(paintCanvas, BorderLayout.CENTER);
JPanel panelButtom = new JPanel();
panelButtom.setLayout(new BorderLayout());
JLabel labelHelp;
labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.NORTH);
labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.CENTER);
labelHelp = new JLabel("SPACE or P for pause",JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.SOUTH);
cp.add(panelButtom,BorderLayout.SOUTH);
mainFrame.addKeyListener(this);
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
begin();
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if (snakeModel.running)
switch(keyCode){
case KeyEvent.VK_UP:
snakeModel.changeDirection(SnakeModel.UP);
break;
case KeyEvent.VK_DOWN:
snakeModel.changeDirection(SnakeModel.DOWN);
break;
case KeyEvent.VK_LEFT:
snakeModel.changeDirection(SnakeModel.LEFT);
break;
case KeyEvent.VK_RIGHT:
snakeModel.changeDirection(SnakeModel.RIGHT);
break;
case KeyEvent.VK_ADD:
case KeyEvent.VK_PAGE_UP:
snakeModel.speedUp();
break;
case KeyEvent.VK_SUBTRACT:
case KeyEvent.VK_PAGE_DOWN:
snakeModel.speedDown();
break;
case KeyEvent.VK_SPACE:
case KeyEvent.VK_P:
snakeModel.changePauseState();
break;
default:
}
if (keyCode == KeyEvent.VK_R ||
keyCode == KeyEvent.VK_S ||
keyCode == KeyEvent.VK_ENTER){
snakeModel.running = false;
begin();
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
void repaint(){
Graphics g = paintCanvas.getGraphics();
//draw background
g.setColor(Color.WHITE);
g.fillRect(0,0,canvasWidth,canvasHeight);
// draw the snake
g.setColor(Color.BLACK);
LinkedList na = snakeModel.nodeArray;
Iterator it = na.iterator();
while(it.hasNext()){
Node n = (Node)it.next();
drawNode(g,n);
}
// draw the food
g.setColor(Color.RED);
Node n = snakeModel.food;
drawNode(g,n);
updateScore();
}
private void drawNode(Graphics g, Node n){
g.fillRect(n.x*nodeWidth,
n.y*nodeHeight,
nodeWidth-1,
nodeHeight-1);
}
public void updateScore(){
String s = "Score: "snakeModel.score;
labelScore.setText(s);
}
void begin(){
if (snakeModel == null || !snakeModel.running){
snakeModel = new SnakeModel(this,
canvasWidth/nodeWidth,
canvasHeight/nodeHeight);
(new Thread(snakeModel)).start();
}
}
public static void main(String[] args){
GreedSnake gs = new GreedSnake();
}
}
///////////////////////////////////////////////////
// 文件2
///////////////////////////////////////////////////
import java.util.*;
import javax.swing.*;
class SnakeModel implements Runnable{
GreedSnake gs;
boolean[][] matrix;
LinkedList nodeArray = new LinkedList();
Node food;
int maxX;
int maxY;
int direction = 2;
boolean running = false;
int timeInterval = 200;
double speedChangeRate = 0.75;
boolean paused = false;
int score = 0;
int countMove = 0;
// UP and DOWN should be even
// RIGHT and LEFT should be odd
public static final int UP = 2;
public static final int DOWN = 4;
public static final int LEFT = 1;
public static final int RIGHT = 3;
public SnakeModel(GreedSnake gs, int maxX, int maxY){
this.gs = gs;
this.maxX = maxX;
this.maxY = maxY;
// initial matirx
matrix = new boolean[maxX][];
for(int i=0; imaxX;i){
matrix = new boolean[maxY];
Arrays.fill(matrix,false);
}
// initial the snake
int initArrayLength = maxX20 ? 10 : maxX/2;
for(int i = 0; iinitArrayLength;i){
int x = maxX/2 i;
int y = maxY/2;
nodeArray.addLast(new Node(x, y));
matrix[x][y] = true;
}
food = createFood();
matrix[food.x][food.y] = true;
}
public void changeDirection(int newDirection){
if (direction % 2 != newDirection % 2){
direction = newDirection;
}
}
public boolean moveOn(){
Node n = (Node)nodeArray.getFirst();
int x = n.x;
int y = n.y;
switch(direction){
case UP:
y--;
break;
case DOWN:
y;
break;
case LEFT:
x--;
break;
case RIGHT:
x;
break;
}
if ((0 = xxmaxX)(0 = yymaxY)){
if (matrix[x][y]){
if(x == food.xy == food.y){
nodeArray.addFirst(food);
int scoreGet = (10000 - 200 * countMove) / timeInterval;
score= scoreGet0? scoreGet : 10;
countMove = 0;
food = createFood();
matrix[food.x][food.y] = true;
return true;
}
else
return false;
}
else{
nodeArray.addFirst(new Node(x,y));
matrix[x][y] = true;
n = (Node)nodeArray.removeLast();
matrix[n.x][n.y] = false;
countMove;
return true;
}
}
求几个JAVA小项目源代码 , 供自己学习参考package com.test01;
import java.util.Scanner;
public class oop5 { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// int x = 9;
// int y = 1;
int x = sc.nextInt();
int y = sc.nextInt();
int z;
z = add(x, y);
System.out.println("xjava小项目代码的值为java小项目代码:"x);
System.out.println("yjava小项目代码的值为java小项目代码:"y);
System.out.println("二者之和为:"z);
}
/** 四种小算法 */
// 加法运算
public static int add(int a, int b) {
int c;
c = ab;
return c;
}
// 减法运算
public static int jian(int d, int v) {
int m;
m = d - v;
return m;
}
// 乘积运算
public static int addAdd(int q, int w) {
int e;
e = q * w;
return e;
}
// 除法运算
public static int chu(int p, int k) {
int f;
f = p / k;
return f;
}
}
求JAVA小项目的完整代码给你个做好了的Java的源程序的记事本,自己看看就行了的 , 不怎么难的···
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class MyNotepad implements ActionListener{
private JFrame frame=new JFrame("新记事本");
private JTextArea jta=new JTextArea();
private String result="";
private boolean flag=true;
private File f;
private JButton jb=new JButton("开始");
private JTextField jtf=new JTextField(15);
private JTextField jt=new JTextField(15);
private JButton jbt=new JButton("替换为");
private JButton jba=new JButton("全部替换");
private Icon ic=new ImageIcon("D:\\java课堂笔记\\GUI\\11.gif");
private String value;
private int start=0;
private JFrame jf=new JFrame("查找");
private JFrame jfc=new JFrame("替换");
@Override
public void actionPerformed(ActionEvent e) {
String comm=e.getActionCommand();
if("新建".equals(comm)){
if(!(frame.getTitle().equals("新记事本"))){
if(!flag){
write();
newNew();
}else{
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}
}else if(!(jta.getText().isEmpty())){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
newNew();
}
}else{
newNew();
}
}else if("打开".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
frame.setTitle(f.getName());
result=read();
flag=false;
value=https://www.04ip.com/post/result;
jta.setText(result);
}
}else if("保存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
if(flag){
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}else{
write();
}
}else if("另存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"另存");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
write();
}
}else if("退出".equals(comm)){
System.exit(0);
}else if("撤销".equals(comm)){
jta.setText(value);
}else if("剪切".equals(comm)){
value=https://www.04ip.com/post/jta.getText();
jta.cut();
}else if("复制".equals(comm)){
jta.copy();
}else if("粘贴".equals(comm)){
value=https://www.04ip.com/post/jta.getText();
jta.paste();
}else if("删除".equals(comm)){
value=https://www.04ip.com/post/jta.getText();
jta.replaceSelection(null);
}else if("全选".equals(comm)){
jta.selectAll();
}else if("查找".equals(comm)){
value=https://www.04ip.com/post/jta.getText();
jf.add(jtf,BorderLayout.CENTER);
jf.add(jb,BorderLayout.SOUTH);
jf.setLocation(300,300);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("替换".equals(comm)){
value=https://www.04ip.com/post/jta.getText();
GridLayout gl=new GridLayout(3,3);
JLabel jl1=new JLabel("查找内容:");
JLabel jl2=new JLabel("替换为:");
jfc.setLayout(gl);
jfc.add(jl1);
jfc.add(jtf);
jfc.add(jb);
jfc.add(jl2);
jfc.add(jt);
jfc.add(jbt);
JLabel jl3=new JLabel();
JLabel jl4=new JLabel();
jfc.add(jl3);
jfc.add(jl4);
jfc.add(jba);
jfc.setLocation(300,300);
jfc.pack();
jfc.setVisible(true);
jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("版本".equals(comm)){
JDialog jd=new JDialog(frame,"关于对话框");
jd.setSize(200,200);
JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");
jd.add(l,BorderLayout.CENTER);
jd.setLocation(100,200);
jd.setSize(300,300);
jd.setVisible(true);
//jd.pack();
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}else if("开始".equals(comm)||"下一个".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s 1;
jb.setText("下一个");
//value=https://www.04ip.com/post/value.substring(s temp.length());//不能截取字串
}else {
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}else if("替换为".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s 1;
jta.replaceSelection(jt.getText());
}else {
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}else if("全部替换".equals(comm)){
String temp=jta.getText();
temp=temp.replaceAll(jtf.getText(), jt.getText());
jta.setText(temp);
}
}
public String read(){
String temp="";
try {
FileInputStream fis = new FileInputStream(f.getAbsolutePath());
byte[] b=new byte[1024];
while(true){
int num=fis.read(b);
if(num==-1)break;
temp=temp new String(b,0,num);
}
fis.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return temp;
}
public void write(){
try {
FileOutputStream fos=new FileOutputStream(f);
fos.write(jta.getText().getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void newNew(){
frame.dispose();
new MyNotepad();
flag=true;
}
public MyNotepad(){
JMenuBar jmb=new JMenuBar();
String[] menuLab={"文件","编辑","帮助"};
String[][] menuItemLab={{"新建","打开","保存","另存","退出"},
{"撤销","剪切","复制","粘贴","删除","全选","查找","替换"},
{"版本"}};
for(int i=0;imenuLab.length;i){
JMenu menu=new JMenu(menuLab[i]);
jmb.add(menu);
for(int j=0;jmenuItemLab[i].length;j){
JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);
menu.add(jmi);
jmi.addActionListener(this);
}
}
frame.setJMenuBar(jmb);
jta.setLineWrap(true);//自动换行
JScrollPane jsp=new JScrollPane(jta);//滚动窗口面板
frame.add(jsp);
jb.addActionListener(this);
jbt.addActionListener(this);
jba.addActionListener(this);
frame.setLocation(200,50);
frame.setSize(620,660);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyNotepad();
}
}
【java小项目代码 java项目代码大全】java小项目代码的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于java项目代码大全、java小项目代码的信息别忘了在本站进行查找喔 。

    推荐阅读