JPanel没有使用BorderLayout显示在JFrame中

时间:2014-06-13 13:46:03

标签: java swing jpanel jlabel

我有一个使用GridLayout的JPanel,它包含6个JLabel。如果我只将这个JPanel添加到JFrame中,一切正常,但是当我将它添加到BorderLayout.WEST(以及EAST,CENTER和SOUTH上的其他3个面板)时,它将无法显示。

这是我正在使用的代码:

public class SwingView extends JFrame {
    private DeckLabel[] terrains={
            new DeckLabel(new ImageIcon("assets/graphics/mountains.png"),0),
            new DeckLabel(new ImageIcon("assets/graphics/planes.png"),1),
            new DeckLabel(new ImageIcon("assets/graphics/forest.png"),2),
            new DeckLabel(new ImageIcon("assets/graphics/fields.png"),3),
            new DeckLabel(new ImageIcon("assets/graphics/swamp.png"),4),
            new DeckLabel(new ImageIcon("assets/graphics/desert.png"),5)};
    public SwingView() {
        super("Frame");
        this.setSize(680, 740);
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        Utilities.center(this);

        // panels
        //terrains
        JPanel terrainsPanel = new JPanel();
        terrainsPanel.setSize(100, 640);
        terrainsPanel.setLayout(new GridLayout(6, 1));
        //map
        JPanel mapPanel = new JPanel();
        mapPanel.setSize(480, 640);
        //info
        JPanel infoPanel = new JPanel();
        infoPanel.setSize(100, 640);
        //chat
        JPanel chatPanel = new JPanel();
        chatPanel.setSize(680, 100);
        chatView.setEditable(false);
        txtChat.addKeyListener(this);
        chatPanel.add(txtChat, BorderLayout.SOUTH);
        chatPanel.add(chatView);

    // terrains
        for (int i = 0; i < 6; i++) {
        terrainsPanel.add(terrains[i]);
        }
        this.add(mapPanel,BorderLayout.CENTER);
        this.add(terrainsPanel, BorderLayout.WEST);
        this.add(infoPanel, BorderLayout.EAST);
        this.add(chatPanel, BorderLayout.SOUTH);

    }
}

public class DeckLabel extends JLabel{
    private Image image;
    private int index;
    public DeckLabel(ImageIcon icon,int index){
        this.image=icon.getImage();
        this.index=index;
    }

    @Override
    public void paint(Graphics g){
        BufferedImage bi = new BufferedImage(85, 78,
                    BufferedImage.TYPE_INT_RGB);
        Graphics2D tg = bi.createGraphics();
        tg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        // draw basic tile
        tg.drawImage(image, 0, 0, null);
        g.drawImage(bi,0,0,null);
    }
}

谢谢

2 个答案:

答案 0 :(得分:3)

很多问题

  • 使用JPanel而不是JLabel,
  • 覆盖paintComponent而不是JLabel / JPanel / Swing JComponents的paint
  • 第一。代码行应该是super.pain(Component),或者是JPanel停止重绘(鼠标和键事件)
  • 或使用JLabel.setIcon for Image
  • 来自Utilities.center(this);的错误,你试图布局容器
  • FlowLayout是JPanel的默认LayoutManager,然后是chatPanel.add(txtChat,BorderLayout.SOUTH);被忽略而不会丢失代码行chatPanel.setLayout(new BorderLayout)
  • txtChat.addKeyListener(本);我希望不是一些JTextComponents,如果是,那么使用DocumentListener / Filter

答案 1 :(得分:1)

由于您的DeckLabel类没有为JLabel设置任何文本,因此该组件没有最小和首选大小。因此边框布局将采用0 - >的大小。组件不可见。使用JPanel时会发生同样的情况。

调用setPreferredSize(),setMinimumSize()或覆盖getMinimumSize()/ getPreferredSize()。