繁体   English   中英

从 JFrame 中清除 JPanel

[英]Clear a JPanel from a JFrame

我正在为我们需要创建一个 JComboBox 的班级做作业,每个选项都会打开一个新窗口,您可以在这些新窗口上做任何您想做的事情。 请记住,我对 GUI 和 Java 都很陌生,以防我的问题很愚蠢。

我有一个问题和一个问题...

我的问题:
当用户选择“The Matrix”选项时,会弹出一个带有引号和两个按钮的新窗口。 现在我有两个 JPanels(panel 和 panel2)面板将引用添加到 NORTH 位置,然后 panel2 使用 BorderLayout 将两个按钮添加到 CENTER 位置。 我的问题是我这样做是否正确......我可以只使用面板来添加引用和按钮,还是有必要为添加到 JFrame 的单独项目创建单独的面板? 当我将它们都添加到同一个面板时,当我运行程序时,引用不在窗口上。

    panel.add(matrixQuote);
    newFrame.add(panel, BorderLayout.NORTH);

当它没有出现时,我就是这样拥有它^^^

我在清除 JFRAME 时遇到了问题
我正在尝试将 ActionListener 添加到 bluePill 按钮,而不是打开另一个新窗口,我想我可以在按下按钮时清除现有窗口中的所有内容,然后在所述窗口上显示新内容。 我能找到的唯一信息是我如何在下面的 actionPerformed 方法中使用它。 我将直接在下面发布我正在谈论的内容的片段,然后在下面发布我的所有代码,以防万一。

我所有的代码...

public class MultiForm extends JFrame{

    private JComboBox menu;
    private JButton bluePill;
    private JButton redPill;
    private JLabel matrixQuote;
    private int matrixSelection;
    private JFrame newFrame;
    private JPanel panel;
    private JPanel panel2;
    private static String[] fileName = {"", "The Matrix", "Another Option"};

public MultiForm() {
    super("Multi Form Program");        
    setLayout(new FlowLayout());
    menu = new JComboBox(fileName);
    add(menu);

    TheHandler handler = new TheHandler();
    menu.addItemListener(handler);  

}

public void matrixPanel() {

    TheHandler handler = new TheHandler();
    //Create a new window when "The Matrix" is clicked in the JCB
    newFrame = new JFrame();
    panel = new JPanel();
    panel2 = new JPanel();

    newFrame.setLayout(new FlowLayout());
    newFrame.setSize(500, 300);
    newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);      

    matrixQuote = new JLabel("<html>After this, there is no turning back. "
            + "<br>You take the blue pill—the story ends, you wake up "
            + "<br>in your bed and believe whatever you want to believe."
            + "<br>You take the red pill—you stay in Wonderland, and I show"
            + "<br>you how deep the rabbit hole goes. Remember: all I'm "
            + "<br>offering is the truth. Nothing more.</html>");

    panel2.add(matrixQuote);
    newFrame.add(panel2, BorderLayout.NORTH);

    //Blue pill button and picture.

    Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
    bluePill = new JButton("Blue Pill", bp);
    panel2.add(bluePill);   
    bluePill.addActionListener(handler);

    //Red pill button and picture
    Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
    redPill = new JButton("Red Pill", rp);
    panel2.add(redPill);

    newFrame.add(panel2, BorderLayout.CENTER);      
    newFrame.setVisible(true);
}

private class TheHandler implements ItemListener, ActionListener{

    public void itemStateChanged(ItemEvent IE) {
        //listen for an item to be selected.
        if(IE.getStateChange() == ItemEvent.SELECTED) {
            Object selection = menu.getSelectedItem();

            if("The Matrix".equals(selection)) {
                matrixPanel();
            }
            else if("Another Option".equals(selection)) {   
            }
        }   
    }

    public void actionPerformed(ActionEvent AE) {
        if(AE.getSource() == bluePill) {
            newFrame.remove(panel);         
            newFrame.remove(panel2);
            newFrame.repaint();
        }
    }   
}

//MAIN
public static void main(String[] args) {
    MultiForm go = new MultiForm();
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(400, 200);
    go.setVisible(true);
}
}

您可以使用:

jpanel.removeAll();

要么使用JComponent本身删除某个JComponent ,如:

JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
frame.remove(panel);

使用卡片布局 您可以根据需要交换面板。

本教程有一个工作示例。

panel.getGraphics().clearRect(0, 0, panel.getWidth(), panel.getHeight());

暂无
暂无

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

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