在Swing中实现后退/前进按钮

时间:2011-04-13 20:02:20

标签: java swing stack

我有一个简单的问题。

我对Swing有一点经验,最简单的方法是绘制一个相当大的GUI。

作为GUI的一部分,我想要前进和后退按钮。我试图采用的方法是实现将当前JPanel推送到堆栈并检索前一个值的方法(是向前或反向的(因此是2个堆栈))。我不能让它工作。也许我会以完全错误的方式解决它,或者堆栈不能以我使用它的方式使用。在任何一种情况下,它真的让我烦恼。我想有可能更简单的方式,如卡片布局,但我认为这种方法应该工作,这是非常烦人的。

值得注意的是,我正在使用JFrame“基类”并根据屏幕更改中央JPanel。导航栏作为“基类”的一部分是常量但是

这个“基类”的代码:

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();
    }

    public void previous_display(){
        if(!previousPanels.empty()){
            invalidate();
            remove(currentPanel);
            forwardPanels.push(currentPanel);
            currentPanel = previousPanels.pop();
            add(currentPanel);
            validate();
        }
    }

    public void forward_display(){
        if(!forwardPanels.empty()){
            invalidate();
            remove(currentPanel);
            previousPanels.push(currentPanel);
            currentPanel = forwardPanels.pop();
            add(currentPanel);
            validate();
        }
    }

    private class ButtonPressHandler implements ActionListener 
       {
          public void actionPerformed( ActionEvent event )
          {
              if(event.getSource() == back){
                  previous_display();
                  System.out.print("You selected back");
              } else if(event.getSource() == forward){
                  forward_display();
                  System.out.print("You selected forward");
              }
          } // end method actionPerformed
       } // end private inner class TextFieldHandler

}

3 个答案:

答案 0 :(得分:15)

以下是使用CardLayout的示例。

enter image description here

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/questions/5654926 */
public class CardPanel extends JPanel {

    private static final Random random = new Random();
    private static final JPanel cards = new JPanel(new CardLayout());
    private final String name;

    public CardPanel(String name) {
        this.name = name;
        this.setPreferredSize(new Dimension(320, 240));
        this.setBackground(new Color(random.nextInt()));
        this.add(new JLabel(name));
    }

    @Override
    public String toString() {
        return name;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 1; i < 9; i++) {
            CardPanel p = new CardPanel("Panel " + String.valueOf(i));
            cards.add(p, p.toString());
        }
        JPanel control = new JPanel();
        control.add(new JButton(new AbstractAction("\u22b2Prev") {

            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.previous(cards);
            }
        }));
        control.add(new JButton(new AbstractAction("Next\u22b3") {

            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.next(cards);
            }
        }));
        f.add(cards, BorderLayout.CENTER);
        f.add(control, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

答案 1 :(得分:7)

  做任何我可以重复使用的想法是一个好主意。 Pity Swing没有内置此功能

查看Card Layout Actions,可能会尝试使卡片布局更容易使用。

答案 2 :(得分:0)

我通常这样做的方式如下:

  1. 我有一个StepManager类(写一次,永远使用它),它处理与步骤相关的所有逻辑。它有像next(),previous(),reset(),isFirst()和isLast()这样的方法。

  2. 然后我通过适当的操作(或者您选择用于监听用户交互的任何内容)获得了“下一个”和“上一个”按钮。

  3. 与“下一步”按钮相关的代码调用stepManager.next()来检索下一步的索引。然后(当我有下一步)我只是调用(另一个方法)showStep(int index)来显示与当前步骤索引相对应的实际步骤用户界面。

  4. 每个步骤都是一个单独的JPanel(Step01,Step02,Step03 ......)。

    public void showStep(int index) {
        ContentPanel.removeAll();
        ContentPanel.setLayout(new BorderLayout());
    
        switch (index) {
            case 0:
                ContentPanel.add(Step01, BorderLayout.CENTER);
                break;
    
            case 1:
                ContentPanel.add(Step02, BorderLayout.CENTER);
                break;
    
            case 2:
                ContentPanel.add(Step03, BorderLayout.CENTER);
                break;
    
            case 3:
                ContentPanel.add(Step04, BorderLayout.CENTER);
    
            }
    
        ContentPanel.validate();
        ContentPanel.repaint();
    }