两个不同程序之间的事件处理

时间:2013-01-20 05:38:32

标签: java swing

我有两个不同的程序MenuWindow.javaSettingsWindow.java 两个GUI,使用Swing制作。 是否可以通过点击SettingsWindow.java中的按钮打开MenuWindow.java

以下是两个代码:

MenuWindow.java

import java.awt.*;
import javax.swing.*;




public class MenuWindow
{
JFrame f ;
JMenuBar m ;
JMenu File , Settings , Timer , Programs , Store , Run , Help ;
JMenuItem m1 , m2 , m3 , m4 , m5 , m6 , m7 , m8 , m9 , m10 , m11 , m12 ;
JButton jb1, jb2, jb3 , jb4 , jb5 , jb6;


public MenuWindow() 
{

    f = new JFrame("Reflow") ;
    f.setSize(600, 600) ;
    m = new JMenuBar() ;
    File = new JMenu("File") ;
    Settings = new JMenu("Settings") ;
    Timer = new JMenu("Timer") ;
    Programs = new JMenu("Programs") ;
    Store = new JMenu("Store");
    Run = new JMenu("Run");
    Help = new JMenu("Help");
    m1 = new JMenuItem("Exit") ;
    m2 = new JMenuItem("Temperature") ;
    m3 = new JMenuItem("Clock") ;
    m4 = new JMenuItem("Profile") ;
    m5 = new JMenuItem(" Weekly Timer") ;
    m6 = new JMenuItem("Preset Programs") ;
    m7 = new JMenuItem("Errors") ;
    m8 = new JMenuItem("Profile") ;
    m9 = new JMenuItem("Auto Run") ;
    m10 = new JMenuItem("Profile Run") ;
    m11 = new JMenuItem("Manual Run") ;
    m12 = new JMenuItem("About");


    ImageIcon SettingsIc = new ImageIcon("settings.png");
    jb1 = new JButton(SettingsIc);
    jb1.setFocusPainted( false );
    //jb1.setBorderPainted(false); 
    jb1.setContentAreaFilled(false); 

    ImageIcon TimerIc = new ImageIcon("clock.png");
    jb2 = new JButton(TimerIc);
    jb2.setFocusPainted( false );
    //jb2.setBorderPainted(false); 
    jb2.setContentAreaFilled(false); 

    ImageIcon ProgramsIc = new ImageIcon("programs.png");
    jb3 = new JButton(ProgramsIc);
    jb3.setFocusPainted( false );
    //jb3.setBorderPainted(false); 
    jb3.setContentAreaFilled(false); 

    ImageIcon StoreIc = new ImageIcon("store.png");
    jb4 = new JButton(StoreIc);
    jb4.setFocusPainted( false );
    //jb4.setBorderPainted(false); 
    jb4.setContentAreaFilled(false); 

    ImageIcon RunIc = new ImageIcon("run.png");
    jb5 = new JButton(RunIc);
    jb5.setFocusPainted( false );
    //jb5.setBorderPainted(false); 
    jb5.setContentAreaFilled(false); 

    ImageIcon ConnectIc = new ImageIcon("connect.png");
    jb6 = new JButton(ConnectIc);
    jb6.setFocusPainted( false );
    //jb6.setBorderPainted(false); 
    jb6.setContentAreaFilled(false); 

    File.add(m1) ;
    Settings.add(m2) ;
    Settings.add(m3) ;
    Settings.add(m4) ;
    Timer.add(m5) ;
    Programs.add(m6) ;
    Store.add(m7);
    Store.add(m8);
    Run.add(m9);
    Run.add(m10);
    Run.add(m11);
    Help.add(m12);
    m.add(File) ;
    m.add(Settings) ;
    m.add(Timer) ;
    m.add(Programs) ;
    m.add(Store) ;
    m.add(Run) ;
    m.add(Help) ;
    f.add(m) ;
    f.add(jb1);
    f.add(jb2);
    f.add(jb3);
    f.add(jb4);
    f.add(jb5);
    f.add(jb6);

    jb1.setToolTipText("Settings");
    jb2.setToolTipText("Weekly Timer");
    jb3.setToolTipText("Program List");
    jb4.setToolTipText("Store");
    jb5.setToolTipText("Run");
    jb6.setToolTipText("Connect");

    System.out.println(System.getProperty("user.dir"));

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setJMenuBar(m);
    f.setLayout(new FlowLayout()) ;
    f.setVisible(true) ;

}


public static void main(String[] arg)
{
        MenuWindow w = new MenuWindow() ;

}

}

