例如
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(); } }); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)