⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainwindow.java

📁 PIY(Program It Yourself)是一个基于Java的应用程序开发环境
💻 JAVA
字号:
package piy;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.File;

/**
* The main window in PIY-II.  This contains the menu and the toolbar for modifying
* user GUI's/ActionLists.  If this window is closed, PIY-II shuts down.
* @author David Vivash
* @version 1.0.1, 25/11/00
*/
public class MainWindow extends JFrame implements ChangeListener, Observer, WindowListener
{
	private String mainString;
	private ScrollingButtonPanel userComponents, userContainers;

	/**
	* Constructs the main window, given a title
	* @param mainString the main title of the window - this will be appended with 
	* the current open project name
	* @param componentDescriptors the descriptor classes for all of the available components
	* @param containerDescriptors the descriptor classes for all of the available containers
	*/
	public MainWindow(String mainString, Descriptor[] componentDescriptors, Descriptor[] containerDescriptors) {
		super(mainString);

		this.mainString = mainString;

		JMenu file		= new JMenu("File");
		JMenu project	= new JMenu("Project");
		JMenu view		= new JMenu("View");
		JMenu help		= new JMenu("Help");

		//----------------- File Menu -------------------
		JMenuItem menuNewProject = new JMenuItem("New Project");
		JMenuItem menuOpenProject = new JMenuItem("Open Project");
		JMenuItem menuSaveProject = new JMenuItem("Save Project");
		JMenuItem menuSaveProjectAs = new JMenuItem("Save Project As...");
		JMenuItem menuExit = new JMenuItem("Exit");
		
		menuNewProject.setActionCommand(PIY.NEW_PROJECT);
		menuOpenProject.setActionCommand(PIY.OPEN_PROJECT);
		menuSaveProject.setActionCommand(PIY.SAVE_PROJECT);
		menuSaveProjectAs.setActionCommand(PIY.SAVE_PROJECT_AS);
		menuExit.setActionCommand(PIY.EXIT);

		menuNewProject.addActionListener(PIY.getInstance());
		menuOpenProject.addActionListener(PIY.getInstance());
		menuSaveProject.addActionListener(PIY.getInstance());
		menuSaveProjectAs.addActionListener(PIY.getInstance());
		menuExit.addActionListener(PIY.getInstance());

		file.add(menuNewProject); 
		file.add(menuOpenProject); 
		file.add(menuSaveProject); 
		file.add(menuSaveProjectAs); 
		file.addSeparator();
		file.add(menuExit);

		//----------------- Project Menu -------------------
		JMenuItem menuRunProject = new JMenuItem("Run Project");
		JMenuItem menuStopProject = new JMenuItem("Stop Running Project");
		JMenuItem menuCompileProject = new JMenuItem("Compile Project");

		menuRunProject.setActionCommand(PIY.RUN_PROJECT);
		menuStopProject.setActionCommand(PIY.STOP_PROJECT);
		menuCompileProject.setActionCommand(PIY.COMPILE_PROJECT);

		menuRunProject.addActionListener(PIY.getInstance());
		menuStopProject.addActionListener(PIY.getInstance());
		menuCompileProject.addActionListener(PIY.getInstance());

		project.add(menuRunProject);
		project.add(menuStopProject);
		project.addSeparator();
		project.add(menuCompileProject);

		//----------------- View Menu -------------------
		JMenuItem menuViewPropertiesEditor = new JMenuItem("Properties Editor");
		JMenuItem menuViewMemoryEditor = new JMenuItem("Memory Editor");
		JMenuItem menuViewActionListEditor = new JMenuItem("Action List Editor");

		menuViewPropertiesEditor.setActionCommand(PIY.VIEW_PROPERTIES);
		menuViewMemoryEditor.setActionCommand(PIY.VIEW_MEMORY_EDITOR);
		menuViewActionListEditor.setActionCommand(PIY.VIEW_LIST_EDITOR);

		menuViewPropertiesEditor.addActionListener(PIY.getInstance());
		menuViewMemoryEditor .addActionListener(PIY.getInstance());
		menuViewActionListEditor.addActionListener(PIY.getInstance());

		view.add(menuViewPropertiesEditor);
		view.add(menuViewMemoryEditor);
		view.add(menuViewActionListEditor);

		//----------------- Help Menu -------------------
		JMenuItem menuAbout = new JMenuItem("About PIY");
		menuAbout.setActionCommand(PIY.ABOUT);
		menuAbout.addActionListener(PIY.getInstance());
		help.add(menuAbout);

		//-------------- Create Menu Bar ----------------
		JMenuBar menu = new JMenuBar();
		menu.add(file);
		menu.add(project);
		menu.add(view);
		menu.add(help);

		setSize(640, 118);
		setJMenuBar(menu);

		//---- Set up container panels ----
		JPanel panel1 = new JPanel(null); //use absolute positioning
		JPanel panel2 = new JPanel(null); //use absolute positioning

		//Border which paints a bevelled line on the right hand side of the component
		Border border = new AbstractBorder() {
			public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
				g.setColor(c.getBackground().darker());
				g.drawLine(width-2, 0, width-2, height);
				g.setColor(c.getBackground().brighter());
				g.drawLine(width-1, 0, width-1, height);
			}
		};

		panel1.setBorder(border);
		panel1.setPreferredSize(new Dimension(100, 64));
		panel1.setMinimumSize(new Dimension(100, 64));
		panel2.setMinimumSize(new Dimension(64, 64));
		panel2.setPreferredSize(new Dimension(64, 64));

		String sep = File.separator;
		String images = "piy" + sep + "images" + sep;

		FloatingButton newProject = new FloatingButton(new ImageIcon(images + "new.gif").getImage(), PIY.NEW_PROJECT, false);
		newProject.addActionListener(PIY.getInstance());
		newProject.setBounds(10, 8, 22, 22);
		newProject.setToolTipText("New Project");

		FloatingButton openProject = new FloatingButton(new ImageIcon(images + "open.gif").getImage(), PIY.OPEN_PROJECT, false);
		openProject.addActionListener(PIY.getInstance());
		openProject.setBounds(35, 8, 22, 22);
		openProject.setToolTipText("Open Project");

		FloatingButton saveProject = new FloatingButton(new ImageIcon(images + "save.gif").getImage(), PIY.SAVE_PROJECT, false);
		saveProject.addActionListener(PIY.getInstance());
		saveProject.setBounds(60, 8, 22, 22);
		saveProject.setToolTipText("Save Project");

		FloatingButton newWindow = new FloatingButton(new ImageIcon(images + "newwin.gif").getImage(), PIY.NEW_WINDOW, false);
		newWindow.addActionListener(PIY.getInstance());
		newWindow.setBounds(10, 33, 22, 22);
		newWindow.setToolTipText("New Window");

		FloatingButton newActionList = new FloatingButton(new ImageIcon(images + "actionlist.gif").getImage(), PIY.VIEW_LIST_EDITOR, false);
		newActionList.addActionListener(PIY.getInstance());
		newActionList.setBounds(35, 33, 22, 22);
		newActionList.setToolTipText("View Action List Editor");

		FloatingButton projectManager = new FloatingButton(new ImageIcon(images + "manager.gif").getImage(), PIY.VIEW_MEMORY_EDITOR, false);
		projectManager.addActionListener(PIY.getInstance());
		projectManager.setBounds(60, 33, 22, 22);
		projectManager.setToolTipText("View Memory Editor");

		panel1.add(newProject);
		panel1.add(openProject);
		panel1.add(saveProject);
		panel1.add(newWindow);
		panel1.add(newActionList);
		panel1.add(projectManager);

		FloatingButton runProject = new FloatingButton(new ImageIcon(images + "run.gif").getImage(), PIY.RUN_PROJECT, false);
		runProject.addActionListener(PIY.getInstance());
		runProject.setBounds(5, 20, 22, 22);
		runProject.setToolTipText("Run Project");

		FloatingButton stopProject = new FloatingButton(new ImageIcon(images + "stop.gif").getImage(), PIY.STOP_PROJECT, false);
		stopProject.addActionListener(PIY.getInstance());
		stopProject.setBounds(30, 20, 22, 22);
		stopProject.setToolTipText("Stop Running Project");

		panel2.add(runProject);
		panel2.add(stopProject);


		Container contentPane = getContentPane();
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		contentPane.setLayout(gridbag);
		c.fill = GridBagConstraints.BOTH;   

		c.gridx = 0;
		c.gridy = 0;
		gridbag.setConstraints(panel1, c);
		contentPane.add(panel1);

		c.gridx = 1;
		c.gridy = 0;
		gridbag.setConstraints(panel2, c);
		contentPane.add(panel2);


		//------ Create tab pane for actions and components -----
		userComponents = new ScrollingButtonPanel(componentDescriptors.length, 26, 26, 4);
		userContainers = new ScrollingButtonPanel(containerDescriptors.length, 26, 26, 4);

