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