gpt4 book ai didi

java - 在 JFrame 中看不到 JPanel 它包含在

转载 作者:行者123 更新时间:2023-12-02 09:47:50 27 4
gpt4 key购买 nike

编译后在JFrame中找不到我的JPanel,框架设置为BorderLayout,面板设置为CENTER 但当我运行它时,面板不会出现。我的代码中是否缺少某些内容?

    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 don't know what was wrong there but it worked

mainWindow.add(panelMain, BorderLayout.CENTER);
mainWindow.add(mb);

以上两种说法是同一件事。如果您不指定约束,则 BorderLayout 将默认使用“CENTER”。

只能将单个组件添加到 BorderLayout 中的任何给定区域。

因此,添加菜单栏会替换现有组件,即 panelMain。

关于java - 在 JFrame 中看不到 JPanel 它包含在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56525377/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com