当我点击JToggleButton时,我需要显示JPanel

时间:2016-04-15 03:56:56

标签: java swing events event-handling

我有一个JFrame分为两个,我有一个JPanel,上面有一些JToggleButtons。另一方面,我需要在单击其中一个JToggleButtons时显示正确的JPanel。现在我只使用一个线程来做到这一点。

这是JFrame

public class AppFrame extends JFrame{
    private JPanel content ;
    private JPanel menu;

    public AppFrame() {
        super("Title");
        setLayout(BorderLayout());
        //content is gonna hold JPanel1 or JPanel2
        content = new JPanel();
        menu = new menu();
        this.add(menu, BorderLayout.WEST);
        this.add(content , BorderLayout.CENTER);
    }

    public void setContet(JPanel activePanel){
        content = activePanel;
        this.add(content , BorderLayout.CENTER);
    }
}

这是持有JToggleButtons(MenuJPanel)

的JPanel
public class MenuJPanel extends JPanel{
private final LayoutManager innerLayout = new GridLayout(3,1,1,1);
private final JToggleButton button1;
private final JToggleButton button1;

public MenuPanel() {
    super();
    SwitchHandler sHandler = new SwitchHandler();
    this.setLayout(innerLayout);
    this.add(button1);
    this.add(button2);
    button1.addActionListener(sHandler);
    button2.addActionListener(sHandler);
}

class SwitchHandler implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button1){
            button2.setSelected(false);
            //at this point I want the frame to display JPanel1
            //I tried this
            AppFrame frame = (AppFrame)getTopLevelAncestor();
            frame.setContet(new JPanel1());
        } else {
            button1.setSelected(false);
            //at this point I want the frame to display JPanel2                
            //I tried this
            AppFrame frame = (AppFrame)getTopLevelAncestor();
            frame.setContet(new JPanel2());
        }
    }
}

有人能告诉我如何在触发事件时在框架中打开这些面板。 感谢

1 个答案:

答案 0 :(得分:1)

基本答案是使用CardLayout,有关详细信息,请参阅How to Use CardLayout

你有很多方法可以做到这一点,但我更喜欢使用某种“导航控制器”,它负责实际处理使其工作的脏细节,这意味着如果你决定改变切换视图的方式,代码的重置并不关心,例如......

import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public enum Page {
        MENU("menu"),
        HERE("here"),
        THERE("there");

        private String pageName;

        private Page(String name) {
            pageName = name;
        }

        public String getPageName() {
            return pageName;
        }

    }

    public interface Navigator {
        public void showPage(Page page);
    }

    public class TestPane extends JPanel implements Navigator {

        private CardLayout cardLayout;

        public TestPane() {
            cardLayout = new CardLayout();
            setLayout(cardLayout);
            add(new MenuPanel(this), Page.MENU.getPageName());
            add(new StuffOverHere(), Page.HERE.getPageName());
            add(new StuffOverThere(), Page.THERE.getPageName());
            showPage(Page.MENU);
        }

        @Override
        public void showPage(Page page) {
            cardLayout.show(this, page.getPageName());
        }

    }

    public class StuffOverHere extends JPanel {

        public StuffOverHere() {
            setLayout(new GridBagLayout());
            JLabel label = new JLabel("Over here");
            add(label);
        }

    }
    public class StuffOverThere extends JPanel {

        public StuffOverThere() {
            setLayout(new GridBagLayout());
            JLabel label = new JLabel("Over there");
            add(label);
        }

    }

    public class MenuPanel extends JPanel {

        private final LayoutManager innerLayout = new GridLayout(3, 1, 1, 1);
        private final JToggleButton button1 = new JToggleButton("Stuff over here");
        private final JToggleButton button2 = new JToggleButton("Stuff over there");

        private Navigator navigator;

        public MenuPanel(Navigator navigator) {
            super();
            SwitchHandler sHandler = new SwitchHandler();
            this.setLayout(innerLayout);
            this.add(button1);
            this.add(button2);
            button1.addActionListener(sHandler);
            button2.addActionListener(sHandler);
            this.navigator = navigator;
        }

        class SwitchHandler implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == button1) {
                    navigator.showPage(Page.HERE);
                } else {
                    navigator.showPage(Page.THERE);
                }
            }
        }
    }
}