Hi,
I am having a problem with this program. I don't know why the counter won't work correctly. After "doctor is out" is clicked the inCounter should be zero. So when the "doctor is in" is clicked "the doctor is in" should be displayed, but "the doctor IS IN, Already!" is displayed. Also, why does the frame have to be enlarged slightly in order for display to change?
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());
JPanel panel3 = 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 for images
JLabel jlblDoctorIn = new JLabel(doctorIn);
JLabel jlblDoctorOut = new JLabel (doctorOut);
//Create two labels for text
JLabel jlblDoctorInText = new JLabel("The doctor is in.");
JLabel jlblDoctorOutText = new JLabel("The doctor is out.");
JLabel jlblDoctorINText = new JLabel ("The doctor IS IN, already!");
int inCounter = 1;
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 the label
panel2.add(jlblDoctorIn,BorderLayout.NORTH);
//Create a panel to hold both panels
panel3.add(panel, BorderLayout.SOUTH);
panel3.add(panel2, BorderLayout.NORTH);
//add contents to the frame
add(panel3,BorderLayout.NORTH);
add(jlblDoctorInText, 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"))
{
doctorIsIn(inCounter);
}
else if(actionCommand.equals("Click for Out"))
{
doctorIsOut(inCounter);
}
else System.out.println("Error in button interface.");
}
public int doctorIsOut(int inCounter)
{
panel2.remove(jlblDoctorIn);
remove(jlblDoctorInText);
remove(jlblDoctorINText);
panel2.add(jlblDoctorOut,BorderLayout.NORTH);
add(jlblDoctorOutText,BorderLayout.SOUTH);
inCounter = 0;
repaint();
return(inCounter);
}
public int doctorIsIn(int inCounter)
{
if(inCounter < 1)
{
panel2.remove(jlblDoctorOut);
remove(jlblDoctorOutText);
panel2.add(jlblDoctorIn,BorderLayout.NORTH);
add(jlblDoctorInText,BorderLayout.SOUTH);
inCounter = inCounter + 1;
repaint();
return (inCounter);
}
else
{
panel2.remove(jlblDoctorOut);
remove(jlblDoctorOutText);
remove(jlblDoctorInText);
panel2.add(jlblDoctorIn,BorderLayout.NORTH);
add(jlblDoctorINText, BorderLayout.SOUTH);
inCounter = inCounter + 1;
repaint();
return (inCounter);
}
}
public static void main(String[] args)
{
JFrame frame = new Lucy();
frame.setTitle("Lucy");
frame.setSize(300, 320);
frame.setLocation(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}