java - 从 JFrame 中清除 JPanel

标签 java user-interface jframe jpanel

我正在为我们需要创建一个 JComboBox 的类(class)分配作业,每个选项都会打开一个新窗口,您可以在这些新窗口上做任何您想做的事情。请记住,我是 GUI 的新手,一般来说是 Java 的新手,以防我的问题很愚蠢。

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

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

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

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

我在清除已修复的 JFRAME 时遇到了问题
我正在尝试向 bluePill 按钮添加一个 ActionListener,而不是打开另一个新窗口,我认为我可以在按下按钮时清除现有窗口中的所有内容,然后在所述窗口上显示新内容。我能找到的唯一信息是我如何在下面的 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);
}
}

最佳答案

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

教程有一个工作示例。

关于java - 从 JFrame 中清除 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32771437/

相关文章:

java - 仅当 JFrame 在屏幕上可见时才执行代码提前执行

java - 为什么在写入 ObjectOutputStream 时必须首先调用 defaultWriteObject 函数?

java - 为什么在这种情况下 List<String> 总是返回大小 1

java - 为什么 ivyde 看不到我的依赖项之一?

c# - 数据 GridView 中的数据不会直观地显示 C# SQL

java - JComponent.setBounds 最后一个 JComponent 在错误的位置

java - 在关闭 JFrame 之前我将如何执行函数

java - 从 ArrayList 读取

user-interface - 在脚本运行时刷新表单上的数据的后台作业?

java - 传递在一个 JFrame 的文本字段中输入的值作为另一个 JFrame 的输入参数