求各位大佬帮帮忙,代码写出来了,但是有问题,我实在是找不出来问题出在哪??

吴溪啊 2020-06-18 12:46:00
package ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class GameInterface extends JFrame implements ActionListener{
JLabel lblBackground;
static JButton play01;
static JButton play02;
static JButton play03;

int index1;
int index2;
int score1;
int score2;
BufferedImage image1;
BufferedImage image2;


public GameInterface() {
// TODO Auto-generated constructor stub
//设置标题
setTitle("游戏主界面");
//位置居中
setLocation(200,100);
//设置大小
setSize(1200, 500);
//设置退出方式
setDefaultCloseOperation(EXIT_ON_CLOSE);

//加载背景图片
ImageIcon imageIcon=new ImageIcon("src/image/游戏主界面.jpg");
lblBackground=new JLabel(imageIcon);
//设置图片大小
lblBackground.setBounds(0, 0, 1200, 500);
lblBackground.setLayout(null);
this.add(lblBackground);

play01=new JButton("round 1");
play01.setBounds(500, 100, 150, 50);
lblBackground.add(play01);
play01.addActionListener(this);

play02=new JButton("round 2");
play02.setBounds(500, 200, 150, 50);
lblBackground.add(play02);
play02.addActionListener(this);

play03=new JButton("round 3");
play03.setBounds(500, 300, 150, 50);
lblBackground.add(play03);
play03.addActionListener(this);


setLayout(null);
setVisible(true);

}

public static void main(String[] args) {
GameInterface g=new GameInterface();
}



public void judge() {

Random r=new Random();
index1=r.nextInt(3)+1;
index2=r.nextInt(3)+1;

image1=App.getImg("/image/0"+index1+".jpg");
image2=App.getImg("/image/0"+index2+".jpg");

repaint();

if(index1==1&&index2==1) {
JOptionPane.showMessageDialog(null,"此局平局");
}
if(index1==1&&index2==2) {
score1++;
JOptionPane.showMessageDialog(null,"此局机兄胜出");
}
if(index1==1&&index2==3) {
score2++;
JOptionPane.showMessageDialog(null,"此局魏兄胜出");
}
if(index1==2&&index2==1) {
score2++;
JOptionPane.showMessageDialog(null,"此局魏兄胜出");
}
if(index1==2&&index2==2) {
JOptionPane.showMessageDialog(null,"此局平局");
}
if(index1==2&&index2==3) {
score1++;
JOptionPane.showMessageDialog(null,"此局机兄胜出");
}
if(index1==3&&index2==1) {
score1++;
JOptionPane.showMessageDialog(null,"此局机兄胜出");
}
if(index1==3&&index2==2) {
score2++;
JOptionPane.showMessageDialog(null,"此局魏兄胜出");
}
if(index1==3&&index2==3) {
JOptionPane.showMessageDialog(null,"此局平局");
}


}

public void finalJudge() {
if(score1>score2) {
JOptionPane.showMessageDialog(null,"结果为:机兄胜利");
}else if(score1<score2){
JOptionPane.showMessageDialog(null,"结果为:魏兄胜利");
}else {
JOptionPane.showMessageDialog(null,"结果为:平局");
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==play01) {
judge();
}


if(e.getSource()==play02) {
judge();
}

if(e.getSource()==play03) {
judge();
finalJudge();
}


}

@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.setColor(Color.BLACK);
g.setFont(new Font("楷体",Font.BOLD,30));
g.drawImage(image1, 0,20,image1.getWidth(),image1.getHeight(), null);
g.drawImage(image2,1030, 20,image2.getWidth(),image2.getHeight(), null);
g.drawString("机兄分数:"+score1, 250, 300);
g.drawString("魏兄分数:"+score2, 850, 300);

}

}

能够运行出来,但是一直有很多警示
...全文
186 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
吴溪啊 2020-06-18
  • 打赏
  • 举报
回复
引用 4 楼 dkwuxiang的回复:
这么感动,得好好请人家吃顿饭,看场电影
啊,哈哈哈,大概率都不在一个城市,哈哈哈
吴溪啊 2020-06-18
  • 打赏
  • 举报
回复
引用 2 楼 qq_39936465的回复:

public class GameInterface extends JFrame implements ActionListener {
	JLabel lblBackground;
	static JButton play01;
	static JButton play02;
	static JButton play03;

	int index1;
	int index2;
	int score1;
	int score2;
	BufferedImage image1;
	BufferedImage image2;
	BufferedImage[] image;
	boolean round1=true,round2=true,round3=true;

