这是用Java程序编的一个配对游戏但怎么修改程序才能使游戏运行
publicclasslianxi8extendsJFrameimplementsMouseListener,MouseMotionListener{publicstat...
public class lianxi8 extends JFrame implements MouseListener,MouseMotionListener{
public static void main(String[] args) {
private static final boolean fasle = false;
private JLabel img[]=new JLabel[5];
private JLabel targets[]=new JLabel[5];
private Point pressPoint;
public lianxi8(){
super("简易配对游戏");
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel imagePanel=new JPanel();
imagePanel.setLayout(null);
imagePanel.setOpaque(false);
setGlassPane(imagePanel);
getGlassPane().setVisible(true);
ImageIcon icon[]=new ImageIcon[5];
icon[0]=new ImageIcon(getClass().getResource("kafei.png"));
icon[1]=new ImageIcon(getClass().getResource("xianshiqi.png"));
icon[2]=new ImageIcon(getClass().getResource("xiyiji.png"));
icon[3]=new ImageIcon(getClass().getResource("yifu.png"));
icon[4]=new ImageIcon(getClass().getResource("zixingche.png"));
final JPanel bottomPanel=new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER,20,5));
this.add(bottomPanel,BorderLayout.SOUTH);
for(int i=0;i<5;i++){
img[i]=new JLabel(icon[i]);
img[i].setSize(600,600);
img[i].setBorder(new LineBorder(Color.BLUE));
int x=(int)(Math.random()*(getWidth()-50));
int y=(int)(Math.random()*(getHeight()-150));
img[i].setLocation(x, y);
img[i].addMouseListener(this);
imagePanel.add(img[i]);
targets[i]=new JLabel();
targets[i].setOpaque(true);
targets[i].setBackground(Color.BLUE);
targets[i].setHorizontalTextPosition(SwingConstants.CENTER);
targets[i].setVerticalTextPosition(SwingConstants.BOTTOM);
targets[i].setPreferredSize(new Dimension(80,80));
targets[i].setHorizontalAlignment(SwingConstants.CENTER);
bottomPanel.add(targets[i]);
}
targets[0].setText("咖啡");
targets[1].setText("显示器");
targets[2].setText("洗衣机");
targets[3].setText("衣服");
targets[4].setText("自行车");
}
public void mousePressed(MouseEvent e){
pressPoint=e.getPoint(); }
public void mouseReleased(MouseEvent e){
if(Check()){
getGlassPane().setVisible(true);
for(int i=0;i<5;i++){
targets[i].setText("配对成功"); } }}
private boolean Check(){
boolean result=true;
for(int i=0;i<5;i++){
Point location=img[i].getLocationOnScreen();
Point seat=targets[i].getLocationOnScreen();
targets[i].setBackground(Color.orange);
if(location.x<seat.x|| location.y<seat.y
|| location.x>seat.x+80 || location.y>seat.y+80 ) {
targets[i].setBackground(Color.blue);
result=fasle;
}
}}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
JLabel sourse=(JLabel)e.getSource();
Point imgPoint=sourse.getLocation();
Point point=e.getPoint();
sourse.setLocation(imgPoint.x+point.x-pressPoint.x,pressPoint.
y+point.y-pressPoint.y);}}} 展开
public static void main(String[] args) {
private static final boolean fasle = false;
private JLabel img[]=new JLabel[5];
private JLabel targets[]=new JLabel[5];
private Point pressPoint;
public lianxi8(){
super("简易配对游戏");
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel imagePanel=new JPanel();
imagePanel.setLayout(null);
imagePanel.setOpaque(false);
setGlassPane(imagePanel);
getGlassPane().setVisible(true);
ImageIcon icon[]=new ImageIcon[5];
icon[0]=new ImageIcon(getClass().getResource("kafei.png"));
icon[1]=new ImageIcon(getClass().getResource("xianshiqi.png"));
icon[2]=new ImageIcon(getClass().getResource("xiyiji.png"));
icon[3]=new ImageIcon(getClass().getResource("yifu.png"));
icon[4]=new ImageIcon(getClass().getResource("zixingche.png"));
final JPanel bottomPanel=new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER,20,5));
this.add(bottomPanel,BorderLayout.SOUTH);
for(int i=0;i<5;i++){
img[i]=new JLabel(icon[i]);
img[i].setSize(600,600);
img[i].setBorder(new LineBorder(Color.BLUE));
int x=(int)(Math.random()*(getWidth()-50));
int y=(int)(Math.random()*(getHeight()-150));
img[i].setLocation(x, y);
img[i].addMouseListener(this);
imagePanel.add(img[i]);
targets[i]=new JLabel();
targets[i].setOpaque(true);
targets[i].setBackground(Color.BLUE);
targets[i].setHorizontalTextPosition(SwingConstants.CENTER);
targets[i].setVerticalTextPosition(SwingConstants.BOTTOM);
targets[i].setPreferredSize(new Dimension(80,80));
targets[i].setHorizontalAlignment(SwingConstants.CENTER);
bottomPanel.add(targets[i]);
}
targets[0].setText("咖啡");
targets[1].setText("显示器");
targets[2].setText("洗衣机");
targets[3].setText("衣服");
targets[4].setText("自行车");
}
public void mousePressed(MouseEvent e){
pressPoint=e.getPoint(); }
public void mouseReleased(MouseEvent e){
if(Check()){
getGlassPane().setVisible(true);
for(int i=0;i<5;i++){
targets[i].setText("配对成功"); } }}
private boolean Check(){
boolean result=true;
for(int i=0;i<5;i++){
Point location=img[i].getLocationOnScreen();
Point seat=targets[i].getLocationOnScreen();
targets[i].setBackground(Color.orange);
if(location.x<seat.x|| location.y<seat.y
|| location.x>seat.x+80 || location.y>seat.y+80 ) {
targets[i].setBackground(Color.blue);
result=fasle;
}
}}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
JLabel sourse=(JLabel)e.getSource();
Point imgPoint=sourse.getLocation();
Point point=e.getPoint();
sourse.setLocation(imgPoint.x+point.x-pressPoint.x,pressPoint.
y+point.y-pressPoint.y);}}} 展开
2个回答
2014-06-01
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class test extends JFrame implements MouseListener, MouseMotionListener {
private static final boolean fasle = false;
private JLabel img[] = new JLabel[5];
private JLabel targets[] = new JLabel[5];
private Point pressPoint;
public test() {
super("简易配对游戏");
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel imagePanel = new JPanel();
imagePanel.setLayout(null);
imagePanel.setOpaque(false);
setGlassPane(imagePanel);
getGlassPane().setVisible(true);
ImageIcon icon[] = new ImageIcon[5];
icon[0] = new ImageIcon(getClass().getResource("kafei.png"));
icon[1] = new ImageIcon(getClass().getResource("xianshiqi.png"));
icon[2] = new ImageIcon(getClass().getResource("xiyiji.png"));
icon[3] = new ImageIcon(getClass().getResource("yifu.png"));
icon[4] = new ImageIcon(getClass().getResource("zixingche.png"));
final JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5));
this.add(bottomPanel, BorderLayout.SOUTH);
for (int i = 0; i < 5; i++) {
img[i] = new JLabel(icon[i]);
img[i].setSize(600, 600);
img[i].setBorder(new LineBorder(Color.BLUE));
int x = (int) (Math.random() * (getWidth() - 50));
int y = (int) (Math.random() * (getHeight() - 150));
img[i].setLocation(x, y);
img[i].addMouseListener(this);
imagePanel.add(img[i]);
targets[i] = new JLabel();
targets[i].setOpaque(true);
targets[i].setBackground(Color.BLUE);
targets[i].setHorizontalTextPosition(SwingConstants.CENTER);
targets[i].setVerticalTextPosition(SwingConstants.BOTTOM);
targets[i].setPreferredSize(new Dimension(80, 80));
targets[i].setHorizontalAlignment(SwingConstants.CENTER);
bottomPanel.add(targets[i]);
}
targets[0].setText("咖啡");
targets[1].setText("显示器");
targets[2].setText("洗衣机");
targets[3].setText("衣服");
targets[4].setText("自行车");
}
public void mousePressed(MouseEvent e) {
pressPoint = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
if (Check()) {
getGlassPane().setVisible(true);
for (int i = 0; i < 5; i++) {
targets[i].setText("配对成功");
}
}
}
private boolean Check() {
boolean result = true;
for (int i = 0; i < 5; i++) {
Point location = img[i].getLocationOnScreen();
Point seat = targets[i].getLocationOnScreen();
targets[i].setBackground(Color.orange);
if (location.x < seat.x || location.y < seat.y
|| location.x > seat.x + 80 || location.y > seat.y + 80) {
targets[i].setBackground(Color.blue);
result = fasle;
}
}
return result;
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
JLabel sourse = (JLabel) e.getSource();
Point imgPoint = sourse.getLocation();
Point point = e.getPoint();
sourse.setLocation(imgPoint.x + point.x - pressPoint.x, pressPoint.y
+ point.y - pressPoint.y);
}
public static void main(String[] args) {
test t = new test();
}
}
//我没有图片 报空指针 你在你那跑一下 应该能跑起来
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class test extends JFrame implements MouseListener, MouseMotionListener {
private static final boolean fasle = false;
private JLabel img[] = new JLabel[5];
private JLabel targets[] = new JLabel[5];
private Point pressPoint;
public test() {
super("简易配对游戏");
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel imagePanel = new JPanel();
imagePanel.setLayout(null);
imagePanel.setOpaque(false);
setGlassPane(imagePanel);
getGlassPane().setVisible(true);
ImageIcon icon[] = new ImageIcon[5];
icon[0] = new ImageIcon(getClass().getResource("kafei.png"));
icon[1] = new ImageIcon(getClass().getResource("xianshiqi.png"));
icon[2] = new ImageIcon(getClass().getResource("xiyiji.png"));
icon[3] = new ImageIcon(getClass().getResource("yifu.png"));
icon[4] = new ImageIcon(getClass().getResource("zixingche.png"));
final JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5));
this.add(bottomPanel, BorderLayout.SOUTH);
for (int i = 0; i < 5; i++) {
img[i] = new JLabel(icon[i]);
img[i].setSize(600, 600);
img[i].setBorder(new LineBorder(Color.BLUE));
int x = (int) (Math.random() * (getWidth() - 50));
int y = (int) (Math.random() * (getHeight() - 150));
img[i].setLocation(x, y);
img[i].addMouseListener(this);
imagePanel.add(img[i]);
targets[i] = new JLabel();
targets[i].setOpaque(true);
targets[i].setBackground(Color.BLUE);
targets[i].setHorizontalTextPosition(SwingConstants.CENTER);
targets[i].setVerticalTextPosition(SwingConstants.BOTTOM);
targets[i].setPreferredSize(new Dimension(80, 80));
targets[i].setHorizontalAlignment(SwingConstants.CENTER);
bottomPanel.add(targets[i]);
}
targets[0].setText("咖啡");
targets[1].setText("显示器");
targets[2].setText("洗衣机");
targets[3].setText("衣服");
targets[4].setText("自行车");
}
public void mousePressed(MouseEvent e) {
pressPoint = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
if (Check()) {
getGlassPane().setVisible(true);
for (int i = 0; i < 5; i++) {
targets[i].setText("配对成功");
}
}
}
private boolean Check() {
boolean result = true;
for (int i = 0; i < 5; i++) {
Point location = img[i].getLocationOnScreen();
Point seat = targets[i].getLocationOnScreen();
targets[i].setBackground(Color.orange);
if (location.x < seat.x || location.y < seat.y
|| location.x > seat.x + 80 || location.y > seat.y + 80) {
targets[i].setBackground(Color.blue);
result = fasle;
}
}
return result;
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
JLabel sourse = (JLabel) e.getSource();
Point imgPoint = sourse.getLocation();
Point point = e.getPoint();
sourse.setLocation(imgPoint.x + point.x - pressPoint.x, pressPoint.y
+ point.y - pressPoint.y);
}
public static void main(String[] args) {
test t = new test();
}
}
//我没有图片 报空指针 你在你那跑一下 应该能跑起来
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |