简体   繁体   English

从 JFrame 中清除 JPanel

[英]Clear a JPanel from a JFrame

I'm working on an assignment for class where we need to create a JComboBox and each option opens a new window where you can do whatever you want on those new windows.我正在为我们需要创建一个 JComboBox 的班级做作业,每个选项都会打开一个新窗口,您可以在这些新窗口上做任何您想做的事情。 Please keep in mind I'm very new to GUI and new to Java in general, in case my questions are dumb.请记住,我对 GUI 和 Java 都很陌生,以防我的问题很愚蠢。

I have a question and an issue...我有一个问题和一个问题...

My question:我的问题:
When the user selects "The Matrix" option a new window pops up with a quote and two buttons.当用户选择“The Matrix”选项时,会弹出一个带有引号和两个按钮的新窗口。 Right now I have two JPanels (panel and panel2) panel adds the quote to the NORTH position and then panel2 adds the two buttons to the CENTER position both using BorderLayout.现在我有两个 JPanels(panel 和 panel2)面板将引用添加到 NORTH 位置,然后 panel2 使用 BorderLayout 将两个按钮添加到 CENTER 位置。 My question is am I doing this correctly...Could I use just panel to add the quote and the buttons or is it necessary to create separate panels for separate items being added to the JFrame?我的问题是我这样做是否正确......我可以只使用面板来添加引用和按钮,还是有必要为添加到 JFrame 的单独项目创建单独的面板? When I had them both added to the same panel the quote was not on the window when I ran the program.当我将它们都添加到同一个面板时,当我运行程序时,引用不在窗口上。

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

That's how I had it when it wasn't showing up ^^^当它没有出现时,我就是这样拥有它^^^

I GOT THE ISSUE WITH CLEARING THE JFRAME FIXED我在清除 JFRAME 时遇到了问题
I am trying to add an ActionListener to the bluePill button and instead of opening another new window I thought I could clear everything from the existing window when the button is pressed and then display something new on said window.我正在尝试将 ActionListener 添加到 bluePill 按钮,而不是打开另一个新窗口,我想我可以在按下按钮时清除现有窗口中的所有内容,然后在所述窗口上显示新内容。 The only info I could find on this is how I have it in the actionPerformed method below.我能找到的唯一信息是我如何在下面的 actionPerformed 方法中使用它。 I'll post a snippet of what I'm talking about directly below and then all my code below that just in case.我将直接在下面发布我正在谈论的内容的片段,然后在下面发布我的所有代码,以防万一。

All my code...我所有的代码...

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);
}
}

You can use:您可以使用:

jpanel.removeAll();

Either to delete a certain JComponent by using the JComponent itself like:要么使用JComponent本身删除某个JComponent ,如:

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

Use aCard Layout .使用卡片布局 You can swap panels as required.您可以根据需要交换面板。

The tutorial has a working example.本教程有一个工作示例。

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

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

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