Implementing back/forward buttons in Swing

后端 未结 3 659
生来不讨喜
生来不讨喜 2020-11-22 01:00

I have a quick question.

I\'m getting a little bit of experience with Swing and the easiest way to do this was to draw up a reasonably big GUI.

As part of t

3条回答
  •  忘了有多久
    2020-11-22 01:43

    The way I usually do it is as follows:

    1. I've got a StepManager class (write it once, use it forever) which handles all logic related to the steps. It got methods like next(), previous(), reset(), isFirst() and isLast().

    2. I've then got 'Next' and 'Previous' buttons with appropriate actions (or whatever you choose to use to listen for user interaction).

    3. The code related to the 'Next' button calls stepManager.next() to retrieve the index for the next step. Then (when I've got the next step) I simply invoke (another method) showStep(int index) to display the actual step user interface corresponding to the current step index.

    Each step is a separate 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();
    }
    

提交回复
热议问题