如何使用JAVA跨多个窗口(不同的jframe)处理事件?

时间:2015-09-27 14:05:48

标签: java swing events

我正在创建一个包含工作区(jframe)和工具箱(另一个jframe)的ide。如何完成跨这两个jframe处理事件的任务?例如,如果我单击工具箱中的工具,则必须在工作区中执行操作。

请帮帮我

工具箱代码:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class ToolboxForPDP extends JFrame {

/**
 *
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ToolboxForPDP frame = new ToolboxForPDP();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public ToolboxForPDP() {
    setResizable(false);
    try {
        UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

    setTitle("Toolbox");
    setType(Type.UTILITY);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 157, 445);
    contentPane = new JPanel();
    contentPane.setBackground(new Color(245, 245, 220));
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnNewButton = new JButton("");
    btnNewButton.setToolTipText("Select an element in the work area");
    btnNewButton.setBackground(new Color(255, 255, 255));
    btnNewButton.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\select.jpg"));
    btnNewButton.setBounds(10, 11, 55, 45);
    contentPane.add(btnNewButton);

    JButton btnNewButton_1 = new JButton("");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnNewButton_1.setToolTipText("Insert Image");
    btnNewButton_1.setBackground(new Color(255, 255, 255));
    btnNewButton_1.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\image.png"));
    btnNewButton_1.setBounds(75, 11, 55, 45);
    contentPane.add(btnNewButton_1);

    JButton btnNewButton_2 = new JButton("");
    btnNewButton_2.setToolTipText("Insert Text");
    btnNewButton_2.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\text.jpg"));
    btnNewButton_2.setBackground(new Color(255, 255, 255));
    btnNewButton_2.setBounds(10, 67, 55, 45);
    contentPane.add(btnNewButton_2);

    JButton btnNewButton_3 = new JButton("");
    btnNewButton_3.setToolTipText("Insert Hyperlink");
    btnNewButton_3.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\hyperlink.png"));
    btnNewButton_3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnNewButton_3.setBackground(new Color(255, 255, 255));
    btnNewButton_3.setBounds(75, 67, 55, 45);
    contentPane.add(btnNewButton_3);

    JButton btnNewButton_4 = new JButton("");
    btnNewButton_4.setToolTipText("Change Page Background Properties");
    btnNewButton_4.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\fill color.png"));
    btnNewButton_4.setBackground(new Color(255, 255, 255));
    btnNewButton_4.setBounds(10, 123, 55, 45);
    contentPane.add(btnNewButton_4);

    JButton btnNewButton_5 = new JButton("");
    btnNewButton_5.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\textbox.jpg"));
    btnNewButton_5.setToolTipText("Insert Textbox");
    btnNewButton_5.setBackground(new Color(255, 255, 255));
    btnNewButton_5.setBounds(10, 179, 55, 45);
    contentPane.add(btnNewButton_5);

    JButton btnNewButton_6 = new JButton("");
    btnNewButton_6.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\radio Button.gif"));
    btnNewButton_6.setToolTipText("Insert Radio Button");
    btnNewButton_6.setBackground(new Color(255, 255, 255));
    btnNewButton_6.setBounds(10, 235, 55, 45);
    contentPane.add(btnNewButton_6);

    JButton btnNewButton_7 = new JButton("");
    btnNewButton_7.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\checkbox.gif"));
    btnNewButton_7.setToolTipText("Insert Checkbox");
    btnNewButton_7.setBackground(new Color(255, 255, 255));
    btnNewButton_7.setBounds(10, 291, 55, 45);
    contentPane.add(btnNewButton_7);

    JButton btnNewButton_8 = new JButton("");
    btnNewButton_8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    btnNewButton_8.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\hr.jpg"));
    btnNewButton_8.setToolTipText("Insert Horizontal Rule");
    btnNewButton_8.setBackground(new Color(255, 255, 255));
    btnNewButton_8.setBounds(75, 123, 55, 45);
    contentPane.add(btnNewButton_8);

    JButton btnNewButton_9 = new JButton("");
    btnNewButton_9.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\button.jpg"));
    btnNewButton_9.setToolTipText("Insert Button");
    btnNewButton_9.setBackground(new Color(255, 255, 255));
    btnNewButton_9.setBounds(75, 179, 55, 45);
    contentPane.add(btnNewButton_9);

    JButton btnNewButton_10 = new JButton("");
    btnNewButton_10.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\drop-down list.png"));
    btnNewButton_10.setToolTipText("Insert Drop-Down List");
    btnNewButton_10.setBackground(new Color(255, 255, 255));
    btnNewButton_10.setBounds(75, 235, 55, 45);
    contentPane.add(btnNewButton_10);

    JButton btnNewButton_11 = new JButton("");
    btnNewButton_11.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\list.jpg"));
    btnNewButton_11.setToolTipText("Insert List");
    btnNewButton_11.setBackground(new Color(255, 255, 255));
    btnNewButton_11.setBounds(75, 291, 55, 45);
    contentPane.add(btnNewButton_11);

    JButton btnNewButton_12 = new JButton("");
    btnNewButton_12.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    btnNewButton_12.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\toolbox icons\\icoScript.png"));
    btnNewButton_12.setToolTipText("Add Script");
    btnNewButton_12.setBackground(new Color(255, 255, 255));
    btnNewButton_12.setBounds(42, 347, 55, 45);
    contentPane.add(btnNewButton_12);
}
}

工作代码:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.ImageIcon;
import java.awt.Toolkit;


public class StartScreen extends JFrame {

/**
 *
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                StartScreen frame = new StartScreen();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public StartScreen() {
    try {
        UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

    setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\logos\\swami_vivekananda2.png"));
    setTitle("PageDesigner PRO(TM)");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(10, 10, 1350, 700);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenu mnNew = new JMenu("New");
    mnNew.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\menu icons\\Folder-New-icon.png"));
    mnFile.add(mnNew);

    JMenuItem mntmNewProject = new JMenuItem("New Project");
    mnNew.add(mntmNewProject);

    JMenuItem mntmNewPage = new JMenuItem("New Page");
    mnNew.add(mntmNewPage);
    mnFile.addSeparator();

    JMenuItem mntmSave = new JMenuItem("Save");
    mntmSave.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\menu icons\\save.png"));
    mnFile.add(mntmSave);

    JMenuItem mntmSaveAs = new JMenuItem("Save As...");
    mnFile.add(mntmSaveAs);
    mnFile.addSeparator();

    JMenuItem mntmAddToProject = new JMenuItem("Add to project");
    mnFile.add(mntmAddToProject);

    JMenuItem mntmTestThisPage = new JMenuItem("Test this page");
    mnFile.add(mntmTestThisPage);
    mnFile.addSeparator();

    JCheckBoxMenuItem chckbxmntmShowWelcomeScreen = new JCheckBoxMenuItem("Show Welcome screen at startup");
    mnFile.add(chckbxmntmShowWelcomeScreen);
    mnFile.addSeparator();

    JMenuItem mntmExit = new JMenuItem("Exit");
    mntmExit.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\menu icons\\exit.png"));
    mnFile.add(mntmExit);

    JMenu mnEdit = new JMenu("Edit");
    menuBar.add(mnEdit);

    JMenuItem mntmModifyElementProperties = new JMenuItem("Modify Element Properties");
    mnEdit.add(mntmModifyElementProperties);

    JMenu mnMode = new JMenu("Mode");
    menuBar.add(mnMode);

    JRadioButtonMenuItem rdbtnmntmBeginnerMode = new JRadioButtonMenuItem("Beginner Mode");
    mnMode.add(rdbtnmntmBeginnerMode);

    JRadioButtonMenuItem rdbtnmntmAdvancedMode = new JRadioButtonMenuItem("Advanced Mode");
    mnMode.add(rdbtnmntmAdvancedMode);

    ButtonGroup modeMenuGroup = new ButtonGroup();
    modeMenuGroup.add(rdbtnmntmBeginnerMode);
    modeMenuGroup.add(rdbtnmntmAdvancedMode);

    JMenu mnHelp = new JMenu("Help");
    menuBar.add(mnHelp);

    JMenuItem mntmUserGuide = new JMenuItem("User Guide");
    mntmUserGuide.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\menu icons\\manual icon.gif"));
    mnHelp.add(mntmUserGuide);

    JMenuItem mntmAbout = new JMenuItem("About...");
    mntmAbout.setIcon(new ImageIcon("D:\\KS\\4-1\\Mini-Project\\PageDesigner PRO(TM)\\PageDesigner PRO(TM)\\resources\\pics\\icons\\menu icons\\info_black.png"));
    mnHelp.add(mntmAbout);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
}
}

1 个答案:

答案 0 :(得分:3)

您的问题是如何将信息从一个JFrame传递到另一个JFrame,这可以像让一个类调用另一个类的方法一样简单地完成。你还没有完成这个,并且你只发布了一个骨架程序,一个带有组件但没有逻辑的程序告诉我你仍然是一个初学Java程序员,所以我的主要建议是首先,您努力学习编码,特别是了解面向对象的原则以及它们与Java的关系。如果没有这些基础知识,我们可以为您提供代码和指示,但它不会对您有所帮助。我建议您转到Java Tutorials并从那里开始,但也可以在Bruce Eckel's Thinking in Java和/或Head First Java等主题上获得一两本不错的书。

至于你的实际代码,我建议你不要创建扩展JFrame的类,因为它会把你锁定到一个JFrame,再次按照上面的评论,你的工具窗口应该是非模态的JDialog而不是JFrame。如果您将代码设置为创建JPanels,那么您可以将它们放置在JFrame,JDialogs,其他JPanel等中......如果需要,这样可以提供更多的灵活性。

程序情况的主要困难不是将信息从一个窗口传递到另一个窗口,而是将一个对象传递给另一个窗口,实际上是 这样做,因为程序是事件驱动的。我自己,我喜欢使用PropertyChangeListeners,基本上使用已经是Swing GUI结构一部分的观察者接口。例如,在下面的代码中,我创建了两个主要的JPanel,一个显示在JFrame中,另一个显示在非模态JDialog中,我将按钮按下信息(按钮的actionCommand String)传递给主GUI中的JTextArea一个PropertyChangeListener:

import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

public class Foo3 {
    private static void createAndShowGui() {
        final MainPanel1 mainPanel1 = new MainPanel1();
        final ToolPanel1 toolPanel1 = new ToolPanel1();

        JFrame frame = new JFrame("Foo3");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel1);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        JDialog dialog = new JDialog(frame, "Toolbar", ModalityType.MODELESS);
        dialog.add(toolPanel1);
        dialog.pack();
        dialog.setLocationByPlatform(true);
        dialog.setVisible(true);

        toolPanel1.addPropertyChangeListener(ToolPanel1.ACTION_COMMAND, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                mainPanel1.appendActionCommand((String) evt.getNewValue());
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

class MainPanel1 extends JPanel {
    private JTextArea actionCommandArea = new JTextArea(30, 50);
    private JScrollPane scrollPane = new JScrollPane(actionCommandArea);

    public MainPanel1() {
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
    }

    public void appendActionCommand(String text) {
        actionCommandArea.append(text + "\n");
    }
}

class ToolPanel1 extends JPanel {
    public static final String ACTION_COMMAND = "action command";
    public static final String[] BTN_TEXTS  = {
        "Select Element",
        "Insert Image",
        "Insert Text",
        "Insert Hyperlink",
        "Change Page Background",
        "Insert Textbox",
        "Insert Radio Button", 
        "Insert Checkbox",
        "Insert Horizontal Rule",
        "Insert Button",
        "Insert Drop-Down List",
        "Insert List",
        "Add Script"
    };
    private String actionCommand = "";

    public ToolPanel1() {
        int rows = 0; // variable number of rows
        int cols = 2; // 2 columns
        int hgap = 5;
        int vgap = hgap;
        setLayout(new GridLayout(rows, cols, hgap, vgap));
        setBorder(BorderFactory.createEmptyBorder(hgap, hgap, hgap, hgap));

        for (String btnText : BTN_TEXTS) {
            add(new JButton(new ButtonAction(btnText)));
        }
    }

    public String getActionCommand() {
        return actionCommand;
    }

    private class ButtonAction extends AbstractAction {
        public ButtonAction(String name) {
            super(name);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String oldValue = "";
            String newValue = e.getActionCommand();
            actionCommand = newValue;
            ToolPanel1.this.firePropertyChange(ACTION_COMMAND, oldValue, newValue);
        }
    }
}

更强大的设计将是使用模型 - 视图 - 控制器类型设计,但这有点更高级,在使用之前,您需要获得更多的代码体验,我认为。还可以查看these links to similar questions/answers