jbutton中的java映像正在相互覆盖要将两个图像相互重叠显示吗

cdmah0mi  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(155)

我打算做一个游戏,一个婴儿1穿过马路,通过一个小男孩2遇到另一个婴儿 JButton (行动)。但当我用act键直到宝宝1碰到宝宝2,宝宝2就消失了。我要把它们一个一个地展示出来。

package activity;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FacGrid extends JFrame implements ActionListener {

    JButton t[][] = new JButton[9][9];
    int baby1_posX = 6, baby1_posY = 2;
    int baby2_posX = 6, baby2_posY = 8;
    int ball_posX = 6, ball_posY = 3;
    int road_posX = 0, road_posY = 4;
    ImageIcon road = new ImageIcon("./image/road.jpg");
    ImageIcon baby1 = new ImageIcon("./image/baby 1.png");
    ImageIcon baby 2 = new ImageIcon("./image/baby2.png");
    ImageIcon ball = new ImageIcon("./image/ball.png");
    public static void main(String[] args) {
        Fac frame= new Fac();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900,700);
        frame.setTitle("GUI");
        frame.setResizable(false);
        frame.createGUI();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

    }
    void createGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(null);

        JPanel Panel =new JPanel();
        Panel.setBounds(30,20,350,300);

        Panel.setLayout(new GridLayout(9,9));
        Panel.setBackground(Color.WHITE);
        for(int i=0 ; i<9 ; i++) {
            for(int j=0 ; j<9 ; j++) {
                JButton tile = new JButton();
                t[i][j] = tile;
                Panel.add(t[i][j]);
                tile.setBorderPainted(false);
                tile.setBackground(Color.WHITE);

            }

        }

        t[baby1_posX][baby1_posY].setIcon(baby1);
        t[baby2_posX][baby2_posY].setIcon(baby2);
        t[ball_posX][ball_posY].setIcon(ball);

        for(int road_posX=0 ; road_posX<9 ; road_posX++ ) {
            t[road_posX][road_posY].setIcon(road);

        }

        window.add(Panel);

        JButton button = new JButton ("Act");
        button.setBounds(700, 20, 80, 30);
        button.addActionListener(this);
        window.add(button);

    }
    @Override
    public void actionPerformed(ActionEvent arg0) {

        t[ball_posX][ball_posY].setIcon(null);
        ball_posY=ball_posY+1;

        t[ball_posX][ball_posY].setIcon(ball);

    }
}

列表项

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题