当前位置:首页 > 代码 > 正文

java大作业代码(编程大作业)

admin 发布:2022-12-19 06:19 157


本篇文章给大家谈谈java大作业代码,以及编程大作业对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java大作业 下面是我的代码,求大神帮忙添加一个重新开始的按钮并实现这个按钮的功能

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

public class yingyu extends JFrame {

private JTextField word;

private JLabel label;

private JButton next;

private JButton restart;

private int index = 0;

int i=1;

private String[] words;

private int score = 0;

public yingyu(){

words = new String[] {"苹果", "香蕉", "狗", "猫", "水"};

word = new JTextField();

Dimension size = new Dimension(60, 30);

word.setPreferredSize(size );

label = new JLabel(words[0]);

next = new JButton("下一道");

restart = new JButton("重新开始");

next.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(iwords.length){

label.setText(words[i]);

i++;}

else {i=1;

label.setText(words[0]);}

}

});

restart.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

label.setText(words[0]);

i=1;

}

});

setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3));

add(label);

add(word);

add(next);

add(restart);

setBounds(100, 100, 300, 300);

setVisible(true);

}

public static void main(String[] args){

yingyu yingyu =new yingyu();

}

public void reload(){

label.setText(words[index]);

repaint();

}

}

需要一份500行的java程序,期末大作业,最好带详细注释。

Java生成CSV文件简单操作实例

CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件。在 CSV文件中,数据“栏”以逗号分隔,可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏。如:

123   1,张三,男2,李四,男3,小红,女   

Java生成CSV文件(创建与导出封装类)

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

*            源数据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();

}

// 定义文件名格式并创建

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();

// 写入文件内容

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();

/*-------------------------------*/ 

//以下部分根据不同业务做出相应的更改

StringBuilder sbContext = new StringBuilder("");

if (null != BeanUtils.getProperty(row,(String) propertyEntry.getKey())) {

if("证件号码".equals(propertyEntry.getValue())){

//避免:身份证号码 ,读取时变换为科学记数 - 解决办法:加 \t(用Excel打开时,证件号码超过15位后会自动默认科学记数)

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();

}

}

}

}

/**

* 删除单个文件

*

* @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 = "文件导出";

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" ,只为解决数字格式超过15位后,在Excel中打开展示科学记数问题。

java大作业,麻烦大神了,先给一百分,解决了,后加一百

复数类:

/**

*复数类

* @author sunsnowad

*/

public class Plural {

double real, unreal;

public Plural(double real, double unreal){

this.real = real;

this.unreal = unreal;

}

private Plural() {

real = 0.0;

unreal = 0.0;

}

public Plural add(Plural a, Plural b){

return new Plural(a.getReal()+b.getReal(), a.getUnreal()+b.getUnreal());

}

public Plural minus(Plural a, Plural b){

return new Plural(a.getReal()-b.getReal(), a.getUnreal()-b.getUnreal());

}

public Plural multi(Plural a, Plural b){

return new Plural(a.getReal()*b.getReal()-a.getUnreal()*b.getUnreal(),

a.getUnreal()*b.getReal() + a.getReal()*b.getUnreal());

}

public Plural division(Plural a, Plural b){

double middleResult = a.getReal()*a.getReal() + a.getUnreal()*b.getUnreal();

double realResult = (a.getReal()*b.getReal()+a.getUnreal()*b.getUnreal())/middleResult;

double unrealResult = (a.getUnreal()*b.getReal()-a.getReal()*b.getUnreal())/middleResult;

return new Plural(realResult, unrealResult);

}

public double getReal() {

return real;

}

public void setReal(double real) {

this.real = real;

}

public double getUnreal() {

return unreal;

}

public void setUnreal(double unreal) {

this.unreal = unreal;

}

@Override

public String toString(){

return real + "+" + unreal + "i";

}

public static Plural parsePlural(String plural){

plural = plural.trim();

String realString = "";

String unrealString = "";

if(plural.contains("i")){

for(int i = 0; i plural.length(); i ++){

if(plural.charAt(i) == '+'){

for(int j = i + 1; j plural.length()-1; j ++){

unrealString += plural.charAt(j);

}

break;

}

realString+=plural.charAt(i);

}

}

//TODO else

return new Plural(Double.parseDouble(realString), Double.parseDouble(unrealString));

}

}

