2012-02-22 91 views
0

我想知道如何在運行時更改JFrame的內容。就像添加一個新的JPanel並刪除舊的JPanel一樣。如何在運行時更改JFrame中的JPanel

+1

出了什麼問題'JFrame.remove()','JFrame.add()'? – DNA 2012-02-22 19:17:41

+1

可能重複[如何更改JFrame中的JPanel?](http://stackoverflow.com/questions/218155/how-do-i-change-jpanel-inside-a-jframe-on-the -fly) – DNA 2012-02-22 19:30:49

+0

http://stackoverflow.com/questions/9371460/how-can-i-stack-overlay-jpanels-in-java/9377623#9377623可能的重複? – Juvanis 2012-02-22 19:48:10

回答

2

您可以考慮使用CardLayout改變一幀中的活動面板。

0

在運行時在此改變的JPanel是代碼:

package stack; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class RemoveAndAddPanel implements ActionListener{ 
    JFrame frame; 
    JPanel firstPanel; 
    JPanel secondPanel; 
    JPanel controlPanel; 
    JButton nextButton; 
    JPanel panelContainer; 
    JButton preButton; 
    JPanel contentPane; 

    public RemoveAndAddPanel() { 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     firstPanel = new JPanel(); 
     firstPanel.add(new JLabel("FirstPanel")); 
     firstPanel.setPreferredSize(new Dimension(100,100)); 

     secondPanel = new JPanel(); 
     secondPanel.add(new JLabel("Second panel")); 
     secondPanel.setPreferredSize(new Dimension(100,100)); 

     panelContainer = new JPanel(); 
     contentPane = new JPanel(new BorderLayout()); 

     nextButton = new JButton("Next panel"); 
     preButton = new JButton("PreButton"); 
     controlPanel = new JPanel(); 

     nextButton.addActionListener(this); 
     preButton.addActionListener(this); 
     preButton.setEnabled(false); 

     controlPanel.add(preButton); 
     controlPanel.add(nextButton); 

     panelContainer.setLayout(new BorderLayout()); 
     panelContainer.add(firstPanel,BorderLayout.CENTER); 
     contentPane.add(controlPanel, BorderLayout.SOUTH); 
     contentPane.add(panelContainer,BorderLayout.CENTER); 

     frame.setContentPane(contentPane); 

     frame.setVisible(true); 
     frame.setSize(300,100); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == nextButton) { 
      panelContainer.removeAll(); 
      panelContainer.setSize(0,0); 
      panelContainer.setSize(secondPanel.getSize()); 
      panelContainer.add(secondPanel,BorderLayout.CENTER); 
      panelContainer.revalidate(); 


      nextButton.setEnabled(false); 
      preButton.setEnabled(true); 
     } 
     if (e.getSource() == preButton) { 
      panelContainer.removeAll(); 
      panelContainer.setSize(0,0); 
      panelContainer.setSize(firstPanel.getSize()); 
      panelContainer.add(firstPanel,BorderLayout.CENTER); 
      nextButton.setEnabled(true); 
      preButton.setEnabled(false); 
     } 
    } 
    public static void main(String args[]) { 
     new RemoveAndAddPanel(); 
    } 
}