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

标签 java swing user-interface jpanel

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

这是代码:

/*
* 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();
  }
 }



}

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

Ouput

最佳答案

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

所以代码中的问题是您没有指定位置。您正在向中心添加 2 个组件。

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

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

关于java - 无法在 Java 中的图像面板中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36075452/

相关文章:

java - 控制 Mac 上 JButton 的外观

java - 如何使用在 IntelliJ IDEA 中创建的 GUI 表单

templates - 无法在cloudstack上注册ISO或模板

angularjs - 数据管理/CRUD 生成器 AngularJS

java - 无法生成某个范围内的所有卡普雷卡数

java - Java中JOptionPane的ConfirmDialog

java - 在 Java Swing 中放置 JSeparator 后的间隙大小

matlab - 在 MATLAB 的进程中间停止 GUI

java - 为什么这个 MinDepth 级别的解决方案与递归解决方案相比如此慢?

JavaFX 应用程序类必须扩展 javafx.application.Application