Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 两个类之间的事件处理_Java_Swing_Jbutton_Actionlistener_Cardlayout - Fatal编程技术网

Java 两个类之间的事件处理

Java 两个类之间的事件处理,java,swing,jbutton,actionlistener,cardlayout,Java,Swing,Jbutton,Actionlistener,Cardlayout,我已经搜索了几个小时,但仍然没有找到我需要的东西,也没有任何教程/帮助指南/论坛为我指明正确的方向 我需要两个独立的类来实现动作侦听器。我可以从一节课转到另一节课,但我想不出回到主课的方法。主类使用卡片布局显示第二个类 有什么好的教程,或者有什么可以帮助/给我这个问题提建议的吗?谢谢 我的结局是: 主类- import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.even

我已经搜索了几个小时,但仍然没有找到我需要的东西,也没有任何教程/帮助指南/论坛为我指明正确的方向

我需要两个独立的类来实现动作侦听器。我可以从一节课转到另一节课,但我想不出回到主课的方法。主类使用
卡片布局
显示第二个类

有什么好的教程,或者有什么可以帮助/给我这个问题提建议的吗?谢谢

我的结局是:
主类-

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

您应该确保
Second
引用了
Main
的一个实例,这样他就可以向它通知一个事件。

您可以在
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();
    }
});
public MyGui() {
...
widgPanel = new MyGui2(this);
....
}
class MyGui2{
MyGui mg;//create an instance variable of MyGui

    MyGui2(MyGui mg)
    {
      this.mg = mg;
      //Do all other stuffs here.
    }
homeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        mg.showHome();
    }
});
编辑
在上面的代码中,两个类之间仍然存在一些耦合。 实现同样的任务和消除耦合问题还有另一种方法: 在
MyGui
中,当您创建
SecondClass
对象时,将
MyGui
当前对象的引用在构造函数中传递给它,如下所示:

homeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        MyGui.showHome();
    }
});
public MyGui() {
...
widgPanel = new MyGui2(this);
....
}
class MyGui2{
MyGui mg;//create an instance variable of MyGui

    MyGui2(MyGui mg)
    {
      this.mg = mg;
      //Do all other stuffs here.
    }
homeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        mg.showHome();
    }
});
MyGui
中创建非静态方法
showHome

public void showHome()
{
  cardLayout.show(cards, "Main GUI");
}
并将
MyGui2
类更改如下:

homeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        MyGui.showHome();
    }
});
public MyGui() {
...
widgPanel = new MyGui2(this);
....
}
class MyGui2{
MyGui mg;//create an instance variable of MyGui

    MyGui2(MyGui mg)
    {
      this.mg = mg;
      //Do all other stuffs here.
    }
homeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        mg.showHome();
    }
});
actionPerformed
中,您可以执行以下操作:

homeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        MyGui.showHome();
    }
});
public MyGui() {
...
widgPanel = new MyGui2(this);
....
}
class MyGui2{
MyGui mg;//create an instance variable of MyGui

    MyGui2(MyGui mg)
    {
      this.mg = mg;
      //Do all other stuffs here.
    }
homeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        mg.showHome();
    }
});

您是否想过实现观察器/侦听器?更多的细节可能会有帮助,或者更好地解释这个问题什么是二等舱?如何在
Main
类中创建和调用其对象,以及在何处创建和调用?显示
Main
classI的完整代码我曾尝试使用侦听器,但也无法使用。基本上,在我的主类中,我可以使用cardlayout导航到另一个类(卡),但不确定如何返回到原始卡。第二个类是作为卡添加的另一个gui/面板。是否实现了通知/更新方法?引入静态方法很少是个好主意。它增加了两个类之间的耦合,使得维护和更改代码变得非常困难。@mariosangiorgio:是的,你是对的。这正是我所考虑的。查看我的更新。在这种情况下,它不会产生太多耦合。现在它更好了,但我会更进一步,完全删除将
静态
关键字传递到
cardLayout
二级
。谢谢。它起作用了。现在,我们已经弄清楚如何将其恢复到原始尺寸。必须恢复到原始尺寸的是什么?