Java6学习笔记20——利用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 Respond extends JFrame implements ActionListener {
public static void main(String arg[]) {
new Respond();
}
public Respond() {
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")) {
int choice = JOptionPane.showConfirmDialog(this,
"Do you want to delete the file?");//静态方法直接调用
if(choice == 0)//取得用户相应
System.out.println("yes");
else if(choice == 1)
System.out.println("no");
else if(choice == 2)
System.out.println("cancel");
}
}
}

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