2017-07-03 18 views
0

我正在創建一個二十一點遊戲,並且我試圖在點擊按鈕被擊中時讓牌在桌面圖像上出現。但是,他們一直顯示在表格圖像的旁邊,並且只顯示a。當我在ActionListener中使用pack()方法時,或者如果我不使用pack(),當調整窗口大小時。當我使用PaintComponent到我的JFrame時,除非使用包,否則我必須調整窗口大小才能顯示它。我該如何補救?

我的代碼:

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代碼:

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

} 
...} 

我嘗試使用重繪(),和BC我得到一個編譯錯誤,我不能使用塗料。我該如何解決這個問題?

+1

以'this.setVisible(true);'使它成爲您建立UI後調用的最後一件事 - 這是一個相對常見的問題。 Swing對它的更新很懶,所以如果你想在界面上實現動態更新UI,你需要調用'revalidate'和'repaint'來觸發一個佈局和paint pass – MadProgrammer

+0

在哪裏添加這些方法重新驗證和重繪)?我試圖在DecisionListener重繪,沒有任何反應。我只是做了重新驗證,並且比包更好,但我希望這些卡出現在桌面上,而不是旁邊。 – Derry

+0

'revalidate'和'repaint'需要在被更改的容器上進行,這可能是框架的'contentPane' - *「,但我希望這些卡片出現在桌子上,而不是旁邊的」* - 將會是佈局管理器的問題 – MadProgrammer

回答

2

我該如何放置這些卡OVER,ON TOP OF,表格圖像?

您需要的部件的層次是這樣的:

  • 的JFrame
    • 背景分量與圖像
      • 與圖像
  • 卡組件

的一種方法是使用標籤來包含你的圖像:

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

如果背景圖像是比你的卡組件做大這隻會工作。

+0

優秀!非常感謝。 – Derry

相關問題