guidemo.java

来自「有关java的源程序,为讲授java程序设计课程使用」· Java 代码 · 共 439 行

JAVA
439
字号
import java.io.*;
import java.awt.*;

public class GUIDemo{
	public static void main (String args[]){
		GUIWin win = new GUIWin("GUI DEMO");
		win.resize(500,200);
		win.show();
	}
}

class GUIWin extends Frame {
	String		currentFile = null;
	MenuBar		mb;
	Menu		file, demo, help;
	MenuItem	open, save, saveAs, close, exit;
	MenuItem	choice, list, scrollbar;
	MenuItem	about;
	infoDialog	aboutDialog;
	Panel		centerPanel;
	TextArea	fileArea;
	Panel		demoPanel, buttonPanel;
	Panel		demoArea;
	CardLayout	card;
	choiceDemo	choiceP;
	listDemo	listP;
	scrollbarDemo scrollbarP;
	Button		home, prev, next;
	Label		description;

	GUIWin (String title){
		super(title);
		mb = new MenuBar();
		setMenuBar(mb);

// Menu File
		file = new Menu("File");
		open = new MenuItem("Open ...");
		save = new MenuItem("Save");
		save.disable();
		saveAs = new MenuItem("Save as ...");
		close = new MenuItem("Close");
		close.disable();
		exit = new MenuItem("Exit");
		file.add(open);
		file.add(save);
		file.add(saveAs);
		file.add(close);
		file.add(exit);
		mb.add(file);

// Menu "demo"
		demo = new Menu ("demo");
		choice = new MenuItem("choice");
		list = new MenuItem("list");
		scrollbar = new MenuItem("scrollbar");
		demo.add(choice);
		demo.add(list);
		demo.add(scrollbar);
		mb.add(demo);

// Menu "Help"
		help = new Menu("help");
		about= new MenuItem("about");
		help.add(about);
		mb.add(help);
		mb.setHelpMenu(help);

// center panel in GUI window
		centerPanel = new Panel();
		centerPanel.setLayout(new GridLayout(1,2));
		add("Center", centerPanel);

// file area and demo panel in center panel
		fileArea = new TextArea();
		centerPanel.add(fileArea);
		demoPanel = new Panel();
		demoPanel.setLayout(new BorderLayout());
		centerPanel.add(demoPanel);

// demo area and button panel in demo panel
		demoArea = new Panel();
		card = new CardLayout();
		demoArea.setLayout(card);
		demoPanel.add("Center", demoArea);
		buttonPanel = new Panel();
		demoPanel.add("south", buttonPanel);

// demo in demo area
		choiceP = new choiceDemo(this);
		listP = new listDemo(this);
		scrollbarP = new scrollbarDemo(this);
		demoArea.add("choice", choiceP);
		demoArea.add("list", listP);
		demoArea.add("scrollbar",scrollbarP);

// buttons in button panel
		home = new Button ("home");
		prev = new Button ("prev");
		next = new Button ("next");
		buttonPanel.add(home);
		buttonPanel.add(prev);
		buttonPanel.add(next);

// label in main frame
		description = new Label ("This window is designed for GUI demo.", Label.LEFT);
		add("South", description);
	}

	public boolean handleEvent(Event evt){
		if (currentFile == null){
			save.disable();
			close.disable();
		} else {
			save.enable();
			close.enable();
		}

		if (evt.id == Event.WINDOW_DESTROY){
			System.exit(0);
		}

		return super.handleEvent(evt);
	}

