Java课程设计作品参考

/*
* 课程设计作品jisuanqi(简单计算器)
* 注意:为防止意外,建议在每次计算前先清零。
*/

import java.awt.*;
import java.awt.event.*;

public class jisuanqi extends WindowAdapter { //继承窗口适配器类
Panel p1 = new Panel(); //创建画布组件P1,P2,P3(属容器组件--小容器)
Panel p2 = new Panel();
Panel p3 = new Panel();
TextField txt; //定义txt为文本框
private Button[] b = new Button[17]; //创建按钮数组,下面是对应的按钮名字
private String ss[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "归零", "0", "=", "/", "关闭" };
static double a; //定义a为计算结果变量
static String s, str; //定义s,str字符串变量
public static void main(String args[]) { //主程序
(new jisuanqi()).frame(); //计算器窗体
}
public void frame() { //构造窗体(属容器组件--大容器)
Frame fm = new Frame("简单计算器"); //创建窗体实例,标题是"简单计算器"
for (int i = 0; i <= 16; i++) {
b[i] = new Button(ss[i]); //创建17个按钮
}
for (int i = 0; i <= 15; i++) { //把前16个按钮元素加入画布P2中
p2.add(b[i]);
}
b[16].setBackground(Color.yellow); //为"关闭"按钮设置背景颜色
txt = new TextField(15); //创建文本框长15字符
txt.setEditable(false); //设置文本不可编辑,只能用按钮输入,不能用键盘输入。
for (int i = 0; i <= 16; i++) {
b[i].addActionListener(new buttonlistener());//对17个按钮创建监听器
}
b[16].addActionListener(new close()); //为关闭按钮创建“关闭类”监听器
fm.addWindowListener(this); //为“窗体”本身加入监听器
fm.setBackground(Color.red); //为“窗体”设置背景颜色(红色)
p1.setLayout(new BorderLayout()); //创建P1画布为边框布局(即东南西北中布局)
p1.add(txt, "North"); //在P1上加入文本框txt (画布组件属容器类)
p2.setLayout(new GridLayout(4, 4)); //创建P2画布为网格布局4行4列
p3.setLayout(new BorderLayout()); //创建P3画布为边框布局
p3.add(b[16]); //在P3上添加关闭按钮
fm.add(p1, "North"); //在窗体fm上部加入P1画布(窗体默认边框布局)
fm.add(p2, "Center"); //在窗体fm中部加入P2画布(窗体组件属容器类)
fm.add(p3, "South"); //在窗体fm下部加入P3画布
fm.pack(); //紧缩窗体上的所有组件
fm.setVisible(true); //设置窗体上的组件为可视
}
public void windowClosing(WindowEvent e) { //似乎没有用上
System.exit(0); //退出系统
}
class buttonlistener implements ActionListener {//按钮监听器类
//编写监听器事件,通过按键得出结果.
public void actionPerformed(ActionEvent e) {//执行操作(当按钮事件发生时)
Button btn = (Button) e.getSource

(); //定义按钮事件源(btn)
if (btn.getLabel() == "=") { //"="按钮
jisuan(); //计算结果
str = String.valueOf(a); //把结果a转换为字符串
txt.setText(str); //并放入文本框中
s = ""; //记录运算符号
} else if (btn.getLabel() == "+") {
jisuan();
txt.setText("");
s = "+";
} else if (btn.getLabel() == "-") {
jisuan();
txt.setText("");
s = "-";
} else if (btn.getLabel() == "/") {
jisuan();
txt.setText("");
s = "/";
} else if (btn.getLabel() == "*") {
jisuan();
txt.setText("");
s = "*";
} else { //按数字或"归零"按钮
txt.setText(txt.getText()+btn.getLabel()); //按数字按钮时,显示输入值。
if (btn.getLabel() == "归零") //按"归零"按钮时,
txt.setText(""); //清空文本框。
}
}
public void jisuan() { //计算方法
if (s == "+")
a += Double.parseDouble(txt.getText());
else if (s == "-")
a -= Double.parseDouble(txt.getText());
else if (s == "*")
a *= Double.parseDouble(txt.getText());
else if (s == "/")
a /= Double.parseDouble(txt.getText());
else
a = Double.parseDouble(txt.getText());
}
}
}
class close implements ActionListener { //按下close按钮退出系统类
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}






