Results 1 to 6 of 6
- 10-19-2009, 09:14 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
how to put a background image in a JPanel
hi. this is my code for the startMenu of my memory game. how can i put a backgroundimage in this? thank you
Java Code:import java.lang.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; //import static java.util.Collections.*; class GameInterface extends JFrame implements ActionListener{ private JButton startBtn, exitBtn, howtoBtn, scoreBtn, levelBtn; private JPanel wholeMenuPanel, firstMenuPanel, secondMenuPanel; private JLabel welcomeLabel, nameLabel; private TextField nameField; public GameInterface(){ init(); MenuPanel(); firstMenuPanel(); secondMenuPanel(); setTitle("GuessMe"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(700,500); setVisible(true); } public void init(){ try{ jbInit(); } catch(Exception e){ e.printStackTrace(); } } private void jbInit()throws Exception { startBtn = new JButton ("Start/New Game"); startBtn.addActionListener(this); howtoBtn = new JButton ("How to play this game"); howtoBtn.addActionListener(this); levelBtn = new JButton ("Level"); levelBtn.addActionListener(this); scoreBtn = new JButton ("Score"); scoreBtn.addActionListener(this); exitBtn = new JButton ("Exit/Quit Game"); exitBtn.addActionListener(this); welcomeLabel.setBounds(new Rectangle(21, 13, 118, 30)); nameLabel.setBounds(new Rectangle(21, 50, 118, 30)); welcomeLabel.setText("WELCOME!"); nameLabel.setText("Your name here:"); nameField = new TextField("", 20); //welcomeLabel.addAcionListener(this); //nameLabel.addActionListener(this); nameField.addActionListener(this); } public void MenuPanel(){ wholeMenuPanel = new JPanel(); firstMenuPanel = new JPanel(); secondMenuPanel = new JPanel(); wholeMenuPanel.setLayout(new BorderLayout()); firstMenuPanel.setLayout(new FlowLayout()); secondMenuPanel.setLayout(new FlowLayout()); wholeMenuPanel.add(firstMenuPanel, BorderLayout.NORTH); wholeMenuPanel.add(secondMenuPanel, BorderLayout.CENTER); } public void firstMenuPanel(){ Panel strtPnl = new Panel(); strtPnl.setLayout(null); Panel buttonPnl = new Panel(); buttonPnl.add(startBtn); buttonPnl.add(exitBtn); buttonPnl.add(howtoBtn); buttonPnl.add(levelBtn); buttonPnl.add(scoreBtn); add(strtPnl, BorderLayout.CENTER); add(buttonPnl, BorderLayout.SOUTH); } public void secondMenuPanel(){ JLabel welcomeLabel = new JLabel(); JLabel nameLabel = new JLabel(); TextField nameField = new TextField(); //backgroundLabel = new JLabel (); //add(welcomeLabel, FlowLayout.CENTER); //add(nameLabel, FlowLayout.CENTER); //add(nameField, TextLayout.CENTER); } public void actionPerformed(ActionEvent e){ if (startBtn == e.getSource()){ //GuessMe application = new GuessMe(); } if (exitBtn == e.getSource()){ System.exit(0); } if (howtoBtn == e.getSource()){ JOptionPane pane = new JOptionPane(); pane.showMessageDialog(null, "To play this game, the player shoud click the cards and match up the pairs."); } if (levelBtn == e.getSource()){} if (scoreBtn == e.getSource()){} } public static void main(String[] args){ GameInterface Application = new GameInterface(); } }Last edited by yanipao; 10-19-2009 at 10:14 AM.
- 10-19-2009, 10:30 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Google is our friend.
- 10-19-2009, 12:55 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
thanks...=)
- 10-21-2009, 02:17 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 25
- Rep Power
- 0
I recently had to do this, and the way i did was by making a new class, ImagePanel..It just extends the JPanel class.Hope this helps you a bit
- 10-21-2009, 02:21 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
In that case then steelshark is also our friend.
- 10-21-2009, 02:27 PM #6
Member
- Join Date
- Aug 2009
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
Background Image of Applet
By BangJava in forum Java AppletsReplies: 8Last Post: 11-24-2010, 05:48 AM -
J2ME background image
By sandeeprao.techno in forum CLDC and MIDPReplies: 0Last Post: 09-14-2009, 09:02 AM -
Background image
By leiferouis in forum New To JavaReplies: 9Last Post: 03-08-2009, 05:49 PM -
Image as background
By Java.child in forum AWT / SwingReplies: 2Last Post: 10-02-2008, 11:37 PM -
Why this image background is black ?
By samson in forum Java 2DReplies: 1Last Post: 07-17-2007, 04:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks