高分求<<可视化Java GUI程序设计——基于Eclipse VE开发环境>>清华大学的PDF,1505047321@qq.com

高分求<<可视化Java GUI程序设计——基于Eclipse VE开发环境>>清华大学的PDF,1505047321@qq.com,第1张

import java.awt.*

import javax.swing.*

import java.awt.event.*

import java.util.*

public class SnakeGame extends JFrame implements KeyListener{

private int stat=1,direction=0,bodylen=6,headx=7,heady=8,

tailx=1,taily=8,tail,foodx,foody,food//初始化定义变量

public final int EAST=1,WEST=2,SOUTH=3,NORTH=4//方向常量

int [][] fillblock=new int [20][20]//定义蛇身所占位置

public SnakeGame() {//构造函数

super("贪吃蛇")

setSize(510,510)

setVisible(true)//设定窗口属性

addKeyListener(this)//添加监听

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

for(int i=1i<=7i++) fillblock[i][8]=EAST//初始化蛇身属性

direction=EAST//方向初始化的设置

FoodLocate()//定位食物

while (stat==1){

fillblock[headx][heady]=direction

switch(direction){

case 1:headx++break

case 2:headx--break

case 3:heady++break

case 4:heady--break

}//蛇头的前进

if(heady>19||headx>19||tailx>19||taily>19||heady<0||headx<0||tailx<0||taily<0||fillblock[headx][heady]!=0){

stat=0

break

} //判断游戏是否结束

try{

Thread.sleep(150)}

catch(InterruptedException e){}//延迟

fillblock[headx][heady]=direction

if(headx==foodx&&heady==foody){//吃到食物

FoodLocate()

food=2

try{

Thread.sleep(100)}

catch(InterruptedException e){}//延迟

}

if(food!=0)food--

else{tail=fillblock[tailx][taily]

fillblock[tailx][taily]=0//蛇尾的消除

switch(tail){

case 1:tailx++break

case 2:tailx--break

case 3:taily++break

case 4:taily--break

}//蛇尾的前进

}

repaint()

}

if(stat==0)

JOptionPane.showMessageDialog(null,"GAME OVER","Game Over",JOptionPane.INFORMATION_MESSAGE)

}

public void keyPressed(KeyEvent e) {//按键响应

int keyCode=e.getKeyCode()

if(stat==1) switch(keyCode){

case KeyEvent.VK_UP:if(direction!=SOUTH) direction=NORTHbreak

case KeyEvent.VK_DOWN:if(direction!=NORTH)direction=SOUTHbreak

case KeyEvent.VK_LEFT:if(direction!=EAST)direction=WESTbreak

case KeyEvent.VK_RIGHT:if (direction!=WEST)direction=EASTbreak

}

}

public void keyReleased(KeyEvent e){}//空函数

public void keyTyped(KeyEvent e){} //空函数

public void FoodLocate(){//定位食物坐标

do{

Random r=new Random()

foodx=r.nextInt(20)

foody=r.nextInt(20)

}while (fillblock[foodx][foody]!=0)

}

public void paint(Graphics g){//画图

super.paint(g)

g.setColor(Color.BLUE)

for(int i=0i<20i++)

for(int j=0j<20j++)

if (fillblock[i][j]!=0)

g.fillRect(25*i+5,25*j+5,24,24)

g.setColor(Color.RED)

g.fillRect(foodx*25+5,foody*25+5,24,24)

}

public static void main(String[] args) {//主程序

SnakeGame application=new SnakeGame()

}

}

=====================第一个类=============================

/**

*

* 实现了系统计算器连续按"="和按"+","-","*","/"进行累记运算的模式

*

* */

import javax.swing.*

import java.awt.*

import java.awt.event.*