SettingsWindow.java

import java.awt.*;
import javax.swing.*;

public class SettingsWindow 
{

JFrame f;
JButton b1, b2, b3; 

public SettingsWindow() 
{
    f = new JFrame ("Settings") ;
    f.setSize(400, 400) ;

    ImageIcon TempuratureIc = new ImageIcon("heat.png");
    b1 = new JButton(TempuratureIc);
    b1.setFocusPainted(false);
    //b1.setBorderPainted(false);
    b1.setContentAreaFilled(false);

    ImageIcon TimeIc = new ImageIcon("timetable.png");
    b2 = new JButton(TimeIc);
    b2.setFocusPainted(false);
    //b2.setBorderPainted(false);
    b2.setContentAreaFilled(false);

    ImageIcon ProfileIc = new ImageIcon("graph.png");
    b3 = new JButton(ProfileIc);
    b3.setFocusPainted(false);
    //b3.setBorderPainted(false);
    b3.setContentAreaFilled(false);

    f.add(b1);
    f.add(b2);
    f.add(b3);

    b1.setToolTipText("Temperature Settings");
    b2.setToolTipText("Time Settings");
    b3.setToolTipText("Profile Settings");

    System.out.println(System.getProperty("user.dir"));

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(new FlowLayout());
    f.setVisible(true);

}

public static void main(String[] arg)
{
    SettingsWindow w = new SettingsWindow() ;
}

}

任何人都可以详细解释一下吗?我是Java的新手。

3 个答案:

答案 0 :(得分:2)

SettingWindow对象的引用传递给MenuWindow。单击MenuWindow类中的按钮操作时使用setVisible方法。

更新 - 1:

将ActionListener添加到jb1(我假设这是调用SettingsWindow的按钮)并在单击按钮时调用SettingWindow

 jb1.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        new SettingWindow();
    }
});

同时从main课程中删除SettingWindow。这不是必需的。

更新 - 2:

在您的代码中,您添加了一个如下代码的按钮:

ImageIcon SettingsIc = new ImageIcon("settings.png");
jb1 = new JButton(SettingsIc);
jb1.setFocusPainted(false);
jb1.setContentAreaFilled(false);
jb1.addActionListener(new ActionListener() {

  @Override
  public void actionPerformed(ActionEvent arg0) {
     new SettingWindow();
  }
 });

所以你添加了一个按钮。单击该按钮时,应调用一个动作。将ActionListener写入此按钮,以便弹出相应的窗口。同样地制作不同的窗口并通过将ActionListener写入所有按钮来调用它们,就像我在上面所说的那样。另请阅读How to write ActionListener

答案 1 :(得分:1)

SettingWindow,类构造函数中,添加mnuwindow=new MenuWindow(this);。 在MenuWindow类构造函数中,添加frame.setVisible(false);。 在mouseClicked(MouseEvent me)添加mnuwindow.getframe().setVisible(true);

答案 2 :(得分:1)

请参阅The Use of Multiple JFrames, Good/Bad Practice?

使用模式JDialogJOptionPane代替第二个“子”窗口。

enter image description here

package menuwindow;

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

public class MenuWindow {

    JFrame f;
    JMenuBar m;
    JMenu File, Settings, Timer, Programs, Store, Run, Help;
    JMenuItem m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
    JButton jb1, jb2, jb3, jb4, jb5, jb6;

