import java.awt.event.*;
import javax.swing.*;
public class DialogHaHa extends JDialog implements ActionListener{
public static void main(String[] args) {
new DialogHaHa().setVisible(true);
}
private JTextField name;
private JCheckBox c1,c2,c3;
private JRadioButton r1,r2,r3;
private JButton ok,no;
private ButtonGroup g;
public DialogHaHa(){
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setSize(300,218);
this.setTitle("hahahaha...");
this.setLocationRelativeTo(null);
this.setResizable(false);
this.getContentPane().setLayout(null);
JLabel l = new JLabel("姓名:");
l.setBounds(10,10,40,22);
add(l);
name = new JTextField();
name.setBounds(50,10,100,22);
add(name);
l = new JLabel("愛好:");
l.setBounds(10,50,40,22);
add(l);
c1=new JCheckBox("音樂");
c2=new JCheckBox("運動");
c3=new JCheckBox("上網");
c1.setBounds(50,50,60,22);
c2.setBounds(110,50,60,22);
c3.setBounds(170,50,60,22);
add(c1);add(c2);add(c3);
l = new JLabel("將來要當:");
l.setBounds(10,90,68,22);
add(l);
r1=new JRadioButton("經理");
r2=new JRadioButton("工程師");
r3=new JRadioButton("教師");
r1.setBounds(78,90,60,22);
r2.setBounds(140,90,78,22);
r3.setBounds(218,90,60,22);
add(r1);add(r2);add(r3);
g =new ButtonGroup();
g.add(r1);g.add(r2);g.add(r3);
ok = new JButton("確定");
ok.setBounds(80,150,78,23);
ok.addActionListener(this);
add(ok);
no = new JButton("離開");
no.setBounds(168,150,78,23);
no.addActionListener(this);
add(no);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(ok)){
JOptionPane.showMessageDialog(this,"我是:"+name.getText()+"\n愛好:"+fav()+" \n將來想當:"+fau());
}
else{
this.dispose();
}
}
private String fau() {
return r1.isSelected()?r1.getText():r2.isSelected()?r2.getText():r3.isSelected()?r3.getText():"";
}
private String fav() {
String s ="";
s+=c1.isSelected()?c1.getText():"";
s+=",";
s+=c2.isSelected()?c2.getText():"";
s+=",";
s+=c3.isSelected()?c3.getText():"";
if(s.endsWith(","))s=s.substring(0,s.length()-1);
return s;
}
}