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

时间:2016-03-18 03:10:57

标签: java swing user-interface jpanel

我正在处理的项目要求用户创建一个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)不会显示。 ImageIcons都指向正确的文件名,我仔细检查过。他们只是没有出现在面板中。我错过了什么?这是输出:

Ouput

1 个答案:

答案 0 :(得分:0)

你要添加到边框布局。所以你必须指定边框布局的位置,你想要添加组件。默认情况下它会添加到中心。否则你不能将2个或更多组件添加到同一个位置。

所以你的代码中的问题没有指定position.you正在添加2个组件到中心。

修复此问题指定像这样的位置

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