Developing a C/S When playing games , Page layout uses CardLayout, This allows you to switch pages in the same window , But game code is another class , This class needs to be in the JPanle Painting N Points , And this one JPanle It's in CardLayout After switching , To show it . When you click start , After switching pages , Again JPanle Painting on , So there's a problem : to START Button plus monitor , Under switch page code , When executing game initialization , Is initialization performed first , After the implementation of page switching .

Of this picture START The button needs to perform two actions : Switch pages , Initialize game code . The original code looks like this :
btnStart.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { // Number of acquisition points
n=Integer.parseInt(comboBox.getSelectedItem()+""); // Switch page code ((CardLayout)
MainJpanel.getLayout()).show(gameMain.getParent(),"game"); // Initialize game code
d.reset(n); } });

When you click START Button , Even after the switch page code is executed , The page is still stuck on the current page , Game page needs JPanle It doesn't show up , And then it was executed d.reset(n); When the execution is finished , The page is still stuck in the current page , So when you jump to the next page , The game area is blank .

absolutely empty .

How to click on the last page START after , You can switch pages first , What about the post initialization game area ?

So I changed the code to look like this :
btnStart.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { Thread panleListen=new Thread(()->{
while(true){ if(isShow==1){ try { Thread.sleep(100); } catch
(InterruptedException e1) { // TODO Automatically generated catch block e1.printStackTrace(); }
d.reset(n); break; } } }); n=Integer.parseInt(comboBox.getSelectedItem()+"");
// Switch page code ((CardLayout)
MainJpanel.getLayout()).show(gameMain.getParent(),"game"); isShow=1;
panleListen.start(); } });

Solution principle : Every click START, When running to panleListen.start(); Time , Just create a new thread , The thread will run after the graphics thread , That is, after the page switch ends , Rerun d.reset().

debug You can see that :

AWT That thread is the GUI thread , Only after the thread has finished executing , To switch pages , The new thread will be in the AWT Run the thread after the end of running , The new thread is responsible for initializing the game , So when it comes to creating a new thread , What the game needs JPanle It's switched over , So we solved the problem successfully .

Technology
©2020 ioDraw All rights reserved
Feature Engineering Comparison of data extraction tools :Kettle,Datax,Sqoop,StreamSets Nikkei : Sony and Jiaxia are actively applying for Huawei's supply license Self made whole person computer program k8s delete pod2020 Nobel Prize in physiology or medicine announced Interview questions : Handwritten list ( Include reverse linked list ) Some views of a student on Hongmeng system Children programming children's learning route Programmer's promotion 28 Sorting code of Guan 3