java编程求解
/** To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tanke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Tanke extends JFrame {
static JButton kaishi=new JButton("开始");//设置按钮
static JButton shezhi=new JButton("设置");
static JButton bangzhu=new JButton("帮助");
static JButton guanyu =new JButton("关于");
static JButton tuichu=new JButton("退出");
static Tanke f=new Tanke();
public static void main(String[] args) {
f.setTitle("坦克大战");//设置标题
f.setLayout(null);//取消布局管理器
f.setSize(800, 526);//设置大小
f.setLocation(300,100);//设置位置
f.setResizable(false);//不可改变大小
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出
f.setVisible(true);//显示
kaishi.setFont(new Font("宋体",50,30));//设置按钮字体大小位置
kaishi.setBounds(130, 100, 100, 50);
shezhi.setFont(new Font("宋体",50,30));
shezhi.setBounds(130, 170, 100, 50);
bangzhu.setFont(new Font("宋体",50,30));
bangzhu.setBounds(130,240,100,50);
guanyu.setFont(new Font("宋体",50,30));
guanyu.setBounds(130,310,100,50);
tuichu.setFont(new Font("宋体",50,30));
tuichu.setBounds(130,380, 100, 50);
f.add(kaishi);//加入按钮
f.add(shezhi);
f.add(bangzhu);
f.add(guanyu);
f.add(tuichu);
kaishi.addMouseListener(new Mykaishi());//注册事件监听者
shezhi.addMouseListener(new Myshezhi());
bangzhu.addMouseListener(new Mybangzhu());
guanyu.addMouseListener(new Myguanyu());
tuichu.addMouseListener(new Mytuichu());
}
@Override
public void paint(Graphics g){//导入开始页面图片
BufferedImage image=null;
try {
image =ImageIO.read(new File("D:/Tanke/src/tanke/kaishi.JPG"));
} catch (IOException ex) {
Logger.getLogger(Tanke.class.getName()).log(Level.SEVERE, null, ex);
}
g.drawImage(image, 0, 0, this);
g.setColor(Color.BLACK);
g.setFont(new Font("宋体",50,50));
g.drawString("坦克大战", 90, 100);
}
static class Mykaishi implements MouseListener{//开始事件处理
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
Game jf=new Game();
jf.game(jf);
jf.repaint();
f.setVisible(false);
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
static class Myshezhi implements MouseListener{//设置设置事件处理
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
JOptionPane.showMessageDialog(shezhi, "Sorry!技术有限无法导入背景音乐!");
f.setVisible(true);
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
static class Mybangzhu implements MouseListener{//设置帮助事件处理
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
JOptionPane.showMessageDialog(bangzhu, "上:W 下:S 左:A 右:D 子弹:Enter");
f.setVisible(true);
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
static class Myguanyu implements MouseListener{//设置关于事件处理
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
JOptionPane.showMessageDialog(guanyu,"本游戏由长大软工1202班董进编写");
f.setVisible(true);
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
static class Mytuichu implements MouseListener{//设置退出事件处理
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
System.exit(0);
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
}
class Game extends JFrame{
static int x=0,y=0; //坦克坐标
public Game(){//游戏界面设置
setTitle("坦克大战");
setSize(800,526);
setLayout(null);
setLocation(300,100);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void paint(Graphics g){//导入游戏界面背景
BufferedImage pic=null;
try {
pic=ImageIO.read(new File("D:/Tanke/src/tanke/beijing.JPG"));
} catch (IOException ex) {
Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
}
g.drawImage(pic, 0, 0, this);
}
public void game(JFrame j){//加入坦克
ImageIcon icw=new ImageIcon("D:/Tanke/src/tanke/tanke7上.jpg");
ImageIcon ica=new ImageIcon("D:/Tanke/src/tanke/tanke7左.jpg");
ImageIcon icd=new ImageIcon("D:/Tanke/src/tanke/tanke7右.jpg");
ImageIcon ics=new ImageIcon("D:/Tanke/src/tanke/tanke7下.jpg");
JButton btw=new JButton(icw);
JButton bta=new JButton(ica);
JButton btd=new JButton(icd);
JButton bts=new JButton(ics);
btw.addKeyListener(new MyKeyListener());
btw.setBounds(500, 300, 30, 36);
j.add(btw);
}
static class MyKeyListener implements KeyListener{//实现坦克移动
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
JButton but=(JButton)e.getSource();
x=but.getLocation().x;
y=but.getLocation().y;
if(e.getKeyCode()==KeyEvent.VK_W)
{
y=y-5;
if(y<=0)y=0;
}
else if(e.getKeyCode()==KeyEvent.VK_S)
{
y=y+5;
if(y>=460)y=460;
}
else if(e.getKeyCode()==KeyEvent.VK_S)
{
y=y+5;
if(y>=460)y=460;
} else if(e.getKeyCode()==KeyEvent.VK_A)
{
x=x-5;
if(x<=0)x=0;
} else if(e.getKeyCode()==KeyEvent.VK_D)
{
x=x+5;
if(x>=765)x=765;
}
System.out.println("x="+x+" "+"y="+y);
but.setLocation(x,y);
}
@Override
public void keyReleased(KeyEvent e) {}
}
}
用NetBeans运行时开始页面和按钮不能兼容,且页面不能刷新,开始进入游戏页面是问题是一样的,且坦克不能转向吧,求大神指点,急用! --------------------编程问答-------------------- 楼主 你只添加事件响应啊。。很多都木有做好
比如 private class painttank implements Runnable{
public void run() {
while(true){
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
构建一个线程内部类 调用你的重画机制 来实现画面的更新。。。
对于坦克 你要构建一个坦克类是不是更好 因为你要打死它的话怎么办?各种问题啊。。。 --------------------编程问答-------------------- 我是菜鸟,大一新生,刚学java,望大哥多多指点!
补充:Java , Java SE