import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class IdealWeight extends JFrame implements ChangeListener {
JRadioButton genderM, genderF;
ButtonGroup genderGroup;
JPanel genderPanel;
JRadioButton heightA, heightB, heightC, heightD, heightE;
ButtonGroup heightGroup;
JPanel heightPanel;
JTextField resultText;
JLabel resultLabl;
JPanel resultPanel;
int Result1 = 35;
int Result2 = 55;
int Result3 = 65;
int Result4 = 75;
int Result5 = 85;
int result = 0;
public IdealWeight() {
setTitle("Your Ideal Weight");
setDefaultCloseOperation(EXIT_ON_CLOSE);
//genderGroup
genderM = new JRadioButton("MALE", true);
genderF = new JRadioButton("Female", false);
genderGroup = new ButtonGroup();
genderGroup.add(genderM);genderGroup.add(genderF);
genderM.addChangeListener(this);
genderF.addChangeListener(this);
genderPanel = new JPanel();
genderPanel.setLayout( new BoxLayout(genderPanel, BoxLayout.Y_AXIS));
genderPanel.add( new JLabel("Your Gender"));
genderPanel.add(genderM);genderPanel.add(genderF);
//heightGroup
heightA = new JRadioButton("60 to 64 inches", true );
heightB = new JRadioButton("64 to 68 inches", false );
heightC = new JRadioButton("68 to 72 inches", false );
heightD = new JRadioButton("72 to 76 inches", false );
heightE = new JRadioButton("76 to 80 inches", false );
heightGroup = new ButtonGroup();
heightGroup.add( heightA ); heightGroup.add( heightB );
heightGroup.add( heightC ); heightGroup.add( heightD );
heightGroup.add( heightE );
heightPanel = new JPanel();
heightPanel.setLayout( new BoxLayout( heightPanel, BoxLayout.Y_AXIS ) );
heightPanel.add( new JLabel("Your Height") );
heightPanel.add( heightA ); heightPanel.add( heightB );
heightPanel.add( heightC ); heightPanel.add( heightD );
heightPanel.add( heightE );
heightA.addChangeListener(this);
heightB.addChangeListener(this);
heightC.addChangeListener(this);
heightD.addChangeListener(this);
heightE.addChangeListener(this);
// result panel
resultText = new JTextField(7);
resultText.setEditable( false );
resultLabl = new JLabel("Ideal Weight");
resultPanel = new JPanel();
resultPanel.add( resultLabl );
resultPanel.add( resultText );
// frame: use default layout manager
add( genderPanel, BorderLayout.WEST );
add( heightPanel, BorderLayout.EAST );
add( resultPanel, BorderLayout.SOUTH );
}
protected void genderResult(ChangeEvent evt) {
JRadioButton source;
source = (JRadioButton)evt.getSource();
if (source == genderM) {
if (source == heightA) {
result += Result1;
}
else if(source == heightB) {
result = result + Result2;
}
else if(source == heightC) {
result = result + Result3;
}
else if(source == heightD) {
result = result + Result4;
}
else if(source == heightE) {
result = result + Result5;
}
}
if (source == genderF){
if (source == heightA) {
result = result + Result1-10;
}
else if(source == heightB) {
result = result + Result2-10;
}
else if(source == heightC) {
result = result + Result3-10;
}
else if(source == heightD) {
result = result + Result4-10;
}
else if(source == heightE) {
result = result + Result5-10;
}
}
resultText.setText(result+"");
}
public void stateChanged(ChangeEvent evt) {
genderResult(evt);
}
public static void main ( String[] args )
{
IdealWeight weightApp = new IdealWeight() ;
weightApp.setSize( 250, 225 );
weightApp.setResizable( false );
weightApp.setVisible( true );
}
}
두 라디오 버튼 그룹중에 남자와 여자 성별에 따른 몸무게를 출력하려고 합니다. 성별 선택 이후 키선택을 하고 몸무게 값이 나오게 하려고 하는데, 결과값이 0으로만 나옵니다.
ItemListener 인터페이스를 쓰게 되면 해결은 되는데, ChangeListener 를 사용해서 결과값을 나오게 하고 싶은데요 좋은 방법있을까요.