I have a question as to why JPanel's work the way this certain way. When I try and make GUI applications or games, I always try to make the centered display JPanel through a different class that extends JPanel.
I've literally just started a Tetris demo, and I decided to make a MainScreen class that extends JPanel. And I add an instance of MainScreen to the center of the original JPanel(Tetrisgame class). I first create an instance of the MainScreen JPanel called screen. I then create an instance of BlockPart called bp. And I invoke screen.addPartToScreenAt(2,2,bp) - the method's function is to add the parameter bp(instance of BlockPart) to the MainScreen JPanel at coordinates 2,2. I do this so that I can see a new JPanel - namely a BlockPart - on the MainScreen. I just want to test that I can actually change the JPanels from separate classes by attempting to display one simple BlockPart(JPanel) on the screen! Assistance?
Code that I did not provide but it irrelevant:
-Main class that creates JFrame window.
-Block contains an array of BlockParts that will eventually be a combination of blockparts that create the conventional Tetris blocks.
You add empty JPanels, yes. But in your addPartToScreenAt method you don't remove one of those JPanels and add the BlockPart. A quick and untested fix:
Also, a small improvement:
dave dal wrote:
Instead of calling setLayout(...), call super(...). Right now these constructors implicitly call super() which sets a FlowLayout. Afterwards you immediately overwrite that with the actual layout. By calling the super class constructor with a layout you skip the FlowLayout.
Wow thank you so much for your help, guys. Yes. I've been studying java, c++, and python for only a year and a half now. But these GUI concepts really help me understand many classes that I have not already learned!