radiobutton の イベント
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class radioTeset extends JFrame implements ActionListener{
//★①
JRadioButton j1;
JRadioButton j2;
ButtonGroup bg;
JLabel ans;
public radioTeset(){
setSize(500,500);
setLayout(null);
setVisible(true);
ans = new JLabel("answer");
ans.setBounds(100,50,100,20);
add(ans);
//★②
j1=new JRadioButton("style1");
j2=new JRadioButton("style2");
j1.setBounds(100,100,100,20);
j2.setBounds(100,120,100,20);
j1.addActionListener(this);
j2.addActionListener(this);
//★③
bg=new ButtonGroup();
bg.add(j1);
bg.add(j2);
add(j1);
add(j2);
}
public void actionPerformed(ActionEvent e){
//★④
if(e.getSource()==j1){
ans.setText("j1");
}else if(e.getSource()==j2){
ans.setText("j2");
}
}
public static void main(String[] args){
new radioTeset();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class radioTeset extends JFrame implements ActionListener{
//★①
JRadioButton j1;
JRadioButton j2;
ButtonGroup bg;
JLabel ans;
public radioTeset(){
setSize(500,500);
setLayout(null);
setVisible(true);
ans = new JLabel("answer");
ans.setBounds(100,50,100,20);
add(ans);
//★②
j1=new JRadioButton("style1");
j2=new JRadioButton("style2");
j1.setBounds(100,100,100,20);
j2.setBounds(100,120,100,20);
j1.addActionListener(this);
j2.addActionListener(this);
//★③
bg=new ButtonGroup();
bg.add(j1);
bg.add(j2);
add(j1);
add(j2);
}
public void actionPerformed(ActionEvent e){
//★④
if(e.getSource()==j1){
ans.setText("j1");
}else if(e.getSource()==j2){
ans.setText("j2");
}
}
public static void main(String[] args){
new radioTeset();
}
}