[英]Can't see JPanel in JFrame it's contained in
I can't find my JPanel
in the JFrame
after compiling, the frame is set to BorderLayout
and the panel is set to CENTER
but when I run it the panel doesn't appear. 编译后,在JFrame
找不到我的JPanel
,该框架设置为BorderLayout
且该面板设置为CENTER
但是当我运行它时,该面板没有出现。 Is there something missing in my code? 我的代码中缺少什么吗?
JFrame mainWindow = new JFrame("Francisco's System");
mainWindow.setBounds(10,10,1024,700);
borderEncode = BorderFactory.createTitledBorder("Encode Module");
borderBackup = BorderFactory.createTitledBorder("Backup Module");
borderRestore = BorderFactory.createTitledBorder("Restore Module");
lbl_testpanel1 = new JLabel("This is the Encode Panel");
lbl_testpanel2 = new JLabel("This is the Backup Panel");
lbl_testpanel3 = new JLabel("This is the Restore Panel");
mb = new JMenuBar();
menu1 = new JMenu("Functions");
menu2 = new JMenu("Help");
menu1_encode = new JMenuItem("Encode");
menu1_backup = new JMenuItem("Backup");
menu1_restore = new JMenuItem("Restore");
panelEncode = new JPanel();
panelBackup = new JPanel();
panelRestore = new JPanel();
panelMain = new JPanel();
menu1_encode.addActionListener(control);
menu1_backup.addActionListener(control);
menu1_restore.addActionListener(control);
menu1.add(menu1_encode);
menu1.add(menu1_backup);
menu1.add(menu1_restore);
mb.add(menu1);
mb.add(menu2);
mainWindow.setLayout(new BorderLayout());
panelEncode.setLayout(new FlowLayout());
panelEncode.add(lbl_testpanel1);
panelEncode.setBorder(borderEncode);
panelBackup.setLayout(new FlowLayout());
panelBackup.add(lbl_testpanel2);
panelBackup.setBorder(borderBackup);
panelRestore.setLayout(new FlowLayout());
panelRestore.add(lbl_testpanel3);
panelRestore.setBorder(borderRestore);
panelMain.setLayout(new FlowLayout());
panelMain.setBorder(BorderFactory.createLineBorder(Color.blue));
panelMain.setBackground(Color.red);
mainWindow.add(panelMain, BorderLayout.CENTER);
mainWindow.add(mb);
mainWindow.setJMenuBar(mb);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);`enter code here`
mainWindow.setVisible(true);
}
I expect the panel to appear and span the width and height of the frame but it doesn't show. 我希望面板出现并跨越框架的宽度和高度,但不会显示。
I don't know what was wrong there but it worked 我不知道那里出了什么问题,但它有效
mainWindow.add(panelMain, BorderLayout.CENTER);
mainWindow.add(mb);
The above two statements are the same thing. 以上两个语句是同一回事。 If you don't specify a constraint, then the BorderLayout will use "CENTER" by default. 如果您未指定约束,则默认情况下BorderLayout将使用“ CENTER”。
Only a single component can be added to any give area in the BorderLayout. 只能将单个组件添加到BorderLayout中的任何给定区域。
So the adding of the menubar replaces the existing component, which is your panelMain. 因此,添加菜单栏将替换现有组件,即您的panelMain。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.