实现效果
在文本框中输入内容,当单击+hello按钮时,在下方文本域中显示输入的内容+hello;当单击+bye按钮时,在下方文本域中显示输入的内容+bye。
实现过程
外部类实现
package ch9.dealevent;
import javax.swing.*;
import java.awt.*;
public class OutClass extends JFrame{
JLabel label;
JTextField text;
JButton button1,button2;
JTextArea textArea;
Box boxH;
Box boxOne,boxTwo;
OutClass(){
init();
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
label = new JLabel("请输入:");
text = new JTextField(10);
button1 = new JButton("+hello");
button2 = new JButton("+bye");
textArea = new JTextArea(9,25);
boxTwo = Box.createHorizontalBox();
boxOne = Box.createHorizontalBox();
boxOne.add(label);
boxOne.add(text);
boxTwo.add(button1);
boxTwo.add(button2);
add(boxOne);
add(boxTwo);
add(new JScrollPane(textArea));
button1.addActionListener(new OutClassListener(text,textArea,button1));
button2.addActionListener(new OutClassListener(text,textArea,button1));
}
}
//外部监听器
package ch9.dealevent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class OutClassListener implements ActionListener{
JButton button1,button2;
JTextField text;
JTextArea textArea;
OutClassListener(JTextField text,JTextArea textArea,JButton button){
this.text = text;
this.textArea = textArea;
this.button1 = button;
}
@Override
public void actionPerformed(ActionEvent e) {
String textStr = text.getText();
if(e.getSource() == button1){
textArea.append(textStr+"hello\n");
}else{
textArea.append(textStr+"bye\n");
}
}
}
内部类实现
package ch9.dealevent.InnerClass;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class InnerClass extends JFrame{
JLabel label;
JTextField text;
JButton button1,button2;
JTextArea textArea;
Box boxH;
Box boxOne,boxTwo;
class InnerListener implements ActionListener {
public void actionPerformed(ActionEvent e){
String textStr = text.getText();
if(e.getSource() == button1){
textArea.append(textStr+"hello\n");
}else{
textArea.append(textStr+"bye\n");
}
}
}
public InnerClass(){
init();
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
label = new JLabel("请输入:");
text = new JTextField(10);
button1 = new JButton("+hello");
button2 = new JButton("+bye");
textArea = new JTextArea(9,25);
boxTwo = Box.createHorizontalBox();
boxOne = Box.createHorizontalBox();
boxOne.add(label);
boxOne.add(text);
boxTwo.add(button1);
boxTwo.add(button2);
add(boxOne);
add(boxTwo);
add(new JScrollPane(textArea));
button1.addActionListener(new InnerListener());
button2.addActionListener(new InnerListener());
}
}
匿名类
package ch9.dealevent.InnerClass;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class AnonymousClass extends JFrame {
JLabel label;
JTextField text;
JButton button1,button2;
JTextArea textArea;
Box boxH;
Box boxOne,boxTwo;
public AnonymousClass(){
init();
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void init(){
setLayout(new FlowLayout());
label = new JLabel("请输入:");
text = new JTextField(10);
button1 = new JButton("+hello");
button2 = new JButton("+bye");
textArea = new JTextArea(9,25);
boxTwo = Box.createHorizontalBox();
boxOne = Box.createHorizontalBox();
boxOne.add(label);
boxOne.add(text);
boxTwo.add(button1);
boxTwo.add(button2);
add(boxOne);
add(boxTwo);
add(new JScrollPane(textArea));
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String textStr = text.getText();
textArea.append(textStr+"hello\n");
text.setText(null);
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String textStr = text.getText();
textArea.append(textStr+"hello\n");
text.setText(null);
}
});
}
}
自身类实现
package ch9.dealevent.InnerClass;
import java.awt.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.event.*;
import ch9.dealevent.OutClassListener;
public class SelfClass extends JFrame implements ActionListener{
JLabel label;
JTextField text;
JButton button1,button2;
JTextArea textArea;
Box boxH;
Box boxOne,boxTwo;
public SelfClass(){
init();
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
label = new JLabel("请输入:");
text = new JTextField(10);
button1 = new JButton("+hello");
button2 = new JButton("+bye");
textArea = new JTextArea(9,25);
boxTwo = Box.createHorizontalBox();
boxOne = Box.createHorizontalBox();
boxOne.add(label);
boxOne.add(text);
boxTwo.add(button1);
boxTwo.add(button2);
add(boxOne);
add(boxTwo);
add(new JScrollPane(textArea));
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String textStr = text.getText();
if(e.getSource() == button1){
textArea.append(textStr+"hello\n");
}else{
textArea.append(textStr+"bye\n");
}
}
}