	public GameInterface() {
// TODO Auto-generated constructor stub
//设置标题
		setTitle("游戏主界面");
//位置居中
		setLocation(200, 100);
//设置大小
		setSize(1200, 500);
//设置退出方式
		setDefaultCloseOperation(EXIT_ON_CLOSE);

//加载背景图片
		ImageIcon imageIcon = new ImageIcon("src/image/游戏主界面.jpg");
		lblBackground = new JLabel();
//设置图片大小
		lblBackground.setBounds(0, 0, 1200, 500);
		lblBackground.setLayout(null);
		this.add(lblBackground);

		play01 = new JButton("round 1");
		play01.setBounds(500, 100, 150, 50);
		lblBackground.add(play01);
		play01.addActionListener(this);

		play02 = new JButton("round 2");
		play02.setBounds(500, 200, 150, 50);
		lblBackground.add(play02);
		play02.addActionListener(this);

		play03 = new JButton("round 3");
		play03.setBounds(500, 300, 150, 50);
		lblBackground.add(play03);
		play03.addActionListener(this);
		
		for(int i=1;i<4;i++) {
				image[i] = App.getImg("/image/0"+i+".jpg");
		}
		image1=image[1];
		image2=image[1];

		setLayout(null);
		setVisible(true);

	}

	public static void main(String[] args) {
		GameInterface g = new GameInterface();
	}

	public void judge() {

		Random r = new Random();
		index1 = r.nextInt(3) + 1;
		index2 = r.nextInt(3) + 1;

	    image1 = image[index1];
	    image2 = image[index2];

		repaint();

		if (index1 == 1 && index2 == 1) {
			JOptionPane.showMessageDialog(null, "此局平局");
		}
		if (index1 == 1 && index2 == 2) {
			score1++;
			JOptionPane.showMessageDialog(null, "此局机兄胜出");
		}
		if (index1 == 1 && index2 == 3) {
			score2++;
			JOptionPane.showMessageDialog(null, "此局魏兄胜出");
		}
		if (index1 == 2 && index2 == 1) {
			score2++;
			JOptionPane.showMessageDialog(null, "此局魏兄胜出");
		}
		if (index1 == 2 && index2 == 2) {
			JOptionPane.showMessageDialog(null, "此局平局");
		}
		if (index1 == 2 && index2 == 3) {
			score1++;
			JOptionPane.showMessageDialog(null, "此局机兄胜出");
		}
		if (index1 == 3 && index2 == 1) {
			score1++;
			JOptionPane.showMessageDialog(null, "此局机兄胜出");
		}
		if (index1 == 3 && index2 == 2) {
			score2++;
			JOptionPane.showMessageDialog(null, "此局魏兄胜出");
		}
		if (index1 == 3 && index2 == 3) {
			JOptionPane.showMessageDialog(null, "此局平局");
		}

	}

	public void finalJudge() {
		if (score1 > score2) {
			JOptionPane.showMessageDialog(null, "结果为:机兄胜利");
		} else if (score1 < score2) {
			JOptionPane.showMessageDialog(null, "结果为:魏兄胜利");
		} else {
			JOptionPane.showMessageDialog(null, "结果为:平局");
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
		if (e.getSource() == play01) {
			if(round1) {
				judge();
				round1=false;
			}	
		}

		if (e.getSource() == play02) {
			if(round2) {
				judge();
				round2=false;
			}	
		}

		if (e.getSource() == play03) {
			if(round3) {
				judge();
				round3=false;
			}			
		}
		if(!round1&&!round2&&!round3) {
			finalJudge();
			round1=round2=round3=true;
			score1=score2=0;
		}

	}

	@Override
	public void paint(Graphics g) {
// TODO Auto-generated method stub
		super.paint(g);
		g.setColor(Color.BLACK);
		g.setFont(new Font("楷体", Font.BOLD, 30));
		g.drawImage(image1, 0, 20, image1.getWidth(), image1.getHeight(), null);
		g.drawImage(image2, 1030, 20, image2.getWidth(), image2.getHeight(), null);
		g.drawString("机兄分数:" + score1, 250, 300);
		g.drawString("魏兄分数:" + score2, 850, 300);

	}
}

大神,我想再请教您一下,麻烦看一下私信,我只能发三条,你帮了我这么多,我能做些什么不
dkwuxiang 2020-06-18
  • 打赏
  • 举报
回复
这么感动,得好好请人家吃顿饭,看场电影
吴溪啊 2020-06-18
  • 打赏
  • 举报
回复
引用 2 楼 qq_39936465的回复:

public class GameInterface extends JFrame implements ActionListener {
	JLabel lblBackground;
	static JButton play01;
	static JButton play02;
	static JButton play03;

	int index1;
	int index2;
	int score1;
	int score2;
	BufferedImage image1;
	BufferedImage image2;
	BufferedImage[] image;
	boolean round1=true,round2=true,round3=true;

