I have a buttonlistener that removes the TextFields and the StartButton when it is clicked, but the last part of the code that tells it to run a method that is supposed to display the icon is screwing up only at the end, but is still deleting the TextFields and the JButton. Please help.
public class TypeInNames extends JApplet{
JButton StartButton;
JTextField name1, name2;
String player1, player2;
String reply;
boolean test = false;
ImageIcon myIcon;
Container cp = getContentPane();
public void init()
{
setSize(350, 400);
setLayout(null);
cp.setBackground(Color.black);
StartButton = new JButton("Start Game!");
name1 = new JTextField("Player 1",35);
name2 = new JTextField("Player 2",35);
//(x, y, width, height);
StartButton.setBounds(115,200,120,30);
name1.setBounds(115,140,120,20);
name2.setBounds(115,170,120,20);
startGame();
}
public void startGame()
{
add(StartButton);
add(name1);
add(name2);
StartButton.addActionListener(new ButtonListener());
}
public void game()
{
}
public void endGame()
{
myIcon = new ImageIcon("portal-cake.jpg");
test = true;
repaint();
}
public void paintComponent(Graphics g) {
super.paint(g);
if(test)
myIcon.paintIcon(this, g, 0, 0);
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == StartButton)
{
player1 = name1.getText();
player2 = name2.getText();
remove(StartButton);
remove(name1);
remove(name2);
endGame();
}
}
}
}