public class CalculatorFrame extends JFrame {

private JTextField txtDis = new JTextField("0", 16)//显示文本框

private JButton[] btnGrp = new JButton[17]//17个按钮

private JPanel jpnMain = new JPanel()//主面板

private JPanel jpnNorth = new JPanel()//上面板

private JPanel jpnSouth = new JPanel()//下面板

private double numSaved//保存的前一个数

private String operator = ""//保存的运算符号

private String lastPress = ""//上一次按的按钮("数字","运算符" 或 "等号")

/*构造函数*/

public CalculatorFrame() {

//窗口设置

setTitle("计算器")

setSize(210, 250)

buttonInit()//按钮初始化

//主面板设置

add(jpnMain)

jpnMain.setLayout(null)

jpnMain.add(jpnNorth)

jpnMain.add(jpnSouth)

jpnMain.add(btnGrp[15])

btnGrp[15].setBounds(8, 180, 188, 30)//添加“=”按钮

//上面板设置

jpnNorth.setBounds(8, 10, 190, 30)

jpnNorth.add(txtDis)

txtDis.setHorizontalAlignment(JTextField.RIGHT)//设置文本右对齐

txtDis.setEditable(false)

//下面板设置

jpnSouth.setBounds(8, 60, 190, 120)

jpnSouth.setLayout(new GridLayout(4, 4))

addSouthJpn()//添加17个按钮

//添加监听器

addListener()

}

/*17个按钮显示值初始化*/

public void buttonInit() {

for (int i = 0i <10i++) {

btnGrp[i] = new JButton("" + i)

}

btnGrp[10] = new JButton(".")

btnGrp[11] = new JButton("+")

btnGrp[12] = new JButton("-")

btnGrp[13] = new JButton("*")

btnGrp[14] = new JButton("/")

btnGrp[15] = new JButton("=")

btnGrp[16] = new JButton("C")

for (int i = 0i <17i++) {

btnGrp[i].setFont(new Font("Dialog", Font.PLAIN, 18))

}

}

/*按网格顺序添加除“=”外16个按钮*/

public void addSouthJpn() {

int[] index={7,8,9,14,4,5,6,13,1,2,3,12,0,16,10,11}

for(int i=0i<index.lengthi++){

jpnSouth.add(btnGrp[index[i]])

}

}

/*给按钮添加监听器*/

public void addListener() {

//给数字按钮添加监听器,btnGrp[10]是"."号

for (int i = 0i <= 10i++) {

btnGrp[i].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String button = ( (JButton) e.getSource()).getText()

//如果有运算 *** 作符或文本框数字为0并且按钮不为“。”

if (lastPress == "运算符" || lastPress == "等号"

|| txtDis.getText().equals("0") &&button != ".") {

txtDis.setText("")//清空文本框

}

if (lastPress == "等号") {

operator = ""//上次按的是=号,这次按数字,清空运算符

}

if (button == "." &&txtDis.getText().indexOf(".") != -1) {

} //这次按的是.文本框内已经包含.这种情况什么都不做,防止.重复

else {

txtDis.setText(txtDis.getText() + button)//文本框添加内容

}

lastPress = "数字"

}

})

}

//给运算 *** 作符按钮添加监听器

for (int i = 11i <= 14i++) {

btnGrp[i].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (operator != "" &&lastPress == "数字") { //运算符不为空并且之前按的是数字

(btnGrp[15].getActionListeners())[0].actionPerformed(e)//手动调用=号的监听响应实现=号功能

}

numSaved = Double.parseDouble(txtDis.getText())//保存当前文本框的数字

operator = ( (JButton) e.getSource()).getText()//保存运算 *** 作符

lastPress = "运算符"//添加运算符标记,下次文本框清空

}

})

}

//给等号按钮添加监听器

btnGrp[15].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

double numNow = Double.parseDouble(txtDis.getText())//当前文本框的数字

//运算符匹配 *** 作

if (operator.equals("+")) {

txtDis.setText( (numSaved + numNow) + "")

}

else if (operator.equals("-")) {

if (lastPress == "等号") { //累记运算时的算法(一直按等号)

txtDis.setText( (numNow - numSaved) + "")

}

else {

txtDis.setText( (numSaved - numNow) + "")

}

}

else if (operator.equals("*")) {

txtDis.setText( (numSaved * numNow) + "")

}

else if (operator.equals("/")) {

if (lastPress == "等号") { //累记运算时的算法(一直按等号)

txtDis.setText( (numNow / numSaved) + "")

}

else {

txtDis.setText( (numSaved / numNow) + "")

}

}

if (lastPress != "等号") { //是第一次,以后按累记运算

numSaved = numNow//保存数改为后一个数

}

lastPress = "等号"//添加运算符标记,下次文本框清空

}

})

//给复位符“C”按钮添加监听器

btnGrp[16].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

txtDis.setText("0")

numSaved = 0

operator = ""

lastPress = ""

}

})

}

}

=====================第二个类=============================

import javax.swing.*

public class Test {

public Test() {

CalculatorFrame calframe = new CalculatorFrame()//新窗口

calframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//设置关闭

calframe.setLocationRelativeTo(null)//窗口置中

calframe.setVisible(true)//显示窗口

calframe.setDefaultLookAndFeelDecorated(false)//使用windows视感

calframe.setResizable(false)//窗口不可调大小

}

public static void main(String[] args) {

Test test = new Test()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11492780.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存