	public boolean action(Event evt, Object arg){
		if (evt.target == open){
			FileDialog openFD = new FileDialog(this, "open file");
			openFD.show();
			currentFile = openFD.getDirectory() + openFD.getFile();
			openFile(currentFile);
			Descript("open"+currentFile);
			return true;
		} else if (evt.target == saveAs) {
			FileDialog saveFD = new FileDialog (this, "save file as", 1);
			saveFD.show();
			currentFile = saveFD.getDirectory()+saveFD.getFile();
			saveFile(currentFile);
			Descript("save file as" + currentFile);
			return true;
		} else if (evt.target == save){
			saveFile(currentFile);
			Descript ("save and close"+currentFile);
			currentFile = null;
			fileArea.setText("");
			return true;
		} else if (evt.target == exit){
			System.exit(0);
		} else if (evt.target == about){
			String info = "this program is desinged by Yang Zheng.\n" +
						  "Please send 10 $ to yyang@cs.tu-berlin.de\n";
			aboutDialog = new infoDialog(this, info);
			aboutDialog.pack();
			aboutDialog.show();
			return true;
		} else if (evt.target == home){
			card.first(demoArea);
			Descript("first demo");
			return true;
		} else if (evt.target == prev){
			card.previous(demoArea);
			Descript ("previous demo");
			return true;
		} else if (evt.target == next){
			card.next(demoArea);
			Descript("next demo");
			return true;
		} else if (evt.target instanceof MenuItem){
			card.show(demoArea, (String)arg);
			Descript(arg+"demo");
			return true;
		}
		return false;
	}

	void openFile(String name){
		fileArea.setText("");
		try{
			FileInputStream fis = new FileInputStream (name);
			byte b[] = new byte[80];
			int len;
			while ((len = fis.read(b)) != -1){
				String text = new String (b, 0, 0 , len);
				fileArea.appendText(text);
			}
		} catch (IOException e){
			fileArea.appendText(e.toString());
		}
	}

	void saveFile (String name){
		try {
			String text = fileArea.getText();
			int len = text.length();
			byte b[] = new byte[len];
			text.getBytes(0, len-1, b, 0);
			FileOutputStream fos = new FileOutputStream(name);
			fos.write(b);
		} catch (IOException e){
		}
	}
	void Descript (String content){
		description.setText(content);
	}
}


class infoDialog extends Dialog{
	Frame parent;
	String info;
	infoDialog (Frame parent, String info){
		super(parent, "info dialog", true);
		this.parent = parent;
		this.info = info;
		TextArea infoTA = new TextArea(info);
		add("center", infoTA);
		add("South", new Button("OK"));
	}

	public boolean action (Event evt, Object arg){
		if (evt.target instanceof Button){
			dispose();
			return true;
		}
		return false;
	}
}

class choiceDemo extends Panel{
	GUIWin parent;
	Choice choice;
	Label label;

	choiceDemo (GUIWin parent){
		this.parent = parent;
		choice = new Choice();
		choice.addItem("ichi");
		choice.addItem("ni");
		choice.addItem("san");
		choice.addItem("you");
		label = new Label();
		setLabelText(choice.getSelectedIndex(), choice.getSelectedItem());
		add(choice);
		add(label);
	}

	void setLabelText(int num, String text){
		label.setText("Item #"+ num + " selected." + 
					  "Text = \"" + text+"\".");
	}

	public boolean action (Event e, Object arg){
		parent.Descript("action event in demo");
		if(e.target instanceof Choice){
			setLabelText(choice.getSelectedIndex(), (String)arg);
			return true;
		}
		return true;
	}

	public Insets insets(){
		return new Insets(10, 10, 10, 10 );
	}
}


class listDemo extends Panel {
	GUIWin parent;
	TextArea output;
	List spanish, italian;

	listDemo(GUIWin parent){
		this.parent = parent;
		spanish = new List(4, true);
		spanish.addItem("uno");
		spanish.addItem("dos");
		spanish.addItem("tres");
		spanish.addItem("cuatro");	
		spanish.addItem("cinco");

		italian = new List();
		italian.addItem("uno");
		italian.addItem("due");
		italian.addItem("tre");
		italian.addItem("quattro");	
		italian.addItem("sette");

		GridBagLayout gridBag = new GridBagLayout();
		setLayout(gridBag);
		output = new TextArea(10, 40);
		output.setEditable(false);
		GridBagConstraints tc = new GridBagConstraints();
		tc.fill = GridBagConstraints.BOTH;
		tc.weightx = 1.0;
		tc.weighty = 1.0;
		tc.gridheight = 2;
		gridBag.setConstraints(output, tc);
		add(output);

		GridBagConstraints lc = new GridBagConstraints();
		lc.fill = GridBagConstraints.VERTICAL;
		lc.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(spanish, lc);
		add(spanish);
		gridBag.setConstraints(italian, lc);
		add(italian);
	}

