简体   繁体   English

无法动态添加JPanel到JFrame

[英]Can't add JPanel to JFrame on the fly

I have to views: 我要观点:

  1. MainWindowView (extends JFrame) MainWindowView(扩展JFrame)
  2. ScanOptimisationView (extends JPanel) ScanOptimisationView(扩展JPanel)

So, I have the combobox in MainWindowView class. 所以,我在MainWindowView类中有组合框。 And I create ActionListener and bind it to this combobox. 然后我创建ActionListener并将其绑定到这个组合框。 actionPerfomed() method of this ActionListener tries to add ScanOptimisationView panel to main window frame. 此ActionListener的actionPerfomed()方法尝试将ScanOptimisationView面板添加到主窗口框架。 Here is the code: 这是代码:

package ru.belaventcev.view;

import java.awt.Container;

public class MainWindowView extends JFrame{
    private int frmHeight = 525;
    private int frmWidth  = 650;

    public Container frmContainer;

    public static JButton btnCalc;

    public static JComboBox cbMethods;

    public MainWindowView(){
        setPreferredSize(new Dimension(frmWidth, frmHeight));
        setSize(frmWidth, frmHeight);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        frmContainer = getContentPane();
        frmContainer.setLayout(new MigLayout("", "[grow,center]", "[::30px,grow,center][grow,center][::500px,grow,center][::25px,grow,center]"));
        cbMethods = new JComboBox();
        cbMethods.setModel(new DefaultComboBoxModel(new JPanel[] {new ScanOptimisationView()}));
        cbMethods.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JPanel temp = (JPanel) cbMethods.getSelectedItem();
                frmContainer.add(temp, "cell 0 1,span");
            }
        });

        /*
         * If I uncomment this, panel is shown!
        JPanel temp = (JPanel) cbMethods.getSelectedItem();
        frmContainer.add(temp, "cell 0 1");
        */

        frmContainer.add(cbMethods, "cell 0 0,growx");



        btnCalc = new JButton("Расчитать");
        frmContainer.add(btnCalc, "cell 0 3,alignx right");

    }
}

Could you help me to understand - why panel doesn't shown with code in actionPerformed(), but it is shown, when I use the code below? 你能帮我理解一下 - 为什么面板没有在actionPerformed()中显示代码,但是当我使用下面的代码时会显示它?

In the non-working case, after your actionListener calls frmContainer.add() , you need to call frmContainer.validate() . 在非工作的情况下,在actionListener调用frmContainer.add() ,需要调用frmContainer.validate() From the Javadocs for Container.add(): 来自Container.add()的Javadocs:

"If a component has been added to a container that has been displayed , validate must be called on that container to display the new component." “如果已将组件添加到已显示的容器中,则必须在该容器上调用validate以显示新组件。”

When you are responding to the click, your container has, obviously, already been displayed. 当您响应点击时,显然已经显示了您的容器。 When you add the JPanel in the constructor your JFrame has not been displayed yet. 当您在构造函数中添加JPanel时,您的JFrame尚未显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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