62,621
社区成员
发帖
与我相关
我的任务
分享import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
*
*/
/**
* @author sunsnowad
*
*/
public class Demo extends JFrame{
JButton button;
JLabel labelResult;
public Demo(){
super();
init();
}
private void init() {
this.setLayout(new FlowLayout());
this.setSize(400, 300);
this.setLocation(0, 0);
button = new JButton("test");
button.addActionListener(new CustomActionListener(this));
this.add(button);
labelResult = new JLabel("show result");
this.add(labelResult);
}
/**
* @param args
*/
public static void main(String[] args) {
Demo frame = new Demo();
frame.setVisible(true);
}
}
class CustomActionListener implements ActionListener {
Demo demo;
public CustomActionListener(Demo demo) {
this.demo = demo;
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
demo.labelResult.setText(JOptionPane.showInputDialog(""));
}
}