简体   繁体   English

当对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?

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. 当我在ActionListener中使用pack()方法时,或者如果我不使用pack()时,则需要调整窗口大小。

My Code: 我的代码:

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();

  }

}

My CardRender2 code: 我的CardRender2代码:

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);

}
...}

I tried using repaint(), and I can't use paint bc I get a compiler error. 我尝试使用repaint(),但无法使用paint bc,但出现编译器错误。 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 J框架
    • background component with image 带有图像的背景组件
      • card component with image 带有图像的卡片组件

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. 仅当背景图像大于卡组件时,此功能才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我必须调整我的JFrame大小以显示内容。 我该如何解决这个问题? - I have to resize my JFrame for the contents to show up. How can i fix that? 如何将TopComponent用作JFrame? - How can I use my TopComponent as a JFrame? 除非最小化或调整JFrame的大小,否则我不能将其称为KeyEvent事件 - I can't call an event, a KeyEvent, unless I minimize or resize my JFrame 为什么我不能使用我的paintComponent? - Why am I unable to use my paintComponent? 我如何使用 JFrame.pack()? - How do I use JFrame.pack()? 如果没有此类,如何用ant / javac编译“ import pack。*”? - How can I compile “import pack.*” with ant/javac, when there are no such classes? 关闭用于更新数据库信息的窗口后,如何从数据库更新主JFrame中的列表 - How can I update my List in my main JFrame from a database after closing a the window I use to update the db info 如何在JFrame中调整这些面板的大小? - How do I resize these panels in my JFrame? 为什么在更改组件时必须在我的JFrame上使用setvisible()? - Why do I have to use setvisible() on my JFrame when I change components? 如果我使用paintComponent,我如何显示一个JLabel? - If I use paintComponent, how do I show a JLabel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM