簡體   English   中英

JPanel僅在調整窗口大小(JFrame)時顯示

[英]JPanel only displaying when I resize Window (JFrame)

我有一個帶有JPanel的JFrame,僅當我調整窗口大小時才顯示(使其變小仍會顯示所有內容,因此不適合不是問題)。 我嘗試在之后調用revalidate()和validate(),並確保在添加所有內容后設置setVisible(true),但這似乎無濟於事。

這是我的JFrame代碼(這是一個壟斷游戲):

public class BoardWindow extends JFrame
{

/** Constructor
 * 
 */
Board board;
public BoardWindow(Player[] players)
    {
    super("MONOPOLY GameBoard");

    setLayout(new FlowLayout());

    board = new Board(players);
    add(board);
    add(new JButton("Refresh"));
    setSize(900, 900);

    //setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    }
}

這是我的JFrame代碼

class Board extends JPanel {




public Player players[];

Board(Player[] players)
{
    this.players = players;
    setVisible(true)

}

@Override
public void paintComponent(Graphics g) 
    {
    super.paintComponent(g);

    setPreferredSize(new Dimension(800, 800));

    ImageIcon boardPic = new ImageIcon("images/board.png");

    boardPic.paintIcon(this, g, 0, 0);

    setBackground(Color.WHITE);

    int x = 0;
    int y = 0;

    for(Player i: this.players) 
    {
        g.setColor(i.getColor());
        int position = i.getGamePosition();


        if(position == 0)
        {
            x = 25;
            y = 700;
        }
        else if (position > 0 && position < 9)
        {
            x = 25;
            y = ((10-position)*63)+31;
        }
        else if (position == 9)
        {
            x = 25;
            y = 25;
        }
        else if (position > 9 && position < 18)
        {
            y = 25;
            x = ((position-9)*63)+98;
        }
        else if(position == 18)
        {
            x = 750;
            y = 10;
        }
        else if(position > 18 && position < 27)
        {
            x = 745;
            y = ((position-18)*63)+95;
        }
        else if (position == 27)
        {
            x = 750;
            y = 660;
        }
        else if(position > 27)
        {
            x = ((20-position)*63)+1105; 
            y= 700;
        }


        g.fillRect(x, y, 40, 40);
    }



    }

}

您在paintComponent中做了太多事情。 永遠不要在其中讀入圖像,而應該在一開始就讀入它們,例如在類的構造函數中。 也不要在此方法內設置preferredSize或設置背景。 否則,您可能會陷入束縛方法覆蓋的風險。 此方法應僅用於繪制和繪制,而不能用於其他任何事情。

例如,

class Board extends JPanel {
    public Player players[];
    private BufferedImage boardPic; // use a BufferedImage

    Board(Player[] players) {
        this.players = players;
        setPreferredSize(new Dimension(800, 800));
        setBackground(Color.WHITE);

        // much better to use resources and not files
        // you'll need to handle an exception here.
        boardPic = new ImageIO.read(new File("images/board.png"));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(boardPic, 0, 0, this);

        int x = 0;
        int y = 0;
        for(Player i: this.players) {
            g.setColor(i.getColor());
            int position = i.getGamePosition();

            if(position == 0) {
                x = 25;
                y = 700;
            }  else if (position > 0 && position < 9)  {
                x = 25;
                y = ((10-position)*63)+31;
            }  else if (position == 9)  {
                x = 25;
                y = 25;
            } else if (position > 9 && position < 18) {
                y = 25;
                x = ((position-9)*63)+98;
            } else if(position == 18) {
                x = 750;
                y = 10;
            } else if(position > 18 && position < 27) {
                x = 745;
                y = ((position-18)*63)+95;
            } else if (position == 27) {
                x = 750;
                y = 660;
            } else if(position > 27) {
                x = ((20-position)*63)+1105; 
                y= 700;
            }
            g.fillRect(x, y, 40, 40);
        }
    }
}

更多:

我將使用類似以下內容來讀取圖像:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class MyMonopoly extends JPanel {
    private static final String IMG_PATH = "http://dl.gamesradar.com/photos/gameopoly/monopoly_original.jpg";
    private static final int PREF_W = 900;
    private static final int PREF_H = PREF_W;
    private BufferedImage board = null;

    public MyMonopoly() throws IOException {
        URL url = new URL(IMG_PATH);
        board = ImageIO.read(url);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (board != null) {
            g.drawImage(board, 0, 0, getWidth(), getHeight(), this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        MyMonopoly mainPanel = null;
        try {
            mainPanel = new MyMonopoly();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }

        JFrame frame = new JFrame("My Monopoly");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }
}

只有我將其作為資源保存在JAR文件中。

我還將為JPanel創建一個自定義布局,然后將我的塊精靈放置到ImageIcons中,將它們放置到JLabel中,然后通過其自定義布局將它們移動到我的JPanel中。

暫無
暫無

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

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