日期:2014-05-20 浏览次数:20670 次
import java.awt.Container; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class selfDefinedDialog1 implements ActionListener{ JFrame f; JDialog dialog; public selfDefinedDialog1(JFrame f){ this.f= f; dialog=new JDialog(f,"自定义对话框",true); Container cp=dialog.getContentPane(); cp.setLayout(new FlowLayout()); JLabel lb1=new JLabel("你是2b"); JButton yes=new JButton("yes"),no=new JButton("no"); JPanel jpl=new JPanel(new FlowLayout()); jpl.add(lb1); jpl.add(yes); jpl.add(no); cp.add(jpl); dialog.setBounds(150,150,300,300); dialog.setVisible(true); no.addActionListener(this); yes.addActionListener(this); } public void actionPerformed(ActionEvent e){ String str=e.getActionCommand(); if(str.equals("yes")){ Graphics g = null; g.drawString("汉昌宁是2b", 100, 100); } else if(str.equals("no")) { //dialog.dispose(); System.exit(0); } } }
no.addActionListener(this); yes.addActionListener(this); addWindowListener(new MyWindowAdapter2()); setVisible(true);