在Swing中实现后退/前进按钮
public class Main_Frame extends JFrame{ static JPanel nav_bar_panel; JButton home; JButton back; JButton forward; JPanel currentPanel; static Stack<JPanel> previousPanels; static Stack<JPanel> forwardPanels; public Main_Frame(){ super("DEMO"); setSize(800,600); setLayout(new BorderLayout()); setVisible(true); add(nav_bar(), BorderLayout.NORTH); currentPanel = init_display(); add(currentPanel, BorderLayout.CENTER); previousPanels = new Stack<JPanel>(); forwardPanels = new Stack<JPanel>(); } private JPanel nav_bar(){ ButtonPressHandler handler = new ButtonPressHandler(); nav_bar_panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10)); back = new JButton("Back"); back.addActionListener(handler); home = new JButton("Home"); home.addActionListener(handler); forward = new JButton("Forward"); forward.addActionListener(handler); nav_bar_panel.add(back); nav_bar_panel.add(home); nav_bar_panel.add(forward); return nav_bar_panel; } private JPanel init_display(){ Home_Panel home_panel = new Home_Panel(); return home_panel; } public void change_display(JPanel myPanel){ invalidate(); remove(currentPanel); previousPanels.push(currentPanel); currentPanel = myPanel; add(currentPanel); validate(); }
牛魔王的故事
相关分类