Panels and Frames
The following question was posted on a Java Developer Forum. Be the “expert,” and explain why this user sees only one panel on his/her frame. “Trying to add two JPanels in one JFrame but only ever get one JPanel and it’s always the last. Can someone please explain this to me? Thanks”
import java.awt.*; import javax.swing.*; // Program modifed for anonymity and clarity // Original posted program was less concise and poorly formatted public class query { public static void main(String[] args) { JFrame myFrame = new JFrame("Anonymous"); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setSize(800, 300); JPanel panel_1 = new JPanel(new GridLayout(2, 3, 5, 5)); panel_1.add(new JButton("Main")); panel_1.add(new JButton("Help")); panel_1.add(new JButton("Save")); JPanel panel_2 = new JPanel(new GridLayout(2, 3, 5, 5)); panel_2.add(new JButton("1")); panel_2.add(new JButton("2")); panel_2.add(new JButton("3")); panel_2.add(new JButton("4")); panel_2.add(new JButton("5")); panel_2.add(new JButton("6")); myFrame.add(panel_1); myFrame.add(panel_2); myFrame.setVisible(true); } }
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.