applettester.java

来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 581 行

JAVA
581
字号
//import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
public class AppletTester extends Applet implements ActionListener
{
	private Button addButton	= new Button("Add");
	private Button subButton	= new Button("Subtract");
	private Button resetButton	= new Button("Reset");
	private TextField input		= new TextField(10);
	private TextField output	= new TextField(10);
	private boolean firstDigit	= true;
	
	private TextField display	= new TextField("0");
	private double number		= 0.0;
	private String operator		= "=";
	private int	total = 0;

	private void handleReset()
	{
		display.setText("0");
		firstDigit = true;
		operator = "=";
	}
	public void init()
	{
		Panel buttons	=	new Panel();
		buttons.setLayout(new FlowLayout());
		buttons.add(addButton);
		buttons.add(subButton);
		buttons.add(resetButton);
		
		Panel display	=	new Panel();
		display.setLayout(new GridLayout(2, 2));
		display.add(new Label("Add/subtract: "));
		display.add(input);
		display.add(new Label("Result: "));
		display.add(output);
		setLayout(new BorderLayout());
		
		add("Center", display);
		add("South", buttons);
		addButton.addActionListener(this);
		subButton.addActionListener(this);
		resetButton.addActionListener(this);
		//handleReset();
	}
	public void actionPerformed(ActionEvent e)
	{
		Object target = e.getSource();
		String label = e.getActionCommand();
	}
}
*/
/*
public class AppletTester extends Applet implements ActionListener
{
	//--------------------------	Calculator	-----------------------------//
	private final String[] KEYS	=	{"7",	"8",	"9",	"/",	
									 "4",	"5",	"6",	"*",	
									 "1",	"2",	"3",	"-",	
									 ".",	"0",	"=",	"+"};
	private Button keys[]		= new Button[KEYS.length];
	private Button reset		= new Button("CE");
	private TextField display	= new TextField("0");
	private boolean firstDigit	= true;
	private double number		= 0.0;
	private String operator		= "=";
	private void setup()
	{
		Panel calcKeys = new Panel();
		calcKeys.setLayout(new GridLayout(4, 4));
		for (int i = 0; i<KEYS.length; i ++)
		{
			keys[i] = new Button(KEYS[i]);
			calcKeys.add(keys[i]);
		}
		
		Panel top = new Panel();
		top.setLayout(new BorderLayout());
		top.add("Center", display);
		top.add("East", reset);
		setLayout(new BorderLayout());
		add("North", top);
		add("Center", calcKeys);
	}
	public void init()
	{
		setup();
		for (int i = 0; i < KEYS.length; i ++)
			keys[i].addActionListener(this);
		reset.addActionListener(this);
		display.setEditable(false);
	}
	public void actionPerformed(ActionEvent e)
	{
		Object target = e.getSource();
		String label = e.getActionCommand();
		if (target == reset)
			handleReset();
		else if ("0123456789.".indexOf(label) >= 0)
			handleNumber(label);
		else
			handleOperator(label);
	}
	private double getNumberFromDisplay()
	{
		return Double.valueOf(display.getText()).doubleValue();
	}
	private void handleNumber(String key)
	{
		if (firstDigit)
			display.setText(key);
		else if ((key.equals(".")) && (display.getText().indexOf(".") < 0))
			display.setText(display.getText() + key);
		firstDigit = false;
	}
	private void handleOperator(String key)
	{
		if (operator.equals("+"))
			number += getNumberFromDisplay();
		else if (operator.equals("-"))
			number -= getNumberFromDisplay();
		else if (operator.equals("*"))
			number *= getNumberFromDisplay();
		else if (operator.equals("/"))
			number /= getNumberFromDisplay();
		else if (operator.equals("="))
			number = getNumberFromDisplay();
		display.setText(String.valueOf(number));
		operator = key;
		firstDigit = true;
	}
	private void handleReset()
	{
		display.setText("0");
		firstDigit = true;
		operator = "=";
	}
}
*/
/*
public class AppletTester extends Frame implements ActionListener
{
	private Button add	=	new Button(" Add ");
	private Button del	=	new Button(" Delete ");
	private Button up	=	new Button(" +	");
	private Button down	=	new Button(" - ");
	private List list	=	new List();
	private TextField taskInput	=	new TextField();
	private Label priorityLabel	=	new Label("change priority");
	private Label taskLabel	=	new Label("task:	");
	
	private class WindowCloser extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
			System.exit(0);
		}
	}
	public AppletTester()
	{
		super("开始java--Task List");
		setup();
		add.addActionListener(this);
		del.addActionListener(this);
		up.addActionListener(this);
		down.addActionListener(this);
		addWindowListener(new WindowCloser());
		pack();
		show();
		//hide();
		list.addActionListener(this);
	}
	private void setup()
	{
		//增加/删除任务按钮	面板
		Panel buttons	=	new Panel();
		buttons.setLayout(new GridLayout(1, 1));
		buttons.add(add);
		buttons.add(del);
		
		//优先权控制	面板
		Panel priorities	=	new Panel();
		priorities.setLayout(new GridLayout(1, 3));
		priorities.add("West", up);
		priorities.add("Center", priorityLabel);
		priorities.add("East", down);
		//输入	面板
		Panel input	=	new Panel();
		input.setLayout(new BorderLayout());
		input.add("West", taskLabel);
		input.add("Center", taskInput);
		//
		Panel top	=	new Panel();
		top.setLayout(new GridLayout(2, 1));
		top.add(input);
		top.add(priorities);
		//添加组件
		setLayout(new BorderLayout());
		add("Center", list);
		add("South", buttons);
		add("North", top);
		//add("South", input);
	}
	private void handleAdd(String newTask)
	{
		//输入控制
		list.add(newTask);
		list.select(list.getItemCount() - 1);
		taskInput.setText("");
	}
	private void handleIncPriority(int pos)
	{
		//增加优先级
		String item	=	list.getItem(pos);
		list.remove(pos);
		list.add(item, pos - 1);
		list.select(pos - 1);
	}
	private void handleDel(int pos)
	{
		list.remove(pos);
		list.select(pos);
	}
	private void handleDecPriority(int pos)
	{
		if (pos < list.getItemCount() - 1)
		{
			String item	=	list.getItem(pos);
			list.remove(pos);
			list.add(item, pos + 1);
			list.select(pos + 1);
		}
	}
	public void actionPerformed(ActionEvent ae)
	{
		if ((ae.getSource() == add) && (!taskInput.getText().equals("")))
			handleAdd(taskInput.getText().trim());
		else if ((ae.getSource() == del) && (list.getSelectedIndex() >= 0))
			handleDel(list.getSelectedIndex());
		else if ((ae.getSource() == up) && (list.getSelectedIndex() >= 0))
				handleIncPriority(list.getSelectedIndex());
		else if ((ae.getSource() == down) && (list.getSelectedIndex() >= 0))
				handleDecPriority(list.getSelectedIndex());
		else if (ae.getSource() == list)
			taskInput.setText(list.getSelectedItem());
		taskInput.requestFocus();
	}
	
	public static void main(String args[])
	{
		AppletTester tl	=	new AppletTester();
	}
}
*/
/*
class ConfirmDialog extends Dialog implements ActionListener
{
	private Button okey		=	new Button("Okey");
	private Button cancel	=	new Button("Cancel");
	private Label label		=	new Label("Are you sure?", Label.CENTER);
	public boolean isOkey	=	false;
	private class WindowCloser extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
			ConfirmDialog.this.isOkey	=	false;
			ConfirmDialog.this.hide();
		}
	}
	public ConfirmDialog(Frame parent)
	{
		this(parent, "Please confirm", "Are you sure?");
	}
	public ConfirmDialog(Frame parent, String title, String question)
	{
		super(parent, title, true);
		label.setText(question);
		setup();
		okey.addActionListener(this);
		cancel.addActionListener(this);
		addWindowListener(new WindowCloser());
		setResizable(false);
		pack();
		int x	=	parent.getLocation().x + (parent.getSize().width - getSize().width) / 2;
		int y	=	parent.getLocation().y + (parent.getSize().height - getSize().height) / 2;
		x = Math.min(getToolkit().getScreenSize().width - getSize().width, x);
		y = Math.min(getToolkit().getScreenSize().height - getSize().height, y);
		setLocation(Math.max(0, x), Math.max(0, y));
		show();
	}
	private void setup()
	{
		Panel buttons	=	new Panel();
		buttons.setLayout(new FlowLayout());
		buttons.add(okey);	buttons.add(cancel);
		setLayout(new BorderLayout());
		add("Center", label);	add("South", buttons);
		setLocationRelativeTo(null);
	    //setLocation(300, 300);
	}
	public void actionPerformed(ActionEvent ae)
	{
		isOkey = (ae.getSource() == okey);
		//---------------------- centerilzition ------------------------------------------------------
	    //--------------------------------------------------------------------------------------------
	    
	    //-------method II
		hide();
	}
	
}

public class AppletTester extends Frame implements ActionListener
{
	private MenuItem fileExit	=	new MenuItem("Exit");
	private class WindowCloser extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
			System.exit(0);
		}
	}
	public AppletTester()
	{
	super("Menu Test Program");
	Menu file	=	new Menu("File");
	file.add(fileExit);		fileExit.setEnabled(true);
	
	MenuBar bar	=	new MenuBar();
	bar.add(file);
	setMenuBar(bar);
	fileExit.addActionListener(this);
	addWindowListener(new WindowCloser());
	pack();
	//---------------------- centerilzition ------------------------------------------------------
	int windowWidth = this.getWidth();                    //获得窗口宽
    int windowHeight = this.getHeight();                  //获得窗口高
    Toolkit kit = Toolkit.getDefaultToolkit();             //定义工具包
    Dimension screenSize = kit.getScreenSize();            //获取屏幕的尺寸
    int screenWidth = screenSize.width;                    //获取屏幕的宽
    int screenHeight = screenSize.height;                  //获取屏幕的高
    this.setLocation(screenWidth/2-windowWidth/2, screenHeight/2-windowHeight/2);//设置窗口居中显示
    //--------------------------------------------------------------------------------------------
	setSize(500, 300);
	show();
	}
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource()	==	fileExit)
		{
			ConfirmDialog exit	=	new ConfirmDialog(this, "Confirm Exit", "Do you really want to exit?");
		
			if (exit.isOkey)
				System.exit(0);
		}
	}
	public static void main(String args[])
	{
		AppletTester at	=	new AppletTester();
	}
}
*/

//----------------------------------	绘图	-------------------------------------//
/*
public class AppletTester extends Frame
{
	private class WindowCloser extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
			System.exit(0);
		}
	}
	public AppletTester()
	{
		super("Graphics Test");
		setSize(400, 400);
		show();
		addWindowListener(new WindowCloser());
	}
	public void paint(Graphics g)
	{
		g.setColor(Color.black);	g.drawOval(10, 40, 90, 90);
		g.setColor(Color.red);		g.drawRect(40, 50, 500, 600);
		g.setColor(Color.blue);		g.drawString("Hello world", 300, 400);
		g.setColor(Color.green);	g.fillOval(120, 100, 400, 400);
		g.setColor(Color.pink);		g.fillArc(30, 120, 700, 700, 60, 120);
		g.setColor(Color.gray);		g.fill3DRect(90, 120, 200, 200, true);
		g.setColor(Color.black);
		
		g.drawOval(120, 100, 400, 400);
		g.drawArc(30, 120, 700, 700, 60, 120);
	}
	public static void main(String args[])
	{
		AppletTester at	=	new AppletTester();
	}
}
*/
/*
//---------------------------------	动画	------------------------------------//
public class AppletTester extends Frame implements ActionListener
{
	private final int WIDTH = 220, HEIGHT = 130, INC = 1;
	private Button left		=	new Button("Left");
	private Button right	=	new Button("Right");
	private Button up		=	new Button("Up");
	private Button	down	=	new Button("Down");
	private Button auto		=	new Button("Auto");
	private int x	=	50,	y	=	50;
	private class WindowCloser extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
			System.exit(0);
		}
	}
	public AppletTester()
	{
		super("Moving Box");
		setup();
		left.addActionListener(this);	right.addActionListener(this);
		up.addActionListener(this);		down.addActionListener(this);
		auto.addActionListener(this);
		addWindowListener(new WindowCloser());
		setSize(400, 400);
		show();
	}
	private void setup()
	{
		Panel buttons	=	new Panel();
		buttons.setLayout(new FlowLayout());
		buttons.add(up);	buttons.add(down);	buttons.add(left);	buttons.add(right);
		setLayout(new BorderLayout());
		add("South", buttons);
		Panel abs	=	new Panel();
		abs.setLayout(new FlowLayout());
		abs.add(auto);
		add("North", abs);
	}
	public void paint(Graphics g)
	{
		//g.setColor(Color.pink);
		g.setColor(Color.MAGENTA);
		//g.fillRect(x, y, WIDTH, HEIGHT);
		//g.fill3DRect(x, y, WIDTH, HEIGHT, false);
		g.fillRoundRect(3*x, 3*y, WIDTH, HEIGHT, WIDTH / 2, HEIGHT / 2);
	}
	public void moveUp()
	{
		y -= INC;
	}
	public void moveDown()
	{
		y += INC;
	}
	public void moveLeft()
	{
		x -= INC;
	}
	public void moveRight()
	{
		x += INC;
	}
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == up)
			moveUp();
		else if (e.getSource() == down)
			moveDown();
		else if (e.getSource() == left)
			moveLeft();
		else if (e.getSource() == right)
			moveRight();
		repaint();
		
		int i = 0;
		int j = 0;
		if (e.getSource() == auto)
		{
			while (i ++ < 30)
			{
				//moveUp();	moveRight();	moveDown();	moveLeft();//	moveRight();	moveRight();
				//moveRight();	repaint();
				moveRight();		repaint();
			}
			while (j ++ < 60)
			{
				//moveUp();	moveRight();	moveDown();	moveLeft();//	moveRight();	moveRight();
				//moveRight();	repaint();
				moveLeft();		repaint();
			}
		}
	}
	public static void main(String args[])
	{
		AppletTester at	=	new AppletTester();
	}
}
*/













































































⌨️ 快捷键说明

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