Applet测试程序(使用Netbeans设计工具):

/**

*

* @author sunsnowad

*/

public class PluralTest extends javax.swing.JApplet {

/** Initializes the applet PluralTest */

public void init() {

try {

java.awt.EventQueue.invokeAndWait(new Runnable() {

public void run() {

initComponents();

}

});

} catch (Exception ex) {

ex.printStackTrace();

}

}

/** This method is called from within the init() method to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// editor-fold defaultstate="collapsed" desc="Generated Code"

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jTextField1 = new javax.swing.JTextField();

jLabel2 = new javax.swing.JLabel();

jTextField2 = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

jButton4 = new javax.swing.JButton();

jLabel3 = new javax.swing.JLabel();

jLabel1.setText("实数1:");

jTextField1.setText("4+5i");

jLabel2.setText("实数2:");

jTextField2.setText("5+4.4i");

jButton1.setText("加");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

jButton2.setText("减");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

}

});

jButton3.setText("乘");

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt);

}

});

jButton4.setText("除");

jButton4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton4ActionPerformed(evt);

}

});

jLabel3.setText("结果:");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jTextField2)))

.addComponent(jButton4)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jButton3)

.addComponent(jButton2))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE))

.addComponent(jButton1))

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton3)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton4))

.addGroup(layout.createSequentialGroup()

.addGap(47, 47, 47)

.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(122, Short.MAX_VALUE))

);

}// /editor-fold

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

Plural a = Plural.parsePlural(jTextField1.getText());

Plural b = Plural.parsePlural(jTextField2.getText());

jLabel3.setText(a.add(a, b).toString());

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

Plural a = Plural.parsePlural(jTextField1.getText());

Plural b = Plural.parsePlural(jTextField2.getText());

jLabel3.setText(a.minus(a, b).toString());

}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

Plural a = Plural.parsePlural(jTextField1.getText());

Plural b = Plural.parsePlural(jTextField2.getText());

jLabel3.setText(a.multi(a, b).toString());

}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

Plural a = Plural.parsePlural(jTextField1.getText());

Plural b = Plural.parsePlural(jTextField2.getText());

jLabel3.setText(a.division(a, b).toString());

}

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JButton jButton3;

private javax.swing.JButton jButton4;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JTextField jTextField1;

private javax.swing.JTextField jTextField2;

// End of variables declaration

JAVA大作业

一句一句看,其实不难的

import java.util.Random;

import java.util.Scanner;

public class Test {

public static Random r = new Random();

public static void printMenu() {    //自定义的方法

System.out.println("(1) play another round");

System.out.println("(2) exit the game");

System.out.print("Selection:");

}

public static void main(String[] args) throws Exception

{

String content = null;

Scanner scan = new Scanner(System.in);  //

while (true)                            //true在这里使得程序一直循环

{

printMenu();                        //调用方法,打印出三句话 ...

content = scan.nextLine();          //读取输入

System.out.println();

// 输入内容是2,则退出程序

if (content.equals("2"))

break;                      //break 跳出当前的循环,也就是跳出while循环,程序往下执行,若没有可执行则结束程序

if (content.equals("1")) {

System.out.print("Text to Match: ");

int count = r.nextInt(50) + 1;   //得到1~50随机数,即[1,50]

char[] letters = new char[count]; //长度为count的char数组

for (int i = 0; i count; i++)   //i count等价i letters.length

{

letters[i] = (char) (r.nextInt(26) + 97);  //给letters[i]赋值,字母a~z

System.out.print(letters[i]);

}

System.out.print("\n               ");

long startTime = System.currentTimeMillis();   //得到开始时间

int correctCount = 0;

content = scan.nextLine();                     //得到输入的字符串

for (int i = 0; i count; i++) {

if (content.charAt(i) == letters[i])       //输入的字符串的每个字母和letters中的比较

correctCount++;

}

long endTime = System.currentTimeMillis();     //得到结束时间

int time = (int) ((endTime - startTime) / 1000);  //计算所花的时间

if (correctCount == 0)

System.out

.println("INCORRECTLY TYPED, YOU GET A SPEED OF 0!");

else {

System.out.println("共花了" + time + "秒,正确率为:"

+ ((int) (((double) correctCount / count)) * 100)

+ "%");

}

}

System.out.println();

}

}

}

我们老师要一份JAVA大作业,但是我不会,想让帮帮我写一个不能太短的程序

我刚做了一个java的小游戏,贪吃蛇,算是草稿版,比较粗糙,不知能否帮上忙!

import java.awt.*;

import java.awt.event.*;

public class GreedSnake //主类

{

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new MyWindow();

}

}

class MyPanel extends Panel implements KeyListener,Runnable//自定义面板类,继承了键盘和线程接口

{

Button snake[]; //定义蛇按钮

int shu=0; //蛇的节数

int food[]; //食物数组

boolean result=true; //判定结果是输 还是赢

Thread thread; //定义线程

static int weix,weiy; //食物位置

boolean t=true; //判定游戏是否结束

int fangxiang=0; //蛇移动方向

int x=0,y=0; //蛇头位置

MyPanel()

{

setLayout(null);

snake=new Button[20];

food=new int [20];

thread=new Thread(this);

for(int j=0;j20;j++)

{

food[j]=(int)(Math.random()*99);//定义20个随机食物

}

weix=(int)(food[0]*0.1)*60; //十位*60为横坐标

weiy=(int)(food[0]%10)*40; //个位*40为纵坐标

for(int i=0;i20;i++)

{

snake[i]=new Button();

}

add(snake[0]);

snake[0].setBackground(Color.black);

snake[0].addKeyListener(this); //为蛇头添加键盘监视器

snake[0].setBounds(0,0,10,10);

setBackground(Color.cyan);

}

public void run() //接收线程

{

while(t)

{

if(fangxiang==0)//向右

{

try

{

x+=10;

snake[0].setLocation(x, y);//设置蛇头位置

if(x==weixy==weiy) //吃到食物

{

shu++;

weix=(int)(food[shu]*0.1)*60;

weiy=(int)(food[shu]%10)*40;

repaint(); //重绘下一个食物

add(snake[shu]); //增加蛇节数和位置

snake[shu].setBounds(snake[shu-1].getBounds());

}

thread.sleep(100); //睡眠100ms

}

catch(Exception e){}

}

else if(fangxiang==1)//向左

{

try

{

x-=10;

snake[0].setLocation(x, y);

if(x==weixy==weiy)

{

shu++;

weix=(int)(food[shu]*0.1)*60;

weiy=(int)(food[shu]%10)*40;

repaint();

add(snake[shu]);

snake[shu].setBounds(snake[shu-1].getBounds());

}

thread.sleep(100);

}

catch(Exception e){}

}

else if(fangxiang==2)//向上

{

try

{

y-=10;

snake[0].setLocation(x, y);

if(x==weixy==weiy)

{

shu++;

weix=(int)(food[shu]*0.1)*60;

weiy=(int)(food[shu]%10)*40;

repaint();

add(snake[shu]);

snake[shu].setBounds(snake[shu-1].getBounds());

}

thread.sleep(100);

}

catch(Exception e){}

}

else if(fangxiang==3)//向下

{

try

{

y+=10;

snake[0].setLocation(x, y);

if(x==weixy==weiy)

{

shu++;

weix=(int)(food[shu]*0.1)*60;

weiy=(int)(food[shu]%10)*40;

repaint();

add(snake[shu]);

snake[shu].setBounds(snake[shu-1].getBounds());

}

thread.sleep(100);

}

catch(Exception e){}

}

int num1=shu;

while(num11)//判断是否咬自己的尾巴

{

if(snake[num1].getBounds().x==snake[0].getBounds().xsnake[num1].getBounds().y==snake[0].getBounds().y)

{

t=false;

result=false;

repaint();

}

num1--;

}

if(x0||x=this.getWidth()||y0||y=this.getHeight())//判断是否撞墙

{

t=false;

result=false;

repaint();

}

int num=shu;

while(num0) //设置蛇节位置

{

snake[num].setBounds(snake[num-1].getBounds());

num--;

}

if(shu==15) //如果蛇节数等于15则胜利

{

t=false;

result=true;

repaint();

}

}

}

public void keyPressed(KeyEvent e) //按下键盘方向键

{

if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右键

{

if(fangxiang!=1)//如果先前方向不为左

fangxiang=0;

}

else if(e.getKeyCode()==KeyEvent.VK_LEFT)

{ if(fangxiang!=0)

fangxiang=1;

}

else if(e.getKeyCode()==KeyEvent.VK_UP)

{ if(fangxiang!=3)

fangxiang=2;

}

else if(e.getKeyCode()==KeyEvent.VK_DOWN)

{ if(fangxiang!=2)

fangxiang=3;

}

}

public void keyTyped(KeyEvent e)

{

}

public void keyReleased(KeyEvent e)

{

}

public void paint(Graphics g) //在面板上绘图

{

int x1=this.getWidth()-1;

int y1=this.getHeight()-1;

g.setColor(Color.red);

g.fillOval(weix, weiy, 10, 10);//食物

g.drawRect(0, 0, x1, y1); //墙

if(t==falseresult==false)

g.drawString("GAME OVER!", 250, 200);//输出游戏失败

else if(t==falseresult==true)

g.drawString("YOU WIN!", 250, 200);//输出游戏成功

}

}

class MyWindow extends Frame implements ActionListener//自定义窗口类

{

MyPanel my;

Button btn;

Panel panel;

MyWindow()

{

super("GreedSnake");

my=new MyPanel();

btn=new Button("begin");

panel=new Panel();

btn.addActionListener(this);

panel.add(new Label("begin后请按Tab键选定蛇"));

panel.add(btn);

panel.add(new Label("按上下左右键控制蛇行动"));

add(panel,BorderLayout.NORTH);

add(my,BorderLayout.CENTER);

setBounds(100,100,610,500);

setVisible(true);

validate();

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e)//按下begin按钮

{

if(e.getSource()==btn)

{

try

{

my.thread.start(); //开始线程

my.validate();

}

catch(Exception ee){}

}

}

}

java课程设计题目及代码是什么?

java课程设计题目及代码分别是:

1、题目:计算器。设计内容是设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算。

设计要求是设计的计算器应用程序可以完成家法、减法、乘法、除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。

2、代码:

数字按钮NumberButton类如下:

import java.awt.

import java.awt.event.

import javax.swing.

public class NumberButton extends Button.

{

int number.

public NumberButton(int number).

 {

super(""+number).

this.number=number.

setForeground(Color.blue).

}

public int getNumber().

{

return number;

}

}

其它java课程设计题目及代码是:

题目:华容道。编写一个按钮的子类,使用该子类创建的对象代表华容道中的人物。通过焦点事件控制人物颜色,当人物获得焦点时颜色为蓝色,当失去焦点时颜色为灰色。

通过键盘事件和鼠标事件来实现曹操、关羽等人物的移动。当人物上发生鼠标事件或键盘事件时,如果鼠标指针的位置是在人物的下方(也就是组件的下半部分)或按下键盘的“↓“键,该人物向下移动。向左、向右和向上的移动原理类似。

代码是:

String name[]={"曹操","关羽","张","刘","马","许","兵","兵","兵","兵"}.

for(int i=0;iname.length;i++).

{

person[i]=new Person(i,name[i]).

person[i].addKeyListener(this).

person[i].addMouseListener(this).

//     person[i].addFocusListener(new Person).

add(person[i]).

}

person[0].setBounds(104,54,100,100).

person[1].setBounds(104,154,100,50).

person[2].setBounds(54,154,50,100).

person[3].setBounds(204,154,50,100).

person[4].setBounds(54,54,50,100).

person[5].setBounds(204,54,50,100);

person[6].setBounds(54,254,50,50);

person[7].setBounds(204,254,50,50);

person[8].setBounds(104,204,50,50);

person[9].setBounds(154,204,50,50);

关于java大作业代码和编程大作业的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://www.ahzz.com.cn/post/3717.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载