This program is supposed to display a frame with a label that contains an image,two buttons, and a text field. When the user clicks the button the image is supposed to change. I know I don't have the code for the change in the text field. I am just trying to get the image to change for right now.
What is happening is that the DoctorOut label is behind the DoctorIn label.
Thank You in advance for any help!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;


public class Lucy extends JFrame implements ActionListener
{

	JPanel panel = new JPanel(new GridLayout(1,2));
	JPanel panel2 = new JPanel(new BorderLayout());
	GridBagConstraints c = new GridBagConstraints();

	//Create two icons
	ImageIcon doctorIn = new ImageIcon("doctorIn.jpg");
	ImageIcon doctorOut = new ImageIcon ("doctorOut.jpg");

	//Create two labels
	JLabel jlblDoctorIn = new JLabel(doctorIn);
	JLabel jlblDoctorOut = new JLabel (doctorOut);


  public Lucy()
  {

	// Create two buttons
	JButton jbtDoctorIn = new JButton("Click for In");
	JButton jbtDoctorOut = new JButton("Click for Out");

 	// Create a panel to hold buttons
         panel.add(jbtDoctorIn,c);
         panel.add(jbtDoctorOut,c);

	//Create a panel to hold panel and label
	panel2.add(jlblDoctorIn,BorderLayout.NORTH);
	panel2.add(panel, BorderLayout.SOUTH);

   	//add contents to the frame
   	add(panel2,BorderLayout.NORTH);
   	add(new JLabel("The doctor is in"), BorderLayout.SOUTH);

    // Register listeners
    jbtDoctorIn.addActionListener(this);
    jbtDoctorOut.addActionListener(this);
  }


  public void actionPerformed(ActionEvent e)
  {
	  String actionCommand = e.getActionCommand();
	  if(actionCommand.equals("Click for In"))
		{
	  	panel2.add(jlblDoctorIn,BorderLayout.NORTH);
	  	add(panel2,BorderLayout.NORTH);
		}
	  else if(actionCommand.equals("Click for Out"))
		{
	  	panel2.add(jlblDoctorOut,BorderLayout.NORTH);
	  	add(panel2, BorderLayout.NORTH);
		}
	else System.out.println("Error in button interface.");
  }


  public static void main(String[] args)
  {
    JFrame frame = new Lucy();
    frame.setTitle("CIS 263 Michelle Ballentine");
    frame.setSize(300, 330);
    frame.setLocation(200, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

Hi Michelle,

You don't actually need to create two JLabel and make it overlapping each other. I would suggest creating one JLabel and make the icon change upon clicking a button. It would be something like this.

First, create 2 ImageIcon objects

ImageIcon doctorIn = new ImageIcon("doctorIn.jpg");
ImageIcon doctorOut = new ImageIcon ("doctorOut.jpg");
..
..

Then we will create a status label. Let's say, by default, the doctor is out

JLabel jlblStatus = new JLabel (doctorOut);

and on your actionPerformed method, we can change the icon by

if(actionCommand.equals("Click for In")){
      jlblStatus.setIcon(doctorIn);
      ..
      ..
}
else if(actionCommand.equals("Click for Out")){
      jlblStatus.setIcon(doctorOut);
      ..
      ..
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.