/*
*课程设计作品Cooking(模拟微波炉烹调食物倒计时过程)
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Cooking extends JFrame implements ActionListener,Runnable{
JLabel l1,l2;
JTextField tf;
JPanel p1,p2;
JButton[] b = new JButton[12];
String[] s = {"7","8","9","4","5","6","1","2","3","清零","0","开始"};
int[] num = new int[4]; //储存分钟和秒钟的四个数字
Thread t; //声明一个线程
public Cooking(){ //构造函数
init();
this.setTitle ("微波炉模拟控制器");
this.pack(); //设置默认大小位置
this.setLocationRelativeTo (null); //窗口居中
this.setVisible(true);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}

//该方法用于初始化成员变量及添加各组件
public void init(){
l1 = new JLabel("烹 调");
l2 = new JLabel("指示灯");
tf = new JTextField(4);
l2.setFont (new Font(Font.SERIF,Font.BOLD,15)); //设置字体属性
tf.setFont (new Font(Font.SERIF,Font.BOLD,18));
tf.setBackground (Color.GREEN); //设置背景色
tf.setEditable (false); //设置不可编辑
tf.setHorizontalAlignment (SwingConstants.CENTER); //文本内容居中显示
tf.setBorder (BorderFactory.createLoweredBevelBorder ());//设置边框属性
p1 = new JPanel(new FlowLayout(FlowLayout.CENTER,20,5));
p2 = new JPanel(new GridLayout(4,3,5,5));
p1.add(l1);
p1.add(tf);
p1.add(l2);
for(int i=0;i<12;i++){
b[i] = new JButton(s[i]);
p2.add(b[i]);

b[i].addActionListener (this);
}
b[9].setForeground (Color.BLUE); //设置前景色
b[11].setForeground (Color.RED);
setColor(Color.GRAY); //方法调用 设置颜色
setNum(); //方法调用 设置定时器
p2.setBorder (BorderFactory.createEtchedBorder ());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
}

//实现接口接口Runnable的方法
public void run(){
for(int i=0;i<4;i++){
//因为初始化是-1,当用户没有全部重置计时器的四个数时,将-1设为0
if(num[i]==-1)num[i] = 0;
}
while(true){
try{
Thread.sleep (1000); //休眠一秒
}catch(InterruptedException ie){
ie.printStackTrace ();
}
if(num[3]!=0){ //秒钟第二个数不为"0"
num[3]--;
}else{ //为"0"
num[3] = 9; //重置为"9"
if(num[2]!=0){ //秒钟第一个数不为"0"
num[2]--;
}else{ //为"0"
num[2] = 5; //重置为"5"
if(num[1]!=0){ //分钟第二个数不为"0"
num[1]--;
}else{ //为"0"
num[1] = 9; //重置为"9"
if(num[0]!=0){ //分钟第一个数不为"0"
num[0]--;
}else{ //都为"0"时,烹调结束
setColor(Color.GRAY);
setEnabled(true);
b[11].setText ("开始");
setNum();
break;
}
}
}
}
String time = num[0]+""+num[1]+":"+num[2]+""+num[3];
tf.setText (time); //刷新定时器
System.out.println("surplus time is: "+time);
}
}

//设置组件的使用状态,true:可以使用,false:禁止使用
public void setEnabled(boolean bool){
for(int i=0;i<11;i++){
b[i].setEnabled (bool);
}
}

//设置定时器和计时器
public void setNum(){
tf.setText("00:00");
for(int i=0;i<4;i++){
num[i] = -1;
}
}

//设置组件的“指示灯”标签的边框及前景色
public void setColor(Color c){
l2.setBorder (BorderFactory.createLineBorder (c));
l2.setForeground (c);
}

//事件处理
public void actionPerformed(ActionEvent ae){
//当用户点击“开始”按钮时
if(ae.getActionCommand ().equals ("开始")){
//当所预定的时间不为“0”时才可响应“开始”按钮事件
if(!tf.getText ().equals ("00:00")){
t = new Thread(this); //创建一个线程类对象
t.start (); //启动线程 烹调开始
setColor(Color.YELLOW); //烹调开始 设置指示灯颜色为“YELLOW”黄色
setEnabled(false); //禁用按钮功能
b[11].setText ("停止"); //设置按钮文本 为"停止"
}
}//当用户点击“停止”按钮时
else if(ae.getActionCommand ().equals ("停止")){
t.stop (); //终止线程 烹调结束
setEnabled(true); //启用按钮功能
setColor(Color.GRAY); //烹调结束 设置指示灯颜色为“GRAY”灰色
setNum(); //计时清零
b[11].setText ("

开始"); //设置按钮文本 为"开始"
}//当用户点击“清零”按钮”时
else if(ae.getActionCommand ().equals ("清零")){
setNum(); //计时清零
}//当用户点击数字按钮时
else{
String s = ae.getActionCommand (); //相当于按钮上的文本内容
int i = Integer.parseInt (s); //类型转换:String--int
//num[0]~num[3]分别是分钟第一,二个数和秒钟第一,二个数
if(num[0]==-1){ //==1 表示还没有设置分钟第一个数
if(i<6){ //分钟不能超过59,所以第一个数字不能大于6
tf.setText(i+"0:00"); //比如"30:00"
num[0] = i; //存储分钟第一个数"3"
}
}else if(num[1]==-1){//同上
// 分钟第二个数从0~9没限制
tf.setText(num[0]+""+i+":00");
num[1] = i;
}else if(num[2]==-1){//...
if(i<6){
tf.setText(num[0]+""+num[1]+":"+i+"0");
num[2] = i;
}
}else if(num[3]==-1){
tf.setText(num[0]+""+num[1]+":"+num[2]+""+i);
num[3] = i;
}
}
}

//主程序
public static void main(String[] args){
new Cooking();
}
}




/*
* 课程设计作品wuziqi(五子棋游戏)
*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener
{
int color_Qizi=0; //旗子的颜色标识 0:白子 1:黑子
int intGame_Start=0; //游戏开始标志 0未开始 1游戏中
int intGame_Body[][]=new int[16][16]; //设置棋盘棋子状态 0 无子 1 白子 2 黑子
Button b1=new Button("游戏开始");
Button b2=new Button("重置游戏");
Label lblWin=new Label(" ");
Checkbox ckbHB[]=new Checkbox[2];
CheckboxGroup ckgHB=new CheckboxGroup();
public void init()
{
setLayout(null);
addMouseListener(this);
add(b1);
b1.setBounds(330,50,80,30);
b1.addActionListener(this);
add(b2);
b2.setBounds(330,90,80,30);
b2.addActionListener(this);
ckbHB[0]=new Checkbox("白子先",ckgHB,false);
ckbHB[0].setBounds(320,20,60,30);
ckbHB[1]=new Checkbox("黑子先",ckgHB,false);
ckbHB[1].setBounds(380,20,60,30);
add(ckbHB[0]);
add(ckbHB[1]);
ckbHB[0].addItemListener(this);
ckbHB[1].addItemListener(this);
add(lblWin);
lblWin.setBounds(330,130,80,30);
Game_start_csh();
}
public void itemStateChanged(ItemEvent e)
{
if (ckbHB[0].getState()) //选择黑子先还是白子先
color_Qizi=0;
else
color_Qizi=1;
}
public void actionPerformed(ActionEvent e)
{
Graphics g=getGraphics();
if (e.getSource()==b1)
Game_start();
else
Game_re();
}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
Graphics g=getGraphics();
int x1,y1;
x1=e.getX();
y1=e.getY();
if (e.getX()<20 || e.getX()>300 || e.getY()<20 || e.getY()

>300)
return;
if (x1%20>10)
x1+=20;
if(y1%20>10)
y1+=20;
x1=x1/20*20;
y1=y1/20*20;
set_Qizi(x1,y1);
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void paint(Graphics g)
{
draw_qipan(g);
}
public void set_Qizi(int x,int y) //落子
{
if (intGame_Start==0) //判断游戏未开始
return;
if (intGame_Body[x/20][y/20]!=0)
return;
Graphics g=getGraphics();
if (color_Qizi==1) //判断黑子还是白子
{
g.setColor(Color.black);
color_Qizi=0;
}
else
{
g.setColor(Color.white);
color_Qizi=1;
}
g.fillOval(x-10,y-10,20,20);
intGame_Body[x/20][y/20]=color_Qizi+1;
if (Game_win_1(x/20,y/20)) //判断输赢
{
lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");
intGame_Start=0;
}
if (Game_win_2(x/20,y/20)) //判断输赢
{
lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");
intGame_Start=0;
}
if (Game_win_3(x/20,y/20)) //判断输赢
{
lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");
intGame_Start=0;
}
if (Game_win_4(x/20,y/20)) //判断输赢
{
lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");
intGame_Start=0;
}
}
public String Get_qizi_color(int x)
{
if (x==0)
return "黑子";
else
return "白子";
}
public void draw_qipan(Graphics G) //画棋盘 15*15
{
G.setColor(Color.lightGray);
G.fill3DRect(10,10,300,300,true);
G.setColor(Color.black);
for(int i=1;i<16;i++)
{
G.drawLine(20,20*i,300,20*i);
G.drawLine(20*i,20,20*i,300);
}
}
public void Game_start() //游戏开始
{
intGame_Start=1;
Game_btn_enable(false);
b2.setEnabled(true);
}
public void Game_start_csh() //游戏开始初始化
{
intGame_Start=0;
Game_btn_enable(true);
b2.setEnabled(false);
ckbHB[0].setState(true);
for (int i=0;i<16 ;i++ )
for (int j=0;j<16 ;j++ )
intGame_Body[i][j]=0;
lblWin.setText("");
}
public void Game_re() //游戏重新开始
{
repaint();
Game_start_csh();
}
public void Game_btn_enable(boolean e) //设置组件状态
{
b1.setEnabled(e);
b2.setEnabled(e);
ckbHB[0].setEnabled(e);
ckbHB[1].setEnabled(e);
}
public boolean Game_win_1(int x,int y) //判断输赢 横
{
int x1,y1,t=1;
x1=x;
y1=y;
for (int i=1;i<5 ;i++ )
{
if (x1>15)
break;
if (intGame_Body[x1+i][y1]==intGame_Body[x][y])
t+=1;
else
break;
}
for (int i=1;i<5 ;i++ )
{
if (x1<1)
break;
if(intGame_Body[x1-i][y1]==intGame_Body[x][y])
t+=1;
else
break;
}
if (t>4)
return true;
else
return false;
}
public boolean Game_win_

2(int x,int y) //判断输赢 竖
{
int x1,y1,t=1;
x1=x;
y1=y;
for (int i=1;i<5 ;i++ )
{
if (x1>15)
break;
if (intGame_Body[x1][y1+i]==intGame_Body[x][y])
t+=1;
else
break;
}
for (int i=1;i<5 ;i++ )
{
if (x1<1)
break;
if(intGame_Body[x1][y1-i]==intGame_Body[x][y])
t+=1;
else
break;
}
if (t>4)
return true;
else
return false;
}
public boolean Game_win_3(int x,int y) //判断输赢 左斜
{
int x1,y1,t=1;
x1=x;
y1=y;
for (int i=1;i<5 ;i++ )
{
if (x1>15)
break;
if (intGame_Body[x1+i][y1-i]==intGame_Body[x][y])
t+=1;
else
break;
}
for (int i=1;i<5 ;i++ )
{
if (x1<1)
break;
if(intGame_Body[x1-i][y1+i]==intGame_Body[x][y])
t+=1;
else
break;
}
if (t>4)
return true;
else
return false;
}
public boolean Game_win_4(int x,int y) //判断输赢 左斜
{
int x1,y1,t=1;
x1=x;
y1=y;
for (int i=1;i<5 ;i++ )
{
if (x1>15)
break;
if (intGame_Body[x1+i][y1+i]==intGame_Body[x][y])
t+=1;
else
break;
}
for (int i=1;i<5 ;i++ )
{
if (x1<1)
break;
if(intGame_Body[x1-i][y1-i]==intGame_Body[x][y])
t+=1;
else
break;
}
if (t>4)
return true;
else
return false;
}
}




/*
* 课程设计作品Clock(日历时钟界面)
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Clock extends JFrame {
public Clock(int r) { //创建时钟钟面类对象
ClockPaint cp = new ClockPaint(r);
this.add(cp);
this.setTitle("Clock");
this.setSize(3*r, 3*r);
this.setVisible(true);
this.setResizable(false); //设置为不可改变窗口大小
this.setAlwaysOnTop(true); //设置为窗口总在前面显示
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); //窗口‘关闭’按钮事件处理
}
});
}
public static void main(String[] s) { //主程序
new Clock(100);
}
}

//该类用于显示时钟钟面
class ClockPaint extends JPanel implements Runnable {

int x, y, r;//上边距,左边距,钟面半径
int h, m, s;// 时钟,分钟,秒钟在钟面上形成的角度数(与“12:00:00”指针形成的夹角)
double rad = Math.PI / 180;//角度1°所对应的弧度

public ClockPaint(int r) {//构造函数
this.x = r/2;
this.y = r/2;
this.r = r;
Calendar now = new GregorianCalendar();//创建当前日期时间对象
s = now.get(Calendar.SECOND) * 6;// 初始化秒钟角度;一圆360度,秒钟转一周60秒,所以每走1秒角度转过6度
m = now.get(

Calendar.MINUTE) * 6;// 同上
h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30//钟面是12时制的,每1个小时所占的角度是360/12=30度
+ now.get(Calendar.MINUTE) / 12 * 6;// 1个小时里时钟经过5个刻度点,所以每12分钟才过1刻度点,
//而每个刻度点只见角度相差6度

Thread t = new Thread(this);//创建一个线程对象
t.start();//启动线程
}

//方法重写,绘制钟面各元素
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
// 清屏
g2d.setColor(Color.BLACK); //设置画笔颜色
g2d.fillRect(0, 0, r * 3, r * 3); //填充矩形,参数分别为左上角顶点横、纵坐标,宽、高
// 画圆
g2d.setColor(Color.WHITE);
g2d.drawOval(x, y, r * 2, r * 2); //参数分别为矩形的左上角顶点横、纵坐标,矩形宽,矩形高(矩形内切椭圆)
int d = 0,x1 = 0,y1 = 0; //角度数,横坐标,纵坐标
// 刻度点
for (int i = 0; i < 60; i++) { //循环绘出60个刻度点
x1 = (int) ((r - 2) * Math.sin(rad * d)); //刻度点横坐标 (r - 2):距圆面向内缩进2像素画点;
y1 = (int) ((r - 2) * Math.cos(rad * d)); //刻度点纵坐标 (rad * d):表示的是弧度
g2d.drawString(".", x + r + x1 - 1, y + r - y1 + 1); //后2个参数表示横纵坐标,即从该坐标处画“.”
d += 6; //一个圆360度,每隔6度画一个点,共60个点
}
// 数字
g2d.setColor(Color.YELLOW);
d = 30;//时钟刻度数从1开始,角度的基线是“00”秒时的秒钟,刻度‘1’的夹角为30°
for (int i = 1; i <= 12; i++) {
x1 = (int) ((r - 10) * Math.sin(rad * d));//(r - 10): 缩进10像素标刻度数
y1 = (int) ((r - 10) * Math.cos(rad * d));
g2d.drawString(i + "", x + r + x1 - 4, y + r - y1 + 5);
d += 30;
}
// 时针
g2d.setColor(Color.GREEN);
g2d.setStroke(new BasicStroke(3.5f));
x1 = (int) ((2*r / 5) * Math.sin(rad * h));
y1 = (int) ((2*r / 5) * Math.cos(rad * h));
g2d.drawLine(x + r, y + r, x + r + x1, y + r - y1);//画直线, 前2参数表起点坐标,后2参数表终点坐标
// 分针
g2d.setColor(Color.BLUE);
g2d.setStroke(new BasicStroke(2.5f));
x1 = (int) ((3*r / 5) * Math.sin(rad * m));
y1 = (int) ((3*r / 5) * Math.cos(rad * m));
g2d.drawLine(x + r, y + r, x + r + x1, y + r - y1);
// 秒针
g2d.setColor(Color.RED);
g2d.setStroke(new BasicStroke(1.5f));
x1 = (int) ((4*r / 5) * Math.sin(rad * s));
y1 = (int) ((4*r / 5) * Math.cos(rad * s));
g2d.drawLine(x + r, y + r, x + r + x1, y + r - y1);

// 获得时间、星期、日期
Calendar now1 = new GregorianCalenda

r(); //获得当前的日期时间对象
int hour = now1.get(Calendar.HOUR_OF_DAY); //小时,获得当前的时刻
int minute = now1.get(Calendar.MINUTE); //分,同上
int second = now1.get(Calendar.SECOND); //秒
int year = now1.get(Calendar.YEAR); //年
int month = now1.get(Calendar.MONTH)+1; //月
int date = now1.get(Calendar.DAY_OF_MONTH); //日
int week = now1.get(Calendar.DAY_OF_WEEK); //星期

// 星期转换字符串
String weekDay = "";
switch(--week){
case 1:weekDay="一";break;
case 2:weekDay="二";break;
case 3:weekDay="三";break;
case 4:weekDay="四";break;
case 5:weekDay="五";break;
case 6:weekDay="六";break;
case 7:weekDay="日";break;
default :weekDay="";break;
}

// 显示时间、星期、日期
g2d.setColor(Color.PINK);
g2d.drawString((hour<10?"0":"")+hour+ ":"
+ (minute<10?"0":"")+minute+ ":"
+ (second<10?"0":"")+second
+" "
+"("+weekDay+")"
+" "
+year+ "/"
+ (month<10?"0":"")+month+ "/"
+ (date<10?"0":"")+date,
5, 15);
}

//实现接口Runnable的方法run,用来刷新时分秒三指针的度数
public void run() {
while (true) {
try {
Thread.sleep(1000);//休眠一秒
} catch (InterruptedException ie) { //当程序出现异常,弹出对话框提示
JOptionPane.showMessageDialog(this, ie.getMessage(), "程序异常", JOptionPane.ERROR_MESSAGE);
}
s += 6; //每过1秒,秒针角度增加6度
if(s%360==0) {
m += 6; //秒针每走一周,分针角度增加6度
if(m%72==0)
h += 6; //当秒针角度为360°的倍数(即秒针指向12),且分针角度为72°的倍数时,时针角度增加6°即走到下一个刻点
}
this.repaint();//重新绘制钟面,系统自动调用paint(Graphics g)
}
}
}























相关文档
最新文档