Results 1 to 2 of 2
  1. #1
    bzknight is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default 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");
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    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

  1. JFrame image background
    By 851marc in forum NetBeans
    Replies: 5
    Last Post: 03-09-2010, 07:27 PM
  2. Need help with JFrame background image
    By ProGenius in forum New To Java
    Replies: 6
    Last Post: 12-27-2009, 05:17 PM
  3. Replies: 4
    Last Post: 03-21-2009, 06:48 PM
  4. Background in JFrame ( GUI).
    By Twister03 in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2009, 04:24 AM
  5. JButton onClick change color background
    By behrk2 in forum AWT / Swing
    Replies: 6
    Last Post: 07-09-2008, 05:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •