2

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

这里有两个代码:

菜单窗口.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() ;

}

}

设置窗口.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 很陌生。

4

3 回答 3

2

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

更新 - 1:

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

 jb1.addActionListener(new ActionListener() {

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

mainSettingWindow班级中删除。这不是必需的。

更新 - 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像我上面所说的那样写入所有按钮来调用它们。另请阅读如何编写 ActionListener

于 2013-01-20T05:43:08.863 回答
1

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

于 2013-01-20T05:53:52.190 回答
1

请参阅使用多个 JFrame,好/坏做法?

使用模态JDialog或 aJOptionPane代替第二个“子”窗口。

在此处输入图像描述

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);
    }
}
于 2013-01-20T07:24:11.250 回答