无法在 Java 的图像面板中显示图像

Unable to get images to display in Image Panel in Java

我正在做的项目要求用户创建一个 GUI,从洗好的牌组中向用户发五张牌(使用链表,但这部分并不重要,至少我不这么认为)。到目前为止,我已经设法使机制正常工作,但 GUI 只是拒绝工作。我从讲师的示例中复制粘贴了该示例,该示例向我们展示了如何显示图像,但即使程序运行,当我单击 'Deal cards' 按钮时,也没有任何反应。

代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package projectone;

import java.util.Arrays;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Geoff
 */
public class CardDealer extends JFrame {

   private JPanel imagePanel;     // To hold the label
   private JPanel buttonPanel;    // To hold a button
   private JLabel imageLabelOne, imageLabelTwo, imageLabelThree, 
   imageLabelFour, imageLabelFive;     // To show an image
   private JButton button;        // To get an image
   public static Object[] theHand; //five cards hand


public static void main(String[] args) 
{






   LinkedStack theDeck = new LinkedStack();

   theDeck = theDeck.theCards();





    System.out.println("List print test\n");
    System.out.println(theDeck.toString());


    theDeck.shuffle(theDeck);
    System.out.println("List shuffle print test\n");
    System.out.println(theDeck.toString());

    Object[] hand =
    {
    theDeck.pop(),
    theDeck.pop(),
    theDeck.pop(),
    theDeck.pop(),
    theDeck.pop(),       
    };


    System.out.println("Hand print test\n");
    System.out.println(Arrays.toString(hand));

    theHand = hand;

   new CardDealer();
   }
   /**
  Constructor
   */

   public CardDealer()
   {
  // Set the title.
  setTitle("Card Dealer");

  // Specify an action for the close button.
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // Create a BorderLayout manager.
  setLayout(new BorderLayout());
  setPreferredSize(new Dimension(600, 400));

  // Build the panels.
  buildImagePanel();
  buildButtonPanel();

  // Add the panels to the content pane.
  add(imagePanel);
  add(buttonPanel);

  // Pack and display the window.
  pack();
  setVisible(true);
}

/**
  The buildImagePanel method adds a label to a panel.
*/

private void buildImagePanel()
{
   // Create a panel.
   imagePanel = new JPanel();
   imagePanel.setPreferredSize(new Dimension(600, 400));
   imagePanel.setLayout(new GridLayout(2, 5));

  // Create a label.
  imageLabelOne = new JLabel();
  imageLabelTwo = new JLabel();
  imageLabelThree = new JLabel();
  imageLabelFour = new JLabel();
  imageLabelFive = new JLabel();

  // Add the label to the panel.
  imagePanel.add(imageLabelOne);
  imagePanel.add(imageLabelTwo);
  imagePanel.add(imageLabelThree);
  imagePanel.add(imageLabelFour);
  imagePanel.add(imageLabelFive);
}

/**
  The buildButtonPanel method adds a button
  to a panel.
*/

private void buildButtonPanel()
{

  // Create a panel.
  buttonPanel = new JPanel();


  // Create a button.
  button = new JButton("Deal");



  // Register an action listener with the button.
  button.addActionListener(new ButtonListener());

  // Add the button to the panel.
  buttonPanel.add(button);
}

/**
  Private inner class that handles the event when
  the user clicks the button.
*/

private class ButtonListener implements ActionListener
{
  public void actionPerformed(ActionEvent e)
  {
     // Read the image file into an ImageIcon object.
      ImageIcon card1, card2, card3, card4, card5;
       card1 = new ImageIcon(theHand[0].toString() + ".jpg");
       card2 = new ImageIcon(theHand[1].toString() + ".jpg");
       card3 = new ImageIcon(theHand[2].toString() + ".jpg");
       card4 = new ImageIcon(theHand[3].toString() + ".jpg");
       card5 = new ImageIcon(theHand[4].toString() + ".jpg");

     // Display the image in the label.
     imageLabelOne.setIcon(card1);
     imageLabelTwo.setIcon(card2);
     imageLabelThree.setIcon(card3);
     imageLabelFour.setIcon(card4);
     imageLabelFive.setIcon(card5);



     // Pack the frame again to accomodate the 
     // new size of the label.
     pack();
  }
 }



}

问题是,当我单击'Deal' 按钮时,五个图像(card1 到card5)没有显示。 ImageIcons 都指向正确的文件名,我仔细检查过。他们只是不出现在面板中。我错过了什么?这是输出:

Ouput

您正在添加到边框布局。因此您必须指定边框布局的位置,您希望添加组件。默认情况下它添加到中心。您也不能将 2 个或更多组件添加到同一位置。

因此您未指定 position.you 的代码中的问题是向中心添加 2 个组件。

要解决此问题,请像这样指定位置

add(imagePanel); // same as `BorderLayout.CENTER` (default)
add(buttonPanel,BorderLayout.NORTH);