Java添加事宜監聽的四種辦法代碼實例。本站提示廣大學習愛好者:(Java添加事宜監聽的四種辦法代碼實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java添加事宜監聽的四種辦法代碼實例正文
Java添加事宜的幾種方法(轉載了codebrother的文章,做了略微的修改):
/**
* Java事宜監聽處置——本身類完成ActionListener接口,作為事宜監聽器
*
* @author codebrother
*/
class EventListener1 extends JFrame implements ActionListener {
private JButton btBlue, btDialog;
public EventListener1() {
setTitle("Java GUI 事宜監聽處置");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("藍色");
btDialog = new JButton("彈窗");
// 將按鈕添加事宜監聽器
btBlue.addActionListener(this);
btDialog.addActionListener(this);
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// ***************************事宜處置***************************
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btBlue) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
else if (e.getSource() == btDialog) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}
}
/**
* Java事宜監聽處置——外部類處置
*
* @author codebrother
*/
class EventListener3 extends JFrame {
private JButton btBlue, btDialog;
// 結構辦法
public EventListener3() {
setTitle("Java GUI 事宜監聽處置");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("藍色");
btDialog = new JButton("彈窗");
// 添加事宜監聽器對象(面向對象思惟)
btBlue.addActionListener(new ColorEventListener());
btDialog.addActionListener(new DialogEventListener());
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 外部類ColorEventListener,完成ActionListener接口
class ColorEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
}
// 外部類DialogEventListener,完成ActionListener接口
class DialogEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}
}
/**
* Java事宜監聽處置——匿名外部類處置
*
* @author codebrother
*/
class EventListener2 extends JFrame {
private JButton btBlue, btDialog;
public EventListener2() {
setTitle("Java GUI 事宜監聽處置");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("藍色");
btDialog = new JButton("彈窗");
// 添加事宜監聽器(此處即為匿名類)
btBlue.addActionListener(new ActionListener() {
// 事宜處置
@Override
public void actionPerformed(ActionEvent e) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
});
// 並添加事宜監聽器
btDialog.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
});
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/**
* Java事宜監聽處置——內部類處置
*
* @author codebrother
*/
class EventListener4 extends JFrame {
private JButton btBlue, btDialog;
public EventListener4() {
setTitle("Java GUI 事宜監聽處置");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("藍色");
btDialog = new JButton("彈窗");
// 將按鈕添加事宜監聽器
btBlue.addActionListener(new ColorEventListener(this));
btDialog.addActionListener(new DialogEventListener());
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
// 內部類ColorEventListener,完成ActionListener接口
class ColorEventListener implements ActionListener {
private EventListener4 el;
ColorEventListener(EventListener4 el) {
this.el = el;
}
@Override
public void actionPerformed(ActionEvent e) {
Container c = el.getContentPane();
c.setBackground(Color.BLUE);
}
}
// 內部類DialogEventListener,完成ActionListener接口
class DialogEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}
public class ActionListenerTest
{
public static void main(String args[])
{
new EventListener2();
}
}