I have a button which runs a function in another class.The button click event handling goes like this:
JPanel ne=new JPanel();
JButton startButton=new JButton("START");
public void actionPerformed(ActionEvent e)
{
System.out.println("Welcome to Guess the number Game");
System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10");
ne.remove(startButton);
gamer2 game=new gamer2();
game.generatenum();
}
The JButton startButton
is added to a JPanel ne
.Once the button is clicked,actionPerformed(ActionEvent e)
runs and the button is supposed to be removed from the JPanel.But the button remains there until the whole program finishes running.Can someone help me with this?
Complete code for the JFrame:
package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
public class test3 implements ActionListener {
JButton p=new JButton("START");
JPanel ne=new JPanel();
public void create()
{
Dimension s=new Dimension(400,400);
JFrame l=new JFrame();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setSize(s);
l.setResizable(true);
Dimension s1=new Dimension(400,200);
Dimension s2=new Dimension(400,100);
JPanel me=new JPanel();
JLabel kingsman=new JLabel ("GUESS THE KING'S NUMBER!");
kingsman.setFont(new Font("Serif", Font.BOLD, 45));
JPanel commonPane=new JPanel();
BoxLayout n1=new BoxLayout(commonPane,1);
commonPane.setLayout(n1);
p.setFont(new Font("Serif", Font.BOLD, 40));
p.setPreferredSize(s1);
JTextField tf=new JTextField();
tf.setFont(new Font("Serif", Font.BOLD, 45));
Border border = BorderFactory.createLineBorder(Color.YELLOW, 5);
tf.setBorder(border);
tf.setPreferredSize(s2);
JPanel cn=new JPanel();
cn.add(tf);
//l.add(p);
me.add(kingsman);
ne.add(p);
commonPane.add(me);
commonPane.add(ne);
commonPane.add(cn);
l.add(commonPane);
l.setVisible(true);
p.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("Welcome to Guess the number Game");
System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10");
ne.remove(p);
ne.updateUI();
gamer2 game=new gamer2();
game.generatenum();
}
public static void main(String args[])
{
test3 ob=new test3();
ob.create();
}
}