1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Graphic extends JFrame implements ActionListener{
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT =500;
private static final int FRAME_X_ORIGIN =250;
private static final int FRAME_Y_ORIGIN =250;
JTextField t1;
JButton b;
JLabel label1, label2, label3;
JCheckBox cbBtn;
JCheckBox cbBtn1;
JCheckBox cbBtn2;
ButtonGroup languageGroup = new ButtonGroup( );
JRadioButton br1,br2,br3;
public static void main(String [] arg){
Graphic frame = new Graphic();
frame.setVisible(true);
}
public Graphic(){
Container contentPane;
setSize (FRAME_WIDTH, FRAME_HEIGHT);
setTitle("My First Frame");
setLocation(FRAME_X_ORIGIN,FRAME_Y_ORIGIN);
contentPane=getContentPane();
contentPane.setBackground(Color.CYAN);
contentPane.setLayout(new FlowLayout());
label1 = new JLabel("Choose your age");
label2 = new JLabel("bbb");
label3 = new JLabel("ccc");
cbBtn= new JCheckBox("18-20 years old");
cbBtn1= new JCheckBox("20-23 years old");
cbBtn2= new JCheckBox("+23 years old");
t1=new JTextField();
b = new JButton("ok");
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(cbBtn);
contentPane.add(cbBtn1);
contentPane.add(cbBtn2);
if (cbBtn.isSelected())
{
System.out.println("You age is"+ cbBtn.getText());
}
//else{
// System.out.println("You age is "+ cbBtn2.getText ());
//}
//}
String[] comboBoxItem= {"Woman", "Man"};
JComboBox comboBox = new JComboBox(comboBoxItem);
contentPane.add(comboBox);
//String selection = (String) comboBox.getSelectedItem();
//System.out.println(selection);
t1.setPreferredSize( new Dimension( 200, 24) );
br1 = new JRadioButton("un");
br2 = new JRadioButton("deux");
br3 = new JRadioButton("trois");
// ajout des boutons radio dans le groupe bg
languageGroup.add(br1);
languageGroup.add(br2);
languageGroup.add(br3);
contentPane.add(t1);
contentPane.add(b);
contentPane.add(br1);
contentPane.add(br2);
contentPane.add(br3);
if (br1.isSelected()){
System.out.println("AAAAAAAAA");
}
b.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String x=t1.getText();
label1.setText(x);
System.out.println("there !");
}
} |
Partager