Results 1 to 2 of 2
- 01-18-2011, 10:43 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
Adding JButton to JFrame with background
Hey, I'm attempting to create a game, and add a buy menu to it, but I can't seem to figure out how to add a JButton. Here's my code, thanks to anybody who can help.
Java Code:import java.awt.*; import java.awt.event.*; import java.io.*; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; public class NewOrLoad extends JPanel implements KeyListener, ActionListener { private JButton butNew1 = new JButton("New Game"); private ImageIcon image = new ImageIcon("Background2.png"); private JLabel label = new JLabel(); private Image imaBg; private ScreenManager screenmanager; private boolean bClicked = false; private String sName = ""; private static final DisplayMode modes1[] = { new DisplayMode(1366, 768, 32, 0), new DisplayMode(1366, 768, 24, 0), new DisplayMode(1366, 768, 16, 0), new DisplayMode(1280, 1024, 32, 0), new DisplayMode(1280, 1024, 24, 0), new DisplayMode(1280, 1024, 16, 0), new DisplayMode(800, 600, 32, 0), new DisplayMode(800, 600, 24, 0), new DisplayMode(800, 600, 16, 0), new DisplayMode(640, 480, 32, 0), new DisplayMode(640, 480, 24, 0), new DisplayMode(640, 480, 16, 0),}; public void run() { screenmanager = new ScreenManager(); try { DisplayMode dm = screenmanager.findFirstCompatibleMode(modes1); screenmanager.setFullScreen(dm); try { loadImages(); } catch (Exception exc) { } movieLoop(); } finally { screenmanager.restoreScreen(); } } public void loadImages() throws IOException { //Load background imaBg = ImageIO.read(new File("Background2.png")); //butLoad.setText("Load Last Game"); //butNew.setText("New Game"); } public void movieLoop() { Window winScreen = screenmanager.getFullScreenWindow(); winScreen.setFocusTraversalKeysEnabled(false); JButton submit = new JButton("Submit Form"); JPanel p = new JPanel(); p.add(submit); winScreen.add(p); p.setVisible(true); //Add listeners to screen winScreen.addKeyListener(this); while (bClicked == false) { Graphics2D g = screenmanager.GetGraphics(); g.setFont(new Font("Impact", Font.BOLD, 36)); g.setColor(Color.WHITE); draw(g); g.dispose(); screenmanager.update(); try { Thread.sleep(20); } catch (Exception ex) { } } } public void draw(Graphics g) { int nWidth = screenmanager.getWidth(); int nHeight = screenmanager.getHeight(); //Load images that change with the resolution g.drawImage(imaBg, 0, 0, nWidth, nHeight, null); //this.add(butNew); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { //Set escape key to exit program int KeyCode = e.getKeyCode(); if (KeyCode == KeyEvent.VK_ESCAPE) { try { bClicked = true; screenmanager.restoreScreen(); } catch (Exception ex) { } } else if (KeyCode == KeyEvent.VK_SPACE) { sName += " "; } else if (KeyCode == KeyEvent.VK_BACK_SPACE) { String sNameCopy = sName; sName = ""; for (int i = 0; i < sNameCopy.length() - 1; i++) { sName += sNameCopy.charAt(i); } } else if (KeyCode == KeyEvent.VK_ENTER) { try { PrintWriter fout = new PrintWriter(new FileWriter("PlayerInfo.txt")); fout.flush(); fout.println(sName); fout.close(); bClicked = true; screenmanager.restoreScreen(); } catch (Exception ex) { } } else if (KeyCode == KeyEvent.VK_SHIFT || KeyCode == KeyEvent.VK_CONTROL || KeyCode == KeyEvent.VK_ALT || KeyCode == KeyEvent.VK_TAB || KeyCode == KeyEvent.VK_CAPS_LOCK){ } else { sName += KeyEvent.getKeyText(KeyCode); e.consume(); } } public void keyReleased(KeyEvent e) { } public void actionPerformed(ActionEvent e) { System.out.println("Test"); } }
- 01-19-2011, 06:55 PM #2
When asking a question like this, it's best to provide an SSCCE- if you're trying to add a JButton to a JFrame, only worry about that part of the problem. Anything else just gets in the way of people trying to help you.
I suggest giving this a read: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
JFrame image background
By 851marc in forum NetBeansReplies: 5Last Post: 03-09-2010, 06:27 PM -
Need help with JFrame background image
By ProGenius in forum New To JavaReplies: 6Last Post: 12-27-2009, 04:17 PM -
How to change the default background color of a DISABLED JButton
By ryogananda in forum Java AppletsReplies: 4Last Post: 03-21-2009, 05:48 PM -
Background in JFrame ( GUI).
By Twister03 in forum AWT / SwingReplies: 2Last Post: 03-12-2009, 03:24 AM -
JButton onClick change color background
By behrk2 in forum AWT / SwingReplies: 6Last Post: 07-09-2008, 04:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks