JFrame删除JPanels并添加新的JPanel

JFrame删除JPanels并添加新的JPanel,第1张

JFrame删除JPanels并添加新的JPanel

例如

import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Myframe extends Jframe {    private static final long serialVersionUID = 1L;    public Myframe() {        final JPanel parentPanel = new JPanel();        parentPanel.setLayout(new BorderLayout(10, 10));        final JPanel childPanel1 = new JPanel();        childPanel1.setBackground(Color.red);        childPanel1.setPreferredSize(new Dimension(300, 40));        final JPanel childPanel2 = new JPanel();        childPanel2.setBackground(Color.blue);        childPanel2.setPreferredSize(new Dimension(800, 600));        JButton myButton = new JButton("Add Component ");        myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {     parentPanel.remove(childPanel1);     parentPanel.add(childPanel2, BorderLayout.CENTER);     parentPanel.revalidate();     parentPanel.repaint();     pack(); }        });        setTitle("My Empty frame");        setLocation(10, 200);        setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        parentPanel.add(childPanel1, BorderLayout.CENTER);        parentPanel.add(myButton, BorderLayout.SOUTH);        add(parentPanel);        pack();        setVisible(true);    }    public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable() { @Override public void run() {     Myframe myframe = new Myframe(); }        });    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5561958.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存