	public boolean action (Event e, Object arg){
		parent.Descript("action event in demo");
		if(e.target instanceof List){
			String language = (e.target == spanish)?
				"Spanish": "Italian";
			output.appendText("Action event occurred on \""+
				(String)arg + "\" in "+
				language + ".\n");

			// output.appendText("Action event occureed on \ "" + (String)arg+"\" in "+ language+ ".\n");
		}
		return true;
	}

	public boolean handleEvent (Event e){
		if (e.target instanceof List){
			parent.Descript("list event in demo");
			List list = (List)(e.target);
			String language = (list == spanish)?
				"Spanish": "Italian";

			switch (e.id){
				case Event.LIST_SELECT:
					int sIndex= ((Integer)e.arg).intValue();
					output.appendText("Select event occurred on item #" +
								sIndex +"(\"" +
								list.getItem(sIndex)+"\") in"+
								language + ".\n");
					break;
				case Event.LIST_DESELECT:
					int dIndex= ((Integer)e.arg).intValue();
					output.appendText("Deselect event occurred on item #" +
								dIndex +"(\"" +
								list.getItem(dIndex)+"\") in"+
								language + ".\n");
				}
		}
		return super.handleEvent(e);
	}

	public Insets insets(){
		return new Insets(10, 10, 10, 10);
	}
}

class scrollbarDemo extends Panel{
	GUIWin parent;
	Scrollbar ranger;
	TextField value;
	Label maxV, minV;
	scrollbarDemo(GUIWin parent){
		this.parent = parent;
		GridBagLayout gridBag = new GridBagLayout();
		setLayout(gridBag);
		GridBagConstraints c = new GridBagConstraints();
		ranger = new Scrollbar(Scrollbar.HORIZONTAL);
		ranger.setValues(0, 64, 0, 255);

		minV = new Label("min = "+ranger.getMinimum());
		c.anchor = GridBagConstraints.EAST;
		gridBag.setConstraints(minV, c);
		add(minV);

		value = new TextField(5);
		value.setText(String.valueOf(ranger.getValue()));
		c.anchor = GridBagConstraints.CENTER;
		gridBag.setConstraints(value, c);
		add(value);

		maxV = new Label("max = "+ranger.getMaximum());
		c.anchor = GridBagConstraints.WEST;
		c.gridwidth = GridBagConstraints.REMAINDER;
		gridBag.setConstraints(maxV, c);
		add(maxV);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridheight= GridBagConstraints.REMAINDER;
		c.insets = new Insets( 5,0,0,0);
		gridBag.setConstraints(ranger, c);
		add(ranger);
	}

	public boolean handleEvent(Event evt){
		switch (evt.id){
		case Event.SCROLL_LINE_UP:
		case Event.SCROLL_LINE_DOWN:
		case Event.SCROLL_PAGE_UP:
		case Event.SCROLL_PAGE_DOWN:
		case Event.SCROLL_ABSOLUTE:
				parent.Descript("scrollbar event");
				value.setText(String.valueOf(((Integer)evt.arg).intValue()));
		}
		return super.handleEvent(evt);
	}

	public boolean action (Event evt, Object arg){
		parent.Descript("action event");
		if(evt.target instanceof TextField){
			int v = getValue();
			ranger.setValue(v);
			return true;
		}
		return false;
	}

	int getValue(){
		int i = 0;
		try{
			i = Double.valueOf(value.getText()).intValue();
		} catch (java.lang.NumberFormatException e){
		} finally {
			int max = ranger.getMaximum();
			int min = ranger.getMinimum();
			if (i>max) i = max;
			if (i< min) i = min;
			value.setText(String.valueOf(i));
		}
		return i;
	}

	public Insets insets(){
		return new Insets(10,10,10,10);
	}
}






⌨️ 快捷键说明

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