简体   繁体   中英

java gui not displaying correctly

I have a problem with the GUI after calling it from another method, I am making a game of dominoes.

In the picture here, the GUI is working fine and does what it is supposed to do, however in the second image I tried to use the exact same GUI however It is parsed through a different way ( I have a start screen, which will call the method and make the GUI visible). Here in the console, the tiles are stored in the allocated array and are also drawn onto the GUI

第一张图片

The second image here, the playerVSAI method is called in an actionlistener from the firstScreen() class. this class is just window which is displayed on the left. However the the console, the playerVSAI method seems to work as intended however the images of tiles are not displayed.

The method which does the stores the data into the array and draws the tiles onto the is the same one, I have attatched a snippet of the code below

        if(input2.equalsIgnoreCase("l"))
    {

        //add selected input to gameboard
        gameboardX.add(0,player.get(input1));

        JLabel imageTile = new JLabel(new ImageIcon(rotate(bi,rotateDegree)));

        //place domino image to left, Y direction still the same, only shift X , 
        // shift X by turnCounter, as turnCounter++  larger amount can only shift outwards
        c.gridy = startingY;
        c.gridx = startingX - turnCounter ;

        //add image
        gui.add(imageTile,c);
        gui.revalidate();
    }

第二张图片

编辑

Make sure that you have called the setVisible(true); line at the end of adding elements to GUI.

This looks and feels like a threading problem, possibly a race condition between your thread and Swing's (but such things can happen just by running setVisible from the main thread).

Make sure that all the logic that starts the window is run from EDT and that no data is passed to Swing objects from other threads. The simplest architecture will be to put all your code into the EventQueue#invokeLater , later you can move to some form of synchronization (frankly, for a game of dominoes, I wouldn't... unless you have a massive artificial intelligence running one of the players).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM