简体   繁体   English

Japplet中的JPanel

[英]JPanel in JApplet

I need to have one JPanel opened on start. 我需要在开始时打开一个JPanel。 I have a button on that one to open to another JPanel which contains a button to bring me back. 我有一个按钮,打开另一个JPanel ,其中包含一个按钮,可以让我回来。 How do i write action listeners for those buttons. 我如何为这些按钮编写动作侦听器。 I have searched extensively. 我进行了广泛的搜索。 Do I need a JFrame ? 我需要一个JFrame吗? All examples seem to have it. 所有的例子似乎都有。

Regardless of which approach you might take, the basic idea is the same. 无论您采用哪种方法,基本思路都是一样的。 You need to know where to go based on where you are... 您需要根据自己的位置知道去哪里...

To this end, this simple example uses a simple navigation interface to provide movement control for the panels and a List to maintain the order of the components. 为此,这个简单的示例使用简单的导航界面为面板提供移动控制,使用List来维护组件的顺序。

You could just as simply use a queue of some kind, pushing the next panel onto it and popping the last panel of it as you switched views. 你可以简单地使用某种类型的队列,将下一个面板推到它上面并在切换视图时弹出它的最后一个面板。

This is a quick and simple example of CardLayout 这是CardLayout一个简单快速的例子

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SwitchPanel extends JApplet{

    private String currentView;
    private List<String> viewNames;

    @Override
    public void init() {
        final CardLayout cardLayout = new CardLayout();
        setLayout(cardLayout);

        Navigator navi = new Navigator() {

            @Override
            public void next() {
                int index = viewNames.indexOf(currentView);
                if (index > -1) {
                    index++;
                    if (index < viewNames.size()) {
                        currentView = viewNames.get(index);
                        cardLayout.show(getContentPane(), currentView);
                    }
                }
            }

            @Override
            public void previous() {
                int index = viewNames.indexOf(currentView);
                if (index > -1) {
                    index--;
                    if (index >= 0) {
                        currentView = viewNames.get(index);
                        cardLayout.show(getContentPane(), currentView);
                    }
                }
            }
        };

        MainPane mainPane = new MainPane(navi);
        LastPane lastPane = new LastPane(navi);

        viewNames = new ArrayList<>(2);
        viewNames.add("main");
        viewNames.add("last");

        add(mainPane, "main");
        add(lastPane, "last");
        currentView = "main";

        cardLayout.show(getContentPane(), "main");

    }

    public interface Navigator {

        public void next();
        public void previous();

    }

    public class MainPane extends JPanel {

        private Navigator navigator;

        public MainPane(Navigator navi) {
            this.navigator = navi;
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            JButton btn = new JButton("Next >");

            add(new JLabel("Main"), gbc);
            add(btn, gbc);
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    navigator.next();
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }

    public class LastPane extends JPanel {

        private Navigator navigator;

        public LastPane(Navigator navi) {
            this.navigator = navi;
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            JButton btn = new JButton("< Previous");

            add(new JLabel("Last"), gbc);
            add(btn, gbc);
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    navigator.previous();
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM