当我对 JFrame 使用 PaintComponent 时,除非我使用 pack,否则我必须调整窗口大小才能显示它.我该如何补救? [英] When I use an PaintComponent to my JFrame, I have to resize my window to get it to show up unless I use pack. How can I remedy this?

查看:26
本文介绍了当我对 JFrame 使用 PaintComponent 时,除非我使用 pack,否则我必须调整窗口大小才能显示它.我该如何补救?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 21 点游戏,并且我正在尝试在点击命中按钮时让卡片显示在桌面图像的顶部.但是,它们一直出现在桌子图像的一侧,并且只出现一个.当我在 ActionListener 中使用 pack() 方法时,或者如果我不使用 pack(),当我调整窗口大小时.

I am creating a Blackjack game, and I'm trying to get cards to show up on top of the table image when the hit button is hit. However, they keep showing up to the side of the table image, and only either show up a. when I use the pack() method in the ActionListener, or if I don't use pack(), when I resize the window.

我的代码:

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


public class BlackjackTable3 extends JFrame {


  JButton stayButton = new JButton("STAY");
  JButton hitButton = new JButton("HIT");
  JPanel mainPanel = new JPanel();

  public BlackjackTable3() {
    this.setTitle("Blackjack Test Table");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel tablePanel = new JPanel();
    ImageIcon pic = new ImageIcon("blackjackTableCanvas.jpg");
    mainPanel.add(new JLabel(pic), BorderLayout.NORTH);




    this.add(mainPanel);
    this.setSize(1600,1600);
    this.setVisible(true);

    JPanel buttonPanel = new JPanel();
    ActionListener pressChoice = new DecisionListener();
    hitButton.addActionListener(pressChoice);
    stayButton.addActionListener(pressChoice);

    buttonPanel.setSize(300,150);
    buttonPanel.add(hitButton,BorderLayout.WEST);
   buttonPanel.add(stayButton,BorderLayout.EAST);
    buttonPanel.setVisible(true);
    this.add(buttonPanel, BorderLayout.SOUTH);

  }



  class DecisionListener implements ActionListener{

    public void actionPerformed(ActionEvent a){
      //code for hit/stay to go here

      if(a.getSource() == hitButton){
        System.out.println("YOU CHOSE HIT!");
        CardRender2 c = new CardRender2(new Card());

        mainPanel.add(c, BorderLayout.CENTER);
        pack();
      }
      else if(a.getSource() == stayButton){
        System.out.println("YOU CHOSE STAY!");
      }

    }
  }



  public static void main(String[] args){
    BlackjackTable3 b = new BlackjackTable3();

  }

}

我的 CardRender2 代码:

My CardRender2 code:

public class CardRender2 extends JComponent{ 
public CardRender2(Card card) {
  this.val = card.value.face;
    this.suit = card.suit.toString();
    String filename = this.fetchCardFileLabel();
    try {
        image = ImageIO.read(new File("card deck\\" + filename + ".png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    JLabel j = new JLabel();
    j.add(this);

}

@Override
protected void paintComponent(Graphics g) {
  Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g2);
    g2.drawImage(image, 0, 0, null);

}
...}

我尝试使用 repaint(),但我无法使用 Paint bc 我收到编译器错误.我该如何解决这个问题?

I tried using repaint(), and I can't use paint bc I get a compiler error. How can I fix this issue?

推荐答案

如何将这些卡片放在桌子图像的上方、上方?

how can I place these cards OVER, ON TOP OF, the table image?

你需要一个像这样的组件层次结构:

You need a hierarchy of components something like:

  • JFrame
    • 带有图像的背景组件
      • 带有图片的卡片组件

      一种方法是使用标签来包含您的图像:

      One way is to use labels to contain your images:

      JLabel card = new JLabel(...);
      JLabel background = new JLabel(...);
      background.setLayout( new FlowLayout() );
      background.add( card );
      frame.add(background, BorderLayout.CENTER);
      

      这仅在背景图像大于您的卡片组件时才有效.

      This will only work if the background image is bigger than your card component.

      这篇关于当我对 JFrame 使用 PaintComponent 时,除非我使用 pack,否则我必须调整窗口大小才能显示它.我该如何补救?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