		FloatingButton toolButton;

		for (int i=0; i<componentDescriptors.length; i++) {
			userComponents.addButton(toolButton = new FloatingButton(componentDescriptors[i].getIcon(), ""+i, true));
			toolButton.setToolTipText(componentDescriptors[i].getName());
		}

		for (int i=0; i<containerDescriptors.length; i++) {
			userContainers.addButton(toolButton = new FloatingButton(containerDescriptors[i].getIcon(), ""+i, true));
			toolButton.setToolTipText(containerDescriptors[i].getName());
		}


		userComponents.addChangeListener(this);
		userContainers.addChangeListener(this);

		JTabbedPane tabs = new JTabbedPane();
		tabs.addTab("Components", userComponents);
		tabs.addTab("Containers", userContainers);

		c.gridx = 2;
		c.gridy = 0;
		c.weightx = 1.0;
		gridbag.setConstraints(tabs, c);
		contentPane.add(tabs);
	
		tabs.setPreferredSize(new Dimension(100,30));
		tabs.setMinimumSize(new Dimension(100,30));
		tabs.setMaximumSize(new Dimension(100,30));

		//Make this class an observer of changes in PIY
		PIY.getInstance().addObserver(this);

		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(this);
	}

	/**
	* Returns false to ensure window can never be made resizable
	* @return false
	*/
	public boolean isResizable() { return false; }

	/**
	* Gets the index value of the currently selected user component, or -1 if no user
	* component is selected.
	* @return a usercomponent index or -1
	*/
	public int getSelectedUserComponent() {
		return userComponents.getSelectedIndex();
	}

	/**
	* Gets the index value of the currently selected container, or -1 if no container is
	* selected.
	* @return a usercontainer index or -1
	*/
	public int getSelectedUserContainer() {
		return userContainers.getSelectedIndex();
	}

	/**
	* Returns true if a component is selected in the user components panel.
	* @return true if a component is currently selected, false otherwise
	*/
	public boolean isComponentSelected() {
		return userComponents.isSelected();
	}

	/**
	* Returns true if a container is selected in the user containers panel.
	* @return true if a container is currently selected, false otherwise
	*/
	public boolean isContainerSelected() {
		return userContainers.isSelected();
	}

	//----------------- ChangeListener method ---------------------
	public void stateChanged(ChangeEvent e) {
		if (e.getSource() == userComponents) {
			if (userComponents.isSelected())
				PIY.getInstance().actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, PIY.COMPONENT_CHOSEN));
			else
				PIY.getInstance().actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, PIY.COMPONENT_UNCHOSEN));
		} else {
			if (userContainers.isSelected())
				PIY.getInstance().actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, PIY.CONTAINER_CHOSEN));
			else
				PIY.getInstance().actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, PIY.CONTAINER_UNCHOSEN));
		}
	}

	//---------------- Observer method ----------------------
	/**
	* This method is called when a change occurs in another PIY referenced window.
	* If the change is something we should ignore (ie. the change was initiated in this window
	* first, we ignore the change.
	* @param o the observable object that has changed (this will be PIY)
	* @param arg the change event that has occurred (will be an ActionEvent)
	*/
	public void update(Observable o, Object arg) {
		//arg will be an action event

		ActionEvent e = (ActionEvent)arg;

		if (e.getSource() != this			&& 
			e.getSource() != userComponents	&& 
			e.getSource() != userContainers) { //another window has changed

			if (e.getActionCommand().equals(PIY.COMPONENT_SELECTED)) {
				userComponents.clearSelection();
				userContainers.clearSelection();
			}
		} else {
			if (e.getActionCommand().equals(PIY.COMPONENT_CHOSEN))
				userContainers.clearSelection();
			else if (e.getActionCommand().equals(PIY.CONTAINER_CHOSEN))
				userComponents.clearSelection();
		}

	}

	//------------------ WindowListener methods ---------------------
	public void windowClosing(WindowEvent e) {
		PIY.getInstance().actionPerformed(new ActionEvent(this, 0, PIY.EXIT)); 
	}
	public void windowActivated(WindowEvent e) { }
	public void windowClosed(WindowEvent e) { }
	public void windowDeactivated(WindowEvent e) { }
	public void windowDeiconified(WindowEvent e) { }
	public void windowIconified(WindowEvent e) { }
	public void windowOpened(WindowEvent e) { }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -