【问题标题】:How to remove a JPanel from a JFrame (Java)如何从 JFrame (Java) 中删除 JPanel
【发布时间】:2015-04-27 19:45:17
【问题描述】:

按下按钮后,我需要从 JFrame 中删除 JPanel。这是我的代码。它还需要添加一个包含不同图像的面板。当我尝试做 p4.remove(p4) 时,它什么也不做,当我做 add(p5, BorderLayout.CENTER);

public class Main extends JFrame {
public Main() {

    //Creates Title Image 
    JLabel title = new JLabel(" ");
    ImageIcon tl = new ImageIcon("title.gif");
    title.setIcon(tl);

    //Creates Start Image
    final JButton start = new JButton("");
    ImageIcon st = new ImageIcon("start.gif");
    start.setIcon(st);

    //Creates Options Image
    JButton options = new JButton("");
    ImageIcon opt = new ImageIcon("options.gif");
    options.setIcon(opt);
    options.setBackground(Color.BLACK);

    //Creates level 0 
    JLabel level0 = new JLabel(" ");
    ImageIcon lvl0 = new ImageIcon("level0.gif");
    level0.setIcon(lvl0);

    //Create first frame for "Start" button
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(1, 1));
    p1.add(start, BorderLayout.CENTER);

    //Create second panel for title label
    JPanel p2 = new JPanel(new BorderLayout());
    p2.setLayout(new GridLayout(1, 3));
    p2.add(title, BorderLayout.WEST);

    //Create third panel for "Options" button
    JPanel p3 = new JPanel(new BorderLayout());
    p3.setLayout(new GridLayout(1, 1));
    p3.add(options, BorderLayout.SOUTH);

    //Creates fourth panel to organize all other primary
    final JPanel p4 = new JPanel(new BorderLayout());
    p4.setLayout(new GridLayout(1, 3));
    p4.add(p1, BorderLayout.WEST);
    p4.add(p2, BorderLayout.CENTER);
    p4.add(p3, BorderLayout.EAST);

    //Creates fifth panel for level 0
    final JPanel p5 = new JPanel(new BorderLayout());
    p5.setLayout(new GridLayout(1, 1));
    p5.add(level0, BorderLayout.CENTER);

    start.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(start.isSelected()) {
                p4.remove(p4);
                add(p5, BorderLayout.CENTER);
            }
            else {
                return;
            }
        }
    });

    //Adds fourth panel to frame
    add(p4, BorderLayout.CENTER);
}

public static void main(String args[]) {
    Main frame = new Main();

    //Finds screen size of monitor
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    //Creates the frame
    frame.setTitle("Cockadoodle Duty: Awakening");
    frame.setSize(screenSize);
    frame.setLocale(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    String background = "#000000";
    frame.setBackground(Color.decode(background));
}

}

【问题讨论】:

标签: java jframe jpanel jbutton


【解决方案1】:

尝试将 ActionListener 中的代码(包括 if 和 else 语句)替换为:

 start.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {


            p4.setVisible(false);
            add(p5, BorderLayout.CENTER);

    }
});

【讨论】:

    猜你喜欢
    • 2011-01-30
    • 1970-01-01
    • 2011-02-23
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多