2012-04-25 44 views
0

我正在用java編寫一個棋盤遊戲的GUI。我創建了很多面板,標籤和按鈕,並將它們全部添加到框架中。一切正常,除非我無法看到圖像。這裏是我的支持評論的代碼。我無法弄清楚爲什麼圖像不會顯示出來。我已經嘗試了許多不同的嘗試,因爲您可以看到有很多註釋掉的代碼。先謝謝您的幫助。問題將圖像添加到JAVA中的GUI

package gui; 

import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.Frame; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

import listeners.PlaceButtonListener; 
import listeners.TextFieldListener; 

public class GUI { 

public static void main(String[] args) 
{ 
    new GUI(); 
} 

public GUI() 
{ 

    int playerTurn = 1; // Variable to hold what player's window it is 

    JFrame frame = new JFrame(); 
    frame.setSize(880, 680); 

    //JPanel totalGUI = new JPanel(); // Contains all other panels 
    //totalGUI.setLayout(null); 

    // Creation of Panels on totalGUI 
    JPanel scorePanel = new JPanel(); 
    JPanel buttonPanel = new JPanel(); 
    JPanel boardPanel = new JPanel(); 
    JPanel tilePanel = new JPanel(); 
    JPanel turnPanel = new JPanel(); 
    JPanel playerPanel = new JPanel(); 
    JPanel placePanel = new JPanel(); 

    // Creation of Labels 
    JLabel scoreLabel = new JLabel("Score:"); 
    JLabel playerLabel = new JLabel("Player: " + playerTurn); 
    JLabel tileLabel = new JLabel("Current Tile:"); 
    JLabel turnLabel = new JLabel("Player " + playerTurn +"'s Turn"); 
    JLabel imageLabel = new JLabel(new ImageIcon("a1.png")); 

    // Creation of Buttons 
    JButton resignButton = new JButton("Resign"); 
    JButton placeButton = new JButton ("Place"); 
    JButton commitButton = new JButton("Commit"); 
    JButton removeButton = new JButton("Remove"); 

    // Creation and adding of JMenuBar, JMenus and JMenuItems 
    JMenuBar mBar = new JMenuBar(); 
    JMenu file = new JMenu("File"); 
    JMenuItem saveAndQuit = new JMenuItem("Save and Quit"); 
    JMenuItem quit = new JMenuItem("Quit"); 
    JMenu help = new JMenu("Help"); 
    JMenuItem gameManual = new JMenuItem("Game Manual"); 
    file.add(saveAndQuit); 
    file.add(quit); 
    help.add(gameManual); 
    mBar.add(file); 
    mBar.add(help); 

    // Creates the text field where the player will input x and y values 
    JTextField placeField = new JTextField(10); 

    // playerPanel displays what Player's window it is 
    playerLabel.setLocation(0, 0); 
    playerLabel.setSize(120, 30); 
    playerLabel.setHorizontalAlignment(0); 
    playerPanel.add(playerLabel); 
    playerPanel.setLayout(null); 
    playerPanel.setLocation(350, 0); 
    playerPanel.setSize(120, 30); 
    //totalGUI.add(playerPanel); 
    frame.add(playerPanel); 


    // scorePanel displays the score of all Players 
    scoreLabel.setLocation(0, 0); 
    scoreLabel.setSize(120, 30); 
    scoreLabel.setHorizontalAlignment(0); 

    scorePanel.add(scoreLabel); 
    scorePanel.setLayout(null); 
    scorePanel.setLocation(740, 40); 
    scorePanel.setSize(120, 300); 
    scorePanel.setBackground(Color.YELLOW); 
    //totalGUI.add(scorePanel); 
    frame.add(scorePanel); 


    // Places and adds buttons to panel 
    buttonPanel.add(commitButton); 
    commitButton.setLocation(10, 0); 
    commitButton.setSize(120, 30); 

    buttonPanel.add(placeButton); 
    placeButton.setLocation(140, 0); 
    placeButton.setSize(120, 30); 

    buttonPanel.add(removeButton); 
    removeButton.setLocation(10, 40); 
    removeButton.setSize(120, 30); 

    buttonPanel.add(resignButton); 
    resignButton.setLocation(140, 40); 
    resignButton.setSize(120, 30); 

    buttonPanel.setLayout(null); 
    buttonPanel.setLocation(350, 560); 
    buttonPanel.setSize(280,100); 
    buttonPanel.setBackground(Color.BLUE); 
    //totalGUI.add(buttonPanel); 
    frame.add(buttonPanel); 

    // placeField takes user input of where to place tile 
    placeField.setLocation(145, 0); 
    placeField.setVisible(true); 
    placeField.setSize(60, 30); 
    placePanel.add(placeField); 
    placePanel.setLocation(630, 560); 
    placePanel.setSize(150, 40); 
    placePanel.setBackground(Color.BLACK); 
    frame.add(placePanel); 


    // Sets size of Board and adds it to totalGUI 
    imageLabel.setSize(100, 100); 
    imageLabel.setLocation(10,10); 
    boardPanel.setLayout(null); 
    boardPanel.setLocation(220, 40); 
    boardPanel.setSize(480, 480); 
    // boardPanel.setBackground(Color.BLACK); 


    boardPanel.add(imageLabel); 
    //totalGUI.add(boardPanel); 
    frame.add(boardPanel); 

    //add JLabel on the boardPanel to display board configuration. HashMap? 



    // Adds tileLabel to tilePanel and sets the size of tilePanel 
    tileLabel.setLocation(0, 0); 
    tileLabel.setSize(200, 30); 
    tileLabel.setHorizontalAlignment(0); 

    tilePanel.add(tileLabel); 
    tilePanel.setLayout(null); 
    tilePanel.setLocation(0, 40); 
    tilePanel.setSize(200, 210); 
    tilePanel.setBackground(Color.RED); 
    //totalGUI.add(tilePanel); 
    frame.add(tilePanel); 


    // Adds turnLabel to turnPanel and sets size 
    turnLabel.setLocation(0, 0); 
    turnLabel.setSize(150, 30); 
    turnLabel.setHorizontalAlignment(0); 

    turnPanel.add(turnLabel); 
    turnPanel.setLayout(null); 
    turnPanel.setLocation(0, 280); 
    turnPanel.setSize(150, 30); 
    turnPanel.setBackground(Color.GREEN); 
    //totalGUI.add(turnPanel); 
    frame.add(turnPanel); 


    // Adds all the panels and menus to the Frame 
    frame.setJMenuBar(mBar); 
    //frame.add(totalGUI); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(280, 190); 
    //frame.pack(); 
    frame.setVisible(true);  
} 

}

+0

使用類加載器增加一個形象,是在你的源代碼 – 2012-04-25 21:59:03

+0

這裏使用的文件是什麼'new ImageIcon(「a1.png」)'? 「a1.png」期望它位於您運行代碼的位置,但可能並非如此。您應該使用圖像的絕對路徑。 – twain249 2012-04-25 22:00:23

回答

1

假設a1.png是在同一文件夾中GUI.class,你想加載它像這樣:

new ImageIcon(GUI.class.getResource("a1.png"))