	public GameInterface() {
// TODO Auto-generated constructor stub
//设置标题
		setTitle("游戏主界面");
//位置居中
		setLocation(200, 100);
//设置大小
		setSize(1200, 500);
//设置退出方式
		setDefaultCloseOperation(EXIT_ON_CLOSE);

//加载背景图片
		ImageIcon imageIcon = new ImageIcon("src/image/游戏主界面.jpg");
		lblBackground = new JLabel();
//设置图片大小
		lblBackground.setBounds(0, 0, 1200, 500);
		lblBackground.setLayout(null);
		this.add(lblBackground);

		play01 = new JButton("round 1");
		play01.setBounds(500, 100, 150, 50);
		lblBackground.add(play01);
		play01.addActionListener(this);

		play02 = new JButton("round 2");
		play02.setBounds(500, 200, 150, 50);
		lblBackground.add(play02);
		play02.addActionListener(this);

		play03 = new JButton("round 3");
		play03.setBounds(500, 300, 150, 50);
		lblBackground.add(play03);
		play03.addActionListener(this);
		
		for(int i=1;i<4;i++) {
				image[i] = App.getImg("/image/0"+i+".jpg");
		}
		image1=image[1];
		image2=image[1];

		setLayout(null);
		setVisible(true);

	}

	public static void main(String[] args) {
		GameInterface g = new GameInterface();
	}

	public void judge() {

		Random r = new Random();
		index1 = r.nextInt(3) + 1;
		index2 = r.nextInt(3) + 1;

	    image1 = image[index1];
	    image2 = image[index2];

		repaint();

		if (index1 == 1 && index2 == 1) {
			JOptionPane.showMessageDialog(null, "此局平局");
		}
		if (index1 == 1 && index2 == 2) {
			score1++;
			JOptionPane.showMessageDialog(null, "此局机兄胜出");
		}
		if (index1 == 1 && index2 == 3) {
			score2++;
			JOptionPane.showMessageDialog(null, "此局魏兄胜出");
		}
		if (index1 == 2 && index2 == 1) {
			score2++;
			JOptionPane.showMessageDialog(null, "此局魏兄胜出");
		}
		if (index1 == 2 && index2 == 2) {
			JOptionPane.showMessageDialog(null, "此局平局");
		}
		if (index1 == 2 && index2 == 3) {
			score1++;
			JOptionPane.showMessageDialog(null, "此局机兄胜出");
		}
		if (index1 == 3 && index2 == 1) {
			score1++;
			JOptionPane.showMessageDialog(null, "此局机兄胜出");
		}
		if (index1 == 3 && index2 == 2) {
			score2++;
			JOptionPane.showMessageDialog(null, "此局魏兄胜出");
		}
		if (index1 == 3 && index2 == 3) {
			JOptionPane.showMessageDialog(null, "此局平局");
		}

	}

	public void finalJudge() {
		if (score1 > score2) {
			JOptionPane.showMessageDialog(null, "结果为:机兄胜利");
		} else if (score1 < score2) {
			JOptionPane.showMessageDialog(null, "结果为:魏兄胜利");
		} else {
			JOptionPane.showMessageDialog(null, "结果为:平局");
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
		if (e.getSource() == play01) {
			if(round1) {
				judge();
				round1=false;
			}	
		}

		if (e.getSource() == play02) {
			if(round2) {
				judge();
				round2=false;
			}	
		}

		if (e.getSource() == play03) {
			if(round3) {
				judge();
				round3=false;
			}			
		}
		if(!round1&&!round2&&!round3) {
			finalJudge();
			round1=round2=round3=true;
			score1=score2=0;
		}

	}

	@Override
	public void paint(Graphics g) {
// TODO Auto-generated method stub
		super.paint(g);
		g.setColor(Color.BLACK);
		g.setFont(new Font("楷体", Font.BOLD, 30));
		g.drawImage(image1, 0, 20, image1.getWidth(), image1.getHeight(), null);
		g.drawImage(image2, 1030, 20, image2.getWidth(), image2.getHeight(), null);
		g.drawString("机兄分数:" + score1, 250, 300);
		g.drawString("魏兄分数:" + score2, 850, 300);

	}
}

哇😭,好感动啊,谢谢您
qq_39936465 2020-06-18
  • 打赏
  • 举报
回复

public class GameInterface extends JFrame implements ActionListener {
	JLabel lblBackground;
	static JButton play01;
	static JButton play02;
	static JButton play03;

	int index1;
	int index2;
	int score1;
	int score2;
	BufferedImage image1;
	BufferedImage image2;
	BufferedImage[] image;
	boolean round1=true,round2=true,round3=true;

