buttonevent.txt

来自「java中图形用户界面的设计」· 文本 代码 · 共 65 行

TXT
65
字号
//buttonEvent
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class buttonEvent 
{
	public static void main(String args[])
	{
		 myFrame Frame1 = new myFrame("My first frame");
		 Frame1.show();
		 Frame1.pack();
	}
}

class myFrame extends JFrame implements ActionListener
{
	 JButton button1 = new JButton("North");
	 JButton button2 = new JButton("south");
	 JButton button3 = new JButton("west");
	 JButton button4 = new JButton("east");
	 JTextArea text = new JTextArea(10,20);
	 
	myFrame(String str)
	{
		super(str);
		setSize(300,200);
		Container Conpane = getContentPane();
		Conpane.add(button1,BorderLayout.NORTH);
		Conpane.add(button2,BorderLayout.SOUTH);
		Conpane.add(button3,BorderLayout.WEST);
		Conpane.add(button4,BorderLayout.EAST);
		Conpane.add(text,BorderLayout.CENTER);
		
		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		button4.addActionListener(this);
		
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == button1)
			setText("North");
		else if(e.getSource() == button2)
			setText("south");
		else if(e.getSource() == button3)
			setText("west");
		else if(e.getSource() == button4)
			setText("east");
	}
	
	private void setText(String str)
	{
		text.setText(str);
	}
	
}

⌨️ 快捷键说明

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