从JFrame清除JPanel

时间:2015-09-24 21:45:02

标签: java user-interface jframe jpanel

我正在为我们需要创建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);
}
}

3 个答案:

答案 0 :(得分:3)

您可以使用:

jpanel.removeAll();

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

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

答案 1 :(得分:2)

使用Card Layout。您可以根据需要交换面板。

本教程有一个工作示例。

答案 2 :(得分:0)

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