buttontest.java

来自「这是清华大学编写的JAVA教材中所有题目的源代码!」· Java 代码 · 共 56 行

JAVA
56
字号
import java.awt.*;
import java.awt.event.*;

class ButtonTest extends Frame implements ActionListener
{
	TextField tf=new TextField(20);
	Button btn1=new Button("Left");
	Button btn2=new Button("Right");

	public ButtonTest(String title) 
	{
		super(title);

		btn1.setActionCommand("b1");
		btn1.addActionListener(this);
		btn2.setActionCommand("b2");
		btn2.addActionListener(this);

		setLayout(new FlowLayout());
		add(tf);
		add(btn1);
		add(btn2);
		setSize(200,150);
		setVisible(true);		
	}

	public void actionPerformed(ActionEvent e)
	{
		/*
		if(e.getActionCommand().equals("b1"))
			tf.setText("你按了Left按纽!");
		else if(e.getActionCommand().equals("b2"))
			tf.setText("你按了Right按纽!");
		*/
		/*
		Button b=(Button)e.getSource();
		if(b==btn1)
			tf.setText("你按了Left按纽!");
		else if(b==btn2)
			tf.setText("你按了Right按纽!");
		*/
		
		Button b=(Button)e.getSource();
		if(b.getLabel().equals("Left"))
			tf.setText("你按了Left按纽!");
		else if(b.getLabel().equals("Right"))
			tf.setText("你按了Right按纽!");
		
	}

	public static void main(String args[])
	{
		new ButtonTest("按钮示例");
	}
}

⌨️ 快捷键说明

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