两个类之间的事件处理

时间:2013-06-23 07:53:28

标签: java swing jbutton actionlistener cardlayout

我一直在寻找几个小时,但仍然没有找到我需要的东西,也没有任何教程/帮助指南/论坛指出我正确的方向。

我需要两个单独的类来实现动作侦听器。我可以从另一个班级去一个班级,但是无法找到回到主班级的方法。主类使用CardLayout来显示第二个类。

是否有任何好的教程,或任何可以帮助/提出我的这个问题的建议?感谢

我最终得到了什么:
主要类别

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyGui extends JFrame {
    //CardLayout
    static CardLayout cardLayout;
    static JPanel cards = new JPanel();

    private void actionPerformed(ActionEvent evt) {
        String cmd = evt.getActionCommand();
        if(cmd.equals("HOME")) {
            cardLayout.show(cards, "Main GUI");
        }
    }

    //main Panel
    JPanel mainP = new JPanel();
    //north panel
    TitleBar tb = new TitleBar();
    JPanel top = new JPanel();
    //JLabel title = new JLabel("Title Bar", JLabel.CENTER);

    //center panel
    JButton widgetBox = new JButton("<--   Touch Screen Widget Area   -->");

    //east panel
    JPanel east = new JPanel();
    JPanel picPanel = new JPanel(new GridLayout(1,1));
    JButton pic = new JButton("Pic goes here");
    JPanel settingsPanel = new JPanel(new GridLayout(1,2));
    JButton appButton = new JButton("Apps");
    JButton settingButton = new JButton("Settings");

    //south panel
    JPanel south = new JPanel();
    JTextField rssFeed = new JTextField("RSS FEED");

    //gui panels
    MyGui2 widgPanel;
    MyGui3 appPanel;
    MyGui4 setPanel;


    public MyGui() {        
        //main layout
        super();
        BorderLayout main = new BorderLayout();
        mainP.setLayout(main);

        //gui init
        widgPanel = new MyGui2();
        appPanel = new MyGui3();
        setPanel = new MyGui4();

        //CardLayout
        final JFrame frame = new JFrame();
        JPanel contentPane = (JPanel) frame.getContentPane();
        cards.setLayout(cardLayout = new CardLayout());

        cards.add("Main GUI", mainP);
        cards.add("Settings GUI", setPanel);
        cards.add("App GUI", appPanel);
        cards.add("Widget GUI", widgPanel);
        cardLayout.show(cards, "Main GUI");
        contentPane.add(cards);

        //north panel
        BorderLayout header = new BorderLayout();
        top.setLayout(header);
        top.add(tb);
        mainP.add(top, BorderLayout.NORTH);

        //center panel
        widgetBox.setEnabled(true);

        widgetBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                Object source = evt.getSource();

                if(source == widgetBox) {
                    frame.setSize(325, 500);
                    cardLayout.show(cards, "Widget GUI");
                }
            }

        });
        mainP.add(widgetBox, BorderLayout.CENTER);

        //east panel
        BoxLayout eastPanel = new BoxLayout(east, BoxLayout.Y_AXIS);
        east.setLayout(eastPanel);
        picPanel.add(pic);
        pic.setEnabled(false);
        pic.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                rssFeed.setText("Add Picture");
            }

        }
        );
        picPanel.setPreferredSize(new Dimension(175, 150));

        //app button action
        settingsPanel.add(appButton);
        appButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                //appPane
                Object source = evt.getSource();
                if(source == appButton) {
                    cardLayout.show(cards, "App GUI");
                }
            }
        });

        //settings button action
        settingsPanel.add(settingButton);
        settingButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                //setPane
                Object source = evt.getSource();
                if(source == settingButton) {
                    cardLayout.show(cards, "Settings GUI");
                }
            }
        });

        east.add(picPanel);
        east.add(settingsPanel);
        mainP.add(east, BorderLayout.EAST);

        //south panel
        BorderLayout footer = new BorderLayout();
        south.setLayout(footer);
        south.add(rssFeed);
        rssFeed.setEditable(false);
        rssFeed.setHorizontalAlignment(JTextField.CENTER);
        mainP.add(south, BorderLayout.SOUTH);


        //setLookAndFeel();
        frame.setResizable(false);
        frame.setSize(500, 325);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true);
    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
                    );
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException exc) {
            //ignore error
        }
    }

    public static void main(String[] args) {
        MyGui gui = new MyGui();
    }
}

第二课 -

bottom.setLayout(footer);
        bottom.add(homeButton);
            //home button action
        homeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
            System.out.println("Go Back Home");
            }
        });

2 个答案:

答案 0 :(得分:3)

您可以在MyGui中创建一个方法,将整个应用程序定向到Home

public static void showHome()
{
  cardLayout.show(cards, "Main GUI");
}

SecondClass中,在actionPerformed中使用此方法,如下所示:

homeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        MyGui.showHome();
    }
});

修改
在上面的代码中,两个类之间仍然存在一些耦合。 还有另一种方法可以实现相同的任务并消除耦合问题: 在创建MyGui对象时SecondClass内,在构造函数中将MyGui当前对象的引用传递给它,如下所示:

public MyGui() {
...
widgPanel = new MyGui2(this);
....
}

showHome中创建非静态方法MyGui

public void showHome()
{
  cardLayout.show(cards, "Main GUI");
}

按如下方式更改MyGui2课程:

class MyGui2{
MyGui mg;//create an instance variable of MyGui

    MyGui2(MyGui mg)
    {
      this.mg = mg;
      //Do all other stuffs here.
    }

actionPerformed范围内,您可以按以下步骤操作:

homeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        mg.showHome();
    }
});

答案 1 :(得分:1)

您应该确保Second引用Main的实例,以便他可以通知事件。