簡體   English   中英

每次按下Roll(JButton)時,都會在屏幕上留下一張滾動按鈕的圖片

[英]Every time I pressed Roll(JButton) it left a picture of roll button on

我正在嘗試制作蛇梯游戲。 它還沒有完成(沒有掉頭轉彎,沒有使用梯子和蛇)並且有很多錯誤。

但我的意思是

我發現了一個令我非常好奇的問題(下圖)。 關於令牌移動。 我的策略是在一個大型JPanel(我將其命名為Board)上添加一個[10] [10]數組JPanal(我將其命名為Cell),並將其bg設置為來自蛇和梯子游戲的圖片谷歌,並將布局設置為gridlayout(10,10)。 並且在每個單元格上都有一個隱藏的令牌,只有在按下滾動按鈕和指向該單元格的輸出點時才會顯示。

這是問題發生的地方。

執行時程序的圖像

有時候我按滾動按鈕

每次按下都會顯示一個按鈕!(盡管它們不可單擊。)

我知道我的起點甚至不在左下角,但是jbutton的全部來自哪里!

這是我的主班

public class Main extends JFrame {
    TextField text = new TextField();
    Dice dice = new Dice();
    int tempi = -1, tempj = -1,sum =0;

    //Main Method
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Main mPage = new Main();
                mPage.setVisible(true);
            }
        });
    }

    //Constructor
    public Main(){
        super("Snakes and Ladders");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(1280,768);
        setLocation(400,150);
        setLayout(new FlowLayout(FlowLayout.LEFT,30,100));
        Board board = new Board();
        getContentPane().add(board);
        getContentPane().add(dice);
        getContentPane().add(text);

        //my problem is here.
        dice.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int score = Dice.rollDice();
                text.setText(String.valueOf(score));
                if (tempi != -1 || tempj != -1){
                    board.cell[9-tempi][9-tempj].fade();
                }
                if (tempi == -1 && tempj == -1){
                    sum = sum + score - 1;
                }
                else sum = sum + score;

                tempj = sum%10;
                tempi = (sum - tempj)/10;
                board.cell[9-tempi][9-tempj].reveal();

            }
        });        

        pack();
        setMinimumSize(this.getSize());
    }
}

這是細胞班

public class Cell extends JPanel implements Cloneable {

    private Token pl1 = new Token();

    //constructor
    public Cell(){
        setOpaque(true);
        setBackground(new Color(0,0,0,0));
        setLayout(new GridLayout(2,2));

        this.fade();
        this.add(pl1);
    }

    public void fade(){
        pl1.setVisible(false);
    }

    public void reveal(){
        pl1.setVisible(true);
    }
}

這是令牌類

public class Token extends JLabel {
    private BufferedImage image = null;

    public Token(){
        try {
            image = ImageIO.read(new File("C:\\Users\\myacc\\IdeaProjects\\Snakes and Ladders\\src\\Token.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Image player = image.getScaledInstance(20,20,Image.SCALE_SMOOTH);

        this.setIcon(new ImageIcon(player));

    }
}
setBackground(new Color(0,0,0,0));

不要使用透明的背景。 Swing不知道如何正確繪制透明背景。

為了獲得完全透明,您只需使組件不透明即可:

    //setOpaque(true);
    //setBackground(new Color(0,0,0,0));
    setOpaque(false);

如果需要半透明,則需要自己進行自定義繪畫。 請查看具有透明度的背景,以獲取有關此主題的更多信息。

也不要使用TextField。 那是一個AWT組件。 使用JTextField作為Swing組件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM