indsættelse af jpg billeder i en GUI
jeg er igang med at lave et tekst-baseret spil, og vil gerne indsætte billeder af de forskellige rum. Alle billederne har samme størrelse (400*400px).Billedet skal være i den JPanel som hedder picturePanel.
her er min source code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* GUI is the class that is responsible of the making a GUI for our game.
*
* @author Jesper Højlund
* @version 0.1
*/
public class GUI
{
private JFrame frame;
private JPanel textOutputPanel;
private JPanel picturePanel;
private JPanel textInputPanel;
private JPanel holderPanel;
private JPanel statusPanel;
private JPanel exitsPanel;
private JPanel healthPanel;
private JLabel statusLabel;
private JLabel healthStatusLabel;
private JLabel healthLabel;
private JLabel exitsLabel;
private JLabel exitsStatusLabel;
private JTextArea textOutput;
private JTextField textField;
/**
* Create an GUI, show it on screen.
*/
public GUI()
{
makeFrame();
}
// ---- implementation of menu functions ----
/**
* Help function: displays the functions in the game.
*/
private void showCommands()
{
// this is some test output, until we do this properly
System.out.println("See the commands");
}
/**
* Quit function: quit the application.
*/
private void quit()
{
System.exit(0);
}
// test function to see if status panel works
private void status()
{
String test = textField.getText();
textField.setText("");
updateStatus(test);
}
private void updateStatus(String test)
{
textOutput.setText(test);
}
/**
* Create the Swing frame and its content.
*/
private void makeFrame()
{
frame = new JFrame("Fire Fighter Bob");
makeMenuBar(frame);
//prepare the main content pane
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setLayout(new BorderLayout(2,2));
// create the panel with the picture
picturePanel = new JPanel();
contentPane.add(picturePanel, BorderLayout.NORTH);
// create the panel with the text output
textOutput = new JTextArea(5,1);
textOutput.setBackground(Color.WHITE);
textOutput.setBorder
(
BorderFactory.createCompoundBorder
(
new EtchedBorder(), new EmptyBorder(10, 10, 10, 10)
)
);
contentPane.add(textOutput, BorderLayout.CENTER);
// create a holder panel for the status (health and exits) and text-input
holderPanel = new JPanel(new BorderLayout());
contentPane.add(holderPanel, BorderLayout.SOUTH);
//status panel
statusPanel = new JPanel(new BorderLayout());
holderPanel.add(statusPanel, BorderLayout.NORTH);
//health panel
healthPanel = new JPanel();
statusPanel.add(healthPanel, BorderLayout.WEST);
// create the labels with the health
healthLabel = new JLabel("Health: ");
healthStatusLabel = new JLabel("100");
healthPanel.add(healthLabel);
healthPanel.add(healthStatusLabel);
//statusPanel exits
exitsPanel = new JPanel();
statusPanel.add(exitsPanel, BorderLayout.EAST);
// create the labels with the exits
exitsLabel = new JLabel("Exits: ");
exitsStatusLabel = new JLabel("north, east, south");
exitsPanel.add(exitsLabel);
exitsPanel.add(exitsStatusLabel);
// create the panel with the textinput
textInputPanel = new JPanel();
textField = new JTextField(30);
textInputPanel.add(textField);
textField.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
status();
}
});
holderPanel.add(textInputPanel, BorderLayout.SOUTH);
// arrange the components and show them
frame.pack();
frame.setVisible(true);
}
/**
* Create the main frame's menu bar.
*/
private void makeMenuBar(JFrame frame)
{
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
// create the Functions menu
JMenu functionMenu = new JMenu("Functions");
menubar.add(functionMenu);
JMenuItem quitItem = new JMenuItem("Quit");
quitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { quit(); }
});
functionMenu.add(quitItem);
menubar.add(Box.createHorizontalGlue());
JMenu helpMenu = new JMenu("Help");
menubar.add(helpMenu);
JMenuItem commandsItem = new JMenuItem("Commands");
commandsItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showCommands();
}
}
);
helpMenu.add(commandsItem);
}
}
