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

📄 buttontest.java

📁 这是清华大学编写的JAVA教材中所有题目的源代码!
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -