I have been learning java GUI(swing to be precise) by referring online sources and practising.
Code:(p
is a JButton)
p.addActionListener(new ActionListener(){
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.revalidate();
ne.repaint();
l.dispose(); //l is a JFrame
gamer2 game=new gamer2();
game.generatenum(); //works on the console
l.setVisible(true);
}});
Problems:
- In some related questions I posted earlier,I learnt that any update/change done to
java GUI
will only be effective after theactionPerformed()
is completely executed. But the thing isl.dispose()
works or theJframe l
disappears even beforegeneratenum()
function is completely executed which meansactionPerformed()
is still not completed executing but theJFrame disappears
.generatenum()
runs on the console.The thing isbutton is removed
only afteractionPerformed()
is completely executed but why is this different in case of the frame. - I am also aware that
java control flows from line to line
(atleast in the above example).TheJFrame reappers since I have written l.setVisible(true);
. But this happens beforegeneratenum()
is completely executed. Thegeneratenum()
will only stop running if I enter a suitable number on the console.So how is the control jumping tol.setVisible(true)
before the previous line/function execution is completed.
What is generatenum()
?
It is a function which accepts user input on the Eclipse console.It doesn't stop running unless it receives a valid input from the user.
void generatenum()
{
int ran=(int)(Math.random()*10);
System.out.println("For developer-no.selected "+ran);
getUserInput(ran);
}
void getUserInput(int k)
{
i++;
System.out.println("print now-Chance "+i);
g.gotValue=k;
InputStreamReader j=new InputStreamReader(System.in);
BufferedReader s=new BufferedReader(j);
try {
int getIt1=Integer.parseInt(s.readLine());
g.getIt=getIt1;
} catch (IOException e)
{
e.printStackTrace();
}
}
generatenum()
works on the console.