简体   繁体   English

无法将JPanel添加到JFrame的构造函数之外的JLayeredPane中

[英]JPanel can't be added to JLayeredPane outside the JFrame's constructor

I want to add a JPanel to a JLayeredPane when the user clicks enter, but the JPanel is not showing up. 我想在用户单击Enter时将JPanel添加到JLayeredPane,但是JPanel没有显示。 If i add the JPanel to the JLayeredPane in the JFrame's constructor, everything is working correctly. 如果我将JPanel添加到JFrame的构造函数中的JLayeredPane,则一切正常。

What do i have to do, that the JPanel is showing up, when the user clicks 'enter'? 当用户单击“输入”时,JPPanel出现了,我该怎么办?

Here's the code: 这是代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class Test extends javax.swing.JFrame {

    public static void main(String[] args) {
        Test test = new Test();
        test.setSize(800, 500);
        test.setVisible(true);
    }

    public Test() {
        setLayout(new BorderLayout());

        //LayeredPane on JFrame
        JLayeredPane jlp = new JLayeredPane();
        jlp.setLayout(new BorderLayout());
        this.add(jlp, BorderLayout.CENTER);

        //Adds a JPanel to the North
        JPanel jPNorth = new JPanel();
        jPNorth.setBackground(Color.RED);
        jlp.add(jPNorth, BorderLayout.NORTH, JLayeredPane.DEFAULT_LAYER);

        //Adds Enter Keybinding
        InputMap key_input_map = jlp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap key_action_map = jlp.getActionMap();

        key_input_map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "add_jpanel");

        key_action_map.put("add_jpanel", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                JPanel jPSouth = new JPanel();
                jPSouth.setBackground(Color.YELLOW);
                jlp.add(jPSouth, BorderLayout.SOUTH, JLayeredPane.DEFAULT_LAYER);

                System.out.println("enter");
            }
        });
    }
}

Thanks, Jumagoro 谢谢Jumagoro

You did everything correct, the solution is very simple. 您所做的一切都正确,解决方案非常简单。 When you dynamically add swing Component s to each other, you must to use component.repaint(); 当您彼此动态添加swing Component ,必须使用component.repaint(); and component.revalidate(); component.revalidate(); to redraw the elements. 重画元素。 Add the two commands after everything is added. 添加完所有内容后,添加两个命令。 So your actionPerformed method should be changed to the following: 因此,您的actionPerformed方法应更改为以下内容:

public void actionPerformed(ActionEvent e) {
    JPanel jPSouth = new JPanel();
    jPSouth.setBackground(Color.YELLOW);
    jlp.add(jPSouth, BorderLayout.SOUTH, JLayeredPane.DEFAULT_LAYER);
    //Need these to here!
    jlp.repaint();
    jlp.revalidate();
    System.out.println("enter");
}

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

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