setImage()没有设置图像

时间:2014-10-14 00:30:31

标签: java swing

很抱歉提出这样一个基本问题,但我只是学习并坚持将myImage.gif放入面板(或标签)。我有一个带有PictureLabel的PicturePanel。两者都与我的图像大小相同(所以我不在乎它是否在Panel或Label上显示,我只是希望它显示出来)。  我认为我将问题缩小到第57行。 NULL是我需要图像的地方。  我将图像放在文件夹中与图像位于同一目录中的文件夹中。  这是代码:

package examplegui;

import java.awt.*; 
import javax.swing.*;
import java.awt.event.*;
/**
* ExamlpeGUI Problem  
* @author Robert J.Runyan
*/
public class ExampleGUI implements  ActionListener {
    JPanel inputPanel, endUpPanel, picturePanel;
    JLabel endUpLabel, pictureLabel;
    JButton testButton, otherButton;
    JTextField textField;

public JPanel createContentPane() {
    // Create a bottom JPanel to place everything on.
    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    // Create textField for User Input.
    textField = new JTextField(60);
    textField.setLayout(null);
    textField.setLocation(10, 10);
    textField.setSize(160, 30);
    textField.setHorizontalAlignment(0);
    textField.addActionListener(new InputListener()); 
    textField.setBackground(Color.gray);
    totalGUI.add(textField);

    // Create EndUp Panel.
    endUpPanel = new JPanel();
    endUpPanel.setLayout(null);
    endUpPanel.setLocation(10, 50);
    endUpPanel.setSize(160, 30);
    endUpPanel.setBackground(Color.gray);
    totalGUI.add(endUpPanel);

    // Create EndUp Label.
    endUpLabel = new JLabel("User Input ends up here.");
    endUpLabel.setLocation(0, 5);
    endUpLabel.setSize(160, 25);
    endUpLabel.setHorizontalAlignment(0);
    endUpPanel.add(endUpLabel);

    // Create Picture Panel.
    picturePanel = new JPanel();
    picturePanel.setLayout(null);
    picturePanel.setLocation(195, 35);
    picturePanel.setSize(72, 96);
    picturePanel.setBackground(Color.gray);
    totalGUI.add(picturePanel);

    // Create Picture Label. *** Set to an IMAGE ***
    pictureLabel = new JLabel();
    pictureLabel.setIcon(null);  // ******* Null is wrong *******
    // I made the image at line 106 with this:
    // image = new ImageIcon(getClass().getResource("Images/myImage.gif")).getImage();
    pictureLabel.setLocation(0, 0);
    pictureLabel.setSize(72, 96);
    pictureLabel.setHorizontalAlignment(0);
    picturePanel.add(pictureLabel);

    // Create the button.
    testButton = new JButton("Request User Input");
    testButton.setLocation(10, 90);
    testButton.setSize(160, 30);
    testButton.addActionListener(new ButtonListener()); // 
    totalGUI.add(testButton);

    // Create the otherButton.
    otherButton = new JButton("The OTHER Button");
    otherButton.setLocation(10, 130);
    otherButton.setSize(160, 30);
    otherButton.addActionListener(new ButtonListener()); // 
    totalGUI.add(otherButton);

    totalGUI.setOpaque(true);
    return totalGUI;
}  // Ends JPanel CreateContent Pane

// SubClass of JPanel
public class ShowImages extends JPanel{
    Image image; // Declare a name for the Image object.
    Image otherCard;

    public ShowImages(){ // Create a constructor method
        super();
        // Load an image file into our Image object.
        // Make an Images folder in the Show Images folder (BY HAND NOT CODING).
        // This Images folder has to be in the same directory as ShowImage.class.
        // Drag-n-drop a copy of images into the Images folder (BY HAND NOT CODING).
        image = new ImageIcon(getClass().getResource("Images/myImage.gif")).getImage();
        otherCard = new ImageIcon(getClass().getResource("Images/otherCard.gif")).getImage();
    }
    public void paintComponent(Graphics g){
        // Draw our Image object.
        g.drawImage(image,0,0,72,96, this); // at location 0, 0, 72 wide and 96 high
        g.drawImage(otherCard,0,0,72,96, this); // Draw the other Image object.
    }
}
@Override // Create actionPerformed(ActionEvent event) to be OVERRIDDEN
public void actionPerformed(ActionEvent event) {
    // When the user clicks, have something happen. (OVERRIDDEN)
}
// Gets source of Event (textField) and performs action.
class InputListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent evt) {
        String text = textField.getText();
        endUpLabel.setText(text);
        textField.setBackground(Color.gray);
        textField.setText("");
    }
}
// Gets source of Event (ButtonPush) and performs action.
class ButtonListener implements ActionListener {
    //private final static String newline = "\n";
    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == testButton) {
            // Code to run on testButton button push.
            textField.setBackground(Color.white);
            textField.setText("Please enter text here");
        } 
        else if(e.getSource() == otherButton) {
            // Code to run on otherButton button push.
            endUpPanel.setBackground(Color.green);
            endUpLabel.setText("Start Game");

            Players thePlayer = new Players ( "Fred", 1, 5, 6500 );

            thePlayer.StartGame ();
        }
    } // Ends actionPerformed
} // Ends ButtonListener.

// Creates the GUI.
private static void createAndShowGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame(" GUI Problem Example ");

    // Create and set up the content pane.
    ExampleGUI GUI = new ExampleGUI();
    frame.setContentPane(GUI.createContentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setVisible(true);
}
public static void main(String[] args) {
    // Run the GUI.
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
// ***  Here is the Inner Class Players.  ***
public class Players  {
    String name; // Name of Player.
    int numberPlayer; // Number for picking for Seating. (Future use).
    int seatNumber;
    int chipStack;

    public Players( String n, int np, int st, int cs ) {
        name = n;
        numberPlayer = np;
        seatNumber = st;
        chipStack = cs;
    }
    // Create the Setters & Getters.    
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void StartGame (  ) {
        Players thePlayer = new Players ( "Fred", 1, 5, 6500 );
        thePlayer.setName(name);
        endUpLabel.setText("Your Card: IT WORKED!!!");
    } // Ends StartGame.
  } // Ends the Inner Class Players.
}

对不起,如果这是一个愚蠢的问题,我只是在学习。再次感谢。

0 个答案:

没有答案