我正在为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 :(得分:1)
假设a1.png
与GUI.class
位于同一文件夹中,您可能需要像下面这样加载它:
new ImageIcon(GUI.class.getResource("a1.png"))