    public MenuWindow() {

        f = new JFrame("Reflow");
        f.setSize(600, 600);
        m = new JMenuBar();
        File = new JMenu("File");
        Settings = new JMenu("Settings");
        Timer = new JMenu("Timer");
        Programs = new JMenu("Programs");
        Store = new JMenu("Store");
        Run = new JMenu("Run");
        Help = new JMenu("Help");
        m1 = new JMenuItem("Exit");
        m2 = new JMenuItem("Temperature");
        m3 = new JMenuItem("Clock");
        m4 = new JMenuItem("Profile");
        m5 = new JMenuItem(" Weekly Timer");
        m6 = new JMenuItem("Preset Programs");
        m7 = new JMenuItem("Errors");
        m8 = new JMenuItem("Profile");
        m9 = new JMenuItem("Auto Run");
        m10 = new JMenuItem("Profile Run");
        m11 = new JMenuItem("Manual Run");
        m12 = new JMenuItem("About");


        Icon SettingsIc = UIManager.getIcon("OptionPane.questionIcon");
                //new ImageIcon("settings.png");
        ActionListener al = new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                new SettingsWindow(f);
            }
        };
        jb1 = new JButton(SettingsIc);
        jb1.addActionListener(al);
        jb1.setFocusPainted(false);
        //jb1.setBorderPainted(false); 
        jb1.setContentAreaFilled(false);

        ImageIcon TimerIc = new ImageIcon("clock.png");
        jb2 = new JButton(TimerIc);
        jb2.setFocusPainted(false);
        //jb2.setBorderPainted(false); 
        jb2.setContentAreaFilled(false);

        ImageIcon ProgramsIc = new ImageIcon("programs.png");
        jb3 = new JButton(ProgramsIc);
        jb3.setFocusPainted(false);
        //jb3.setBorderPainted(false); 
        jb3.setContentAreaFilled(false);

        ImageIcon StoreIc = new ImageIcon("store.png");
        jb4 = new JButton(StoreIc);
        jb4.setFocusPainted(false);
        //jb4.setBorderPainted(false); 
        jb4.setContentAreaFilled(false);

        ImageIcon RunIc = new ImageIcon("run.png");
        jb5 = new JButton(RunIc);
        jb5.setFocusPainted(false);
        //jb5.setBorderPainted(false); 
        jb5.setContentAreaFilled(false);

        ImageIcon ConnectIc = new ImageIcon("connect.png");
        jb6 = new JButton(ConnectIc);
        jb6.setFocusPainted(false);
        //jb6.setBorderPainted(false); 
        jb6.setContentAreaFilled(false);

        File.add(m1);
        Settings.add(m2);
        Settings.add(m3);
        Settings.add(m4);
        Timer.add(m5);
        Programs.add(m6);
        Store.add(m7);
        Store.add(m8);
        Run.add(m9);
        Run.add(m10);
        Run.add(m11);
        Help.add(m12);
        m.add(File);
        m.add(Settings);
        m.add(Timer);
        m.add(Programs);
        m.add(Store);
        m.add(Run);
        m.add(Help);
        f.add(m);
        f.add(jb1);
        f.add(jb2);
        f.add(jb3);
        f.add(jb4);
        f.add(jb5);
        f.add(jb6);

        jb1.setToolTipText("Settings");
        jb2.setToolTipText("Weekly Timer");
        jb3.setToolTipText("Program List");
        jb4.setToolTipText("Store");
        jb5.setToolTipText("Run");
        jb6.setToolTipText("Connect");

        System.out.println(System.getProperty("user.dir"));

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setJMenuBar(m);
        f.setLayout(new FlowLayout());
        f.setVisible(true);

    }

    public static void main(String[] arg) {
        MenuWindow w = new MenuWindow();

    }
}

class SettingsWindow {

    JFrame f;
    JButton b1, b2, b3;

    public SettingsWindow(Container c) {
        JPanel f = new JPanel(new FlowLayout());
        //f.setSize(400, 400);

        Icon TempuratureIc = UIManager.getIcon("OptionPane.warningIcon");
        b1 = new JButton(TempuratureIc);
        b1.setFocusPainted(false);
        //b1.setBorderPainted(false);
        b1.setContentAreaFilled(false);

        Icon TimeIc = UIManager.getIcon("OptionPane.errorIcon");
        b2 = new JButton(TimeIc);
        b2.setFocusPainted(false);
        //b2.setBorderPainted(false);
        b2.setContentAreaFilled(false);

        Icon ProfileIc = UIManager.getIcon("OptionPane.informationIcon");
        b3 = new JButton(ProfileIc);
        b3.setFocusPainted(false);
        //b3.setBorderPainted(false);
        b3.setContentAreaFilled(false);

        f.add(b1);
        f.add(b2);
        f.add(b3);

        b1.setToolTipText("Temperature Settings");
        b2.setToolTipText("Time Settings");
        b3.setToolTipText("Profile Settings");

        System.out.println(System.getProperty("user.dir"));

        //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //f.setLayout();
        //f.setVisible(true);

        JOptionPane.showMessageDialog(c, f, "Settings", JOptionPane.QUESTION_MESSAGE);
    }
}