Search code examples
javaimageswingjapplet

Creating a JApplet MadLibs game


I am creating a JApplet MadLibs game, but the image I add fills up the entire page so that my Labels and JTextFields are being blocked. I only want the logo to be centered at the top of the applet.

My code:

import java.awt.*;
import javax.swing.*;
import javax.swing.JApplet;
import java.awt.event.*;
import java.net.URL;

public class MadLibs extends JApplet implements ActionListener {
  JLabel label1 = new JLabel("A Proper First Name: ");
  JLabel label2 = new JLabel("A Pet: ");
  JLabel label3 = new JLabel("A Personal Attribute: ");
  JLabel label4 = new JLabel("An Adjective: ");
  JLabel label5 = new JLabel("A Verb: ");
  JTextField jtf1 = new JTextField();
  JTextField jtf2 = new JTextField();
  JTextField jtf3 = new JTextField();
  JTextField jtf4 = new JTextField();
  JTextField jtf5 = new JTextField();
  JButton jb = new JButton("Create!");
  JLabel madlib = new JLabel("");
  Container con = getContentPane();
  private Image image;


  public void init() {
     image = getImage(getDocumentBase(), "MadLibsLogo.gif");
     jb.addActionListener(this);
     con.add(label1);
     con.add(jtf1);
     con.add(label2);
     con.add(jtf2);
     con.add(label3);
     con.add(jtf3);
     con.add(label4);
     con.add(jtf4);
     con.add(label5);
     con.add(jtf5);
     con.add(jb);
     con.add(madlib);
  }
  public void actionPerformed(ActionEvent e) {
     madlib.setText(jtf1.getText() + " had a little " + jtf2.getText() + "/nIts " + jtf3.getText() + " was " + jtf4.getText() +
        " as snow/nAnd everywhere that " + jtf1.getText() + jtf5.getText() + "/nThe " + jtf2.getText() + " was sure to go.");
     repaint();
  }
  public void paint(Graphics g)
  {
     g.drawImage(image, 0, 0, 229, 73, this);
  }
}

Solution

  • Don't override a JApplet's paint method, and in fact it is usually a bad idea to override the paint method of any top-level window unless you're sure that you need to do this, don't care about or need the double buffering offered with Swing graphics, and understand that this may effect the GUI's borders and child components.

    Why not simply put the Image in an ImageIcon and that in a JLabel, and then place the JLabel where you desire to display the image?

    Edit: you appear to be adding many components to the contentPane without regard for layout. Have you changed it's layout from the default BorderLayout somewhere in code not posted?

    Edit 2 You state:

    No I haven't...I still haven't gotten to the layout. How would I change the BorderLayout so that everything is aligned?

    If you haven't changed the layout, then in all likelihood the only thing you are seeing is the last component added to the contentPane, the madLib JLabel. What you will want to is to layer JPanels on top of each other, each with its own layout manager. For instance, continue leaving the contentPane with a BorderLayout, adding the logo JLabel with the image in the BorderLayout.NORTH or PAGE_START position. Then add most of the body of the GUI into a JPanel that's placed BorderLayout.CENTER. Read up on the various layout managers and play with them as that's the best way to learn how to use them.