	public GameInterface() {
// TODO Auto-generated constructor stub
//设置标题
		setTitle("游戏主界面");
//位置居中
		setLocation(200, 100);
//设置大小
		setSize(1200, 500);
//设置退出方式
		setDefaultCloseOperation(EXIT_ON_CLOSE);

//加载背景图片
		ImageIcon imageIcon = new ImageIcon("src/image/游戏主界面.jpg");
		lblBackground = new JLabel();
//设置图片大小
		lblBackground.setBounds(0, 0, 1200, 500);
		lblBackground.setLayout(null);
		this.add(lblBackground);

		play01 = new JButton("round 1");
		play01.setBounds(500, 100, 150, 50);
		lblBackground.add(play01);
		play01.addActionListener(this);

		play02 = new JButton("round 2");
		play02.setBounds(500, 200, 150, 50);
		lblBackground.add(play02);
		play02.addActionListener(this);

		play03 = new JButton("round 3");
		play03.setBounds(500, 300, 150, 50);
		lblBackground.add(play03);
		play03.addActionListener(this);
		
		for(int i=1;i<4;i++) {
				image[i] = App.getImg("/image/0"+i+".jpg");
		}
		image1=image[1];
		image2=image[1];

		setLayout(null);
		setVisible(true);

	}

	public static void main(String[] args) {
		GameInterface g = new GameInterface();
	}

	public void judge() {

		Random r = new Random();
		index1 = r.nextInt(3) + 1;
		index2 = r.nextInt(3) + 1;

	    image1 = image[index1];
	    image2 = image[index2];

		repaint();

		if (index1 == 1 && index2 == 1) {
			JOptionPane.showMessageDialog(null, "此局平局");
		}
		if (index1 == 1 && index2 == 2) {
			score1++;
			JOptionPane.showMessageDialog(null, "此局机兄胜出");
		}
		if (index1 == 1 && index2 == 3) {
			score2++;
			JOptionPane.showMessageDialog(null, "此局魏兄胜出");
		}
		if (index1 == 2 && index2 == 1) {
			score2++;
			JOptionPane.showMessageDialog(null, "此局魏兄胜出");
		}
		if (index1 == 2 && index2 == 2) {
			JOptionPane.showMessageDialog(null, "此局平局");
		}
		if (index1 == 2 && index2 == 3) {
			score1++;
			JOptionPane.showMessageDialog(null, "此局机兄胜出");
		}
		if (index1 == 3 && index2 == 1) {
			score1++;
			JOptionPane.showMessageDialog(null, "此局机兄胜出");
		}
		if (index1 == 3 && index2 == 2) {
			score2++;
			JOptionPane.showMessageDialog(null, "此局魏兄胜出");
		}
		if (index1 == 3 && index2 == 3) {
			JOptionPane.showMessageDialog(null, "此局平局");
		}

	}

	public void finalJudge() {
		if (score1 > score2) {
			JOptionPane.showMessageDialog(null, "结果为:机兄胜利");
		} else if (score1 < score2) {
			JOptionPane.showMessageDialog(null, "结果为:魏兄胜利");
		} else {
			JOptionPane.showMessageDialog(null, "结果为:平局");
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
		if (e.getSource() == play01) {
			if(round1) {
				judge();
				round1=false;
			}	
		}

		if (e.getSource() == play02) {
			if(round2) {
				judge();
				round2=false;
			}	
		}

		if (e.getSource() == play03) {
			if(round3) {
				judge();
				round3=false;
			}			
		}
		if(!round1&&!round2&&!round3) {
			finalJudge();
			round1=round2=round3=true;
			score1=score2=0;
		}

	}

	@Override
	public void paint(Graphics g) {
// TODO Auto-generated method stub
		super.paint(g);
		g.setColor(Color.BLACK);
		g.setFont(new Font("楷体", Font.BOLD, 30));
		g.drawImage(image1, 0, 20, image1.getWidth(), image1.getHeight(), null);
		g.drawImage(image2, 1030, 20, image2.getWidth(), image2.getHeight(), null);
		g.drawString("机兄分数:" + score1, 250, 300);
		g.drawString("魏兄分数:" + score2, 850, 300);

	}
}

qq_39936465 2020-06-18
  • 打赏
  • 举报
回复
image1和image2 没有初始化 先设置一个BufferImage数组,变量名为image 先把3张图片在类构造器全部加入 image这个数组

for(int i=1;i<4;i++) {
				image[i] = App.getImg("/image/0"+i+".jpg");
		}
image1=image[1];
image2=image[1];

在judge方法中修改成下面语句

image1=image[index1];
image2=image[index2];

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