Java6学习笔记14——利用Swing创建确定对话框

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Okay extends JFrame implements ActionListener {
public static void main(String arg[]) {
new Okay();
}
public Okay() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
setLocation(250,150);
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
JButton button = new JButton("Dialog");
button.addActionListener(this);
pane.add(button);
pack();

setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String selection = e.getActionCommand();
if(selection.equals("Dialog")) {
JOptionPane.showMessageDialog(this,"You're hair is green.");//静态方法可直接调用
}
}
}

你可能感兴趣的:(java,swing)