firstapplet.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 39 行

JAVA
39
字号
import java.awt.*; // graphical classes
import java.awt.event.*; // event classes

/**	An applet that contains two buttons and a text field.  When the buttons
	on the screen are clicked, the value in the text field changes accordingly. */
public class FirstApplet extends java.applet.Applet implements ActionListener
{
	Button countBtn, resetBtn;
	int counter = 0;
	TextField counterFld;

	public void init()
	{
		setLayout(new FlowLayout(FlowLayout.CENTER)); // how items will be laid out
		countBtn = new Button("Click Me!");
		resetBtn = new Button("Reset");
		counterFld = new TextField("0", 4);
		
		/*	Indicate who will handle events. */
		countBtn.addActionListener(this);
		resetBtn.addActionListener(this);

		/*	Add components to display. */
		add(countBtn);
		add(counterFld);
		add(resetBtn);
	}

	/**	This method is what is called when the buttons are clicked. */ 
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource() == countBtn) // count button clicked
			counter++;
		else
			counter = 0;
		counterFld.setText(counter + ""); // update text field
	}
}

⌨️ 快捷键说明

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