E001 // 이벤트 대기중
E002 // '시작' 선택
E003 // '문밖으로 나간다' 선택
E004 // '이전으로' 선택
E005 // '여기에서 찾아본다' 선택
E006 // '책상 위' 선택
E007 // '처음으로' 선택
import java.awt.*;
import javax.swing.*;
public class FindKeyGameMain
{
1 public static void main(String args[]) {
String[][] questions = {
{ "1", "열쇠를 찾아라!", "시작" },
{ "2", "<html>약속이 있어서 나가야 하는데 차 키가 없다<br>열쇠는 어디에 있을까?<br> </html>", "문 밖으로 나간다", "여기서 찾아본다", "4" },
{ "3", "<html>거실에 TV와 화분이 보인다<br>열쇠는 어디에 있을까?<br> </html>", "이전으로", "화분 주변", "6" },
{ "4", "<html>서재에 책장과 책상이 보인다<br>열쇠는 어디에 있을까?<br> </html>", "책상 위", "책장 선반", "6" },
{ "5", "열쇠 찾기 성공!", "처음으로" },
{ "6", "열쇠 찾기 실패!", "처음으로" },
};
final JFrame frame = new JFrame( "열쇠찾기" );
final CardLayout cards = new CardLayout( 20, 20 );
frame.setLayout( cards );
2 for( String[] question : questions )
3 frame.add( new QuestionPanel( frame.getContentPane(), cards, question ), question[0] );
frame.setPreferredSize( new Dimension( 400,250 ) );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
4 }
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QuestionPanel extends JPanel
{
private JRadioButton option1, option2;
private String nextOption2;
private Container frame;
private CardLayout cards;
Pb public QuestionPanel( Container frame, CardLayout cards, String[] question ) {
setLayout( new GridLayout( 3, 1 ) );
JLabel label = new JLabel( question[1] );
label.setFont( new Font( "굴림", Font.BOLD, 17 ) );
add( label );
ClickListener click = new ClickListener();
if( ( question.length > 2 ) && ( question[2] != null ) && !question[2].equals("") ) {
option1 = new JRadioButton( question[2] );
option1.setFont( new Font( "굴림", Font.BOLD, 17 ) );
option1.addActionListener( click );
add( option1 );
}
if( ( question.length > 4 ) && ( question[3] != null ) && !question[3].equals("") ) {
option2 = new JRadioButton( question[3] );
option2.setFont( new Font( "굴림", Font.BOLD, 17 ) );
option2.addActionListener( click );
add( option2 );
nextOption2 = question[4];
}
this.frame = frame;
this.cards= cards;
Pe }
private class ClickListener implements ActionListener {
Lb public void actionPerformed( ActionEvent event ) {
if ( ( event.getSource() == option1 ) && ( option1.getText().equals( "처음으로" ) ) ) {
option1.setSelected( false );
L1 cards.first( frame );
}
else if ( ( event.getSource() == option1 ) && ( option1.getText().equals( "이전으로" ) ) ) {
option1.setSelected( false );
L2 cards.previous( frame );
}
else if ( event.getSource() == option1 ) {
option1.setSelected( false );
L3 cards.next( frame );
}
else if ( event.getSource() == option2 ) {
option2.setSelected( false );
L4 cards.show( frame, nextOption2 );
}
}
Le }
}