CardLayout类具有可以满足您的要求的有用的API。使用类似的方法
next(), first(),last()可能会有所帮助。我准备了一个简单的演示,该演示在父面板和/或框架中更改面板。看一看:
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class PanelChanger implements ActionListener{ JPanel panels; public void init(Container pane) { JButton switcher = new JButton("Switch Active Panel!"); switcher.addActionListener(this); JPanel login = new JPanel(); login.setBackground(Color.CYAN); login.add(new JLabel("Welcome to login panel.")); JPanel another = new JPanel(); another.setBackground(Color.GREEN); another.add(new JLabel("Yeah, this is another panel.")); panels = new JPanel(new CardLayout()); panels.add(login); panels.add(another); pane.add(switcher, BorderLayout.PAGE_START); pane.add(panels, BorderLayout.CENTER); } public void actionPerformed(ActionEvent evt) { CardLayout layout = (CardLayout)(panels.getLayout()); layout.next(panels); } public static void main(String[] args) { Jframe frame = new Jframe("CardLayoutDemo"); PanelChanger changer = new PanelChanger(); changer.init(frame.getContentPane()); frame.pack(); frame.setVisible(true); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)