如何给 NorthButton 按钮添加一个点击事件 点击后添出一个消息框显示点了NorthButton就行..
我试了几下,也看了几个老贴的例子..依然没改好,哪位朋友能告诉我下该把事件的实现代码加在哪里?或是说我这代码还有更多的问题?
谢谢!!
代码
************************************************************************
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class MyFrame extends JFrame
{
JPanel MyPanel;
JButton NorthButton;
JButton SouthButton;
JButton EastButton;
JButton WestButton;
MyFrame()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,300);
setTitle("东南西北BorderLayout练习");
Init();
show();
}
void Init()
{
MyPanel = (JPanel)getContentPane();
BorderLayout b = new BorderLayout();
MyPanel.setLayout(b);
NorthButton = new JButton("北");
SouthButton = new JButton("南");
EastButton = new JButton("东");
WestButton = new JButton("西");
NorthButton.setSize(60,20);
SouthButton.setSize(80,20);
MyPanel.add(NorthButton,BorderLayout.NORTH);
MyPanel.add(SouthButton,BorderLayout.SOUTH);
MyPanel.add(EastButton,BorderLayout.EAST);
MyPanel.add(WestButton,BorderLayout.WEST);
NorthButton.addActionListener(new my());//注册监听器
}
}
class my implements ActionListener //实现监听器
{
public void actionPerformed(ActionEvent e)
{
//是在这里面实现吗?我怎么弄不出来?
}
}
class Frame1
{
public static void main(String [] args)
{
MyFrame fr = new MyFrame();
}
}