- PR -

ウィンドウ処理について

投稿者投稿内容
grand_blue_x
会議室デビュー日: 2004/03/27
投稿数: 7
投稿日時: 2004-03-27 15:12
はじめまして、最近Javaを勉強し始めたgrand_blue_xといいます。
以後よろしくお願いします。

早速ですが、やりたいことがあるのですがどうすれば実現できるのかまったくわからないのです。
それは、簡単な質問プログラムです。
1、質問をつくる。
2、ウィンドウにチェックボックス(もしくはラジオボタン)をつくる。
3、Buttonを利用して次のアクションへ。
をしたいのですが、1,2はプログラムを書けるのですが、3がわかりません。
ボタンを押した後に例えば”正解”などとウィンドウに表示し、なおかつ次の質問を表示するにはどうすればいいのでしょうか?

中途半端な質問で申し訳ありませんがどうぞよろしくお願いします。
ぽん
大ベテラン
会議室デビュー日: 2003/05/13
投稿数: 157
投稿日時: 2004-03-27 15:41
分かるところまでのソースを提示してみてはいかがでしょうか?
(意図した動きをしなくても良いので)

その方が回答を付け易くなると思いますよ。
grand_blue_x
会議室デビュー日: 2004/03/27
投稿数: 7
投稿日時: 2004-03-27 22:11
レスありがとうございます^^;
ソースは以下の通りですが、正解とした後どうすれば、次の問題(最初と同じような画面)に戻れるのかがわからないのです。

import java.awt.*;
import java.awt.event.*;

public class Test7 extends Frame{
Label mylabel,mylabel2;
Checkbox cb1,cb2,cb3;
CheckboxGroup cbg;

public Test7(){
super();
setTitle("問題");
setSize(300,150);
setLayout(null);

cbg = new CheckboxGroup();
ClickItem ci = new ClickItem();

mylabel = new Label("今勉強しているのは何言語??");
mylabel.setBounds(50,25,200,25);
this.add(mylabel);

cb1 = new Checkbox("C言語",true,cbg);
cb1.setBounds(20,50,150,20);
this.add(cb1);
cb1.addItemListener(ci);

cb2 = new Checkbox("Java言語",true,cbg);
cb2.setBounds(20,70,150,20);
this.add(cb2);
cb2.addItemListener(ci);

cb3 = new Checkbox("Perl言語",true,cbg);
cb3.setBounds(20,90,150,20);
this.add(cb3);
cb3.addItemListener(ci);
}
public static void main(String args[]){
new Test7().show();
}

class ClickItem implements ItemListener{
public void itemStateChanged(ItemEvent ev){
String str1,str2;
if(cb1.getState()){
str1 = "不正解";
mylabel.setText(str1);
//System.exit(0);
}
else if(cb2.getState()){
str1 = "正解";
str2 = "next";
mylabel.setText(str1+str2);
}
else if(cb3.getState()){
str1 = "不正解";
mylabel.setText(str1);
//System.exit(0);
}
}
}
}

どうぞよろしくお願いします。
ぽん
大ベテラン
会議室デビュー日: 2003/05/13
投稿数: 157
投稿日時: 2004-03-28 10:37
修正してみました。

修正のポイントとしては
1.繰り返し処理を行えるようにする(initialize)
2.情報を体系的にまとめる(Question・Choice)
です。

※ソースは適当なので修正して下さい。
コード:

public class Test7 extends Frame {
private static final Question[] QUESTIONS;
private int number = 0;
Label mylabel, mylabel2;
Checkbox cb1, cb2, cb3;
CheckboxGroup cbg;
Question question;

public Test7() {
setTitle("問題");
setSize(300, 150);
setLayout(null);

cbg = new CheckboxGroup();
ClickItem ci = new ClickItem();

mylabel = new Label("問題");
mylabel.setBounds(50, 25, 200, 25);
this.add(mylabel);

mylabel2 = new Label("");
mylabel2.setBounds(8, 25, 50, 25);
this.add(mylabel2);


cb1 = new Checkbox("1",true,cbg);
cb1.setBounds(20,50,150,20);
this.add(cb1);
cb1.addItemListener(ci);

cb2 = new Checkbox("2",true,cbg);
cb2.setBounds(20,70,150,20);
this.add(cb2);
cb2.addItemListener(ci);

cb3 = new Checkbox("3",true,cbg);
cb3.setBounds(20,90,150,20);
this.add(cb3);
cb3.addItemListener(ci);

initialize(QUESTIONS[number]);


addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});
}

public void initialize(final Question question) {
this.question = question;

this.mylabel.setText(question.getText());
this.cb1.setLabel(question.getChoiceTitle(0));
this.cb2.setLabel(question.getChoiceTitle(1));
this.cb3.setLabel(question.getChoiceTitle(2));

doLayout();
}

public static void main(String args[]) {
new Test7().show();
}

class ClickItem implements ItemListener {
public void itemStateChanged(ItemEvent ev) {
String str1, str2;
final int index;
if (cb1.getState()) {
index = 0;
str1 = "不正解";
//System.exit(0);
}
else if (cb2.getState()) {
index = 1;
}
else if (cb3.getState()) {
index = 2;
}
else {
throw new IllegalArgumentException();
}

final String text;
if (question.isCorrect(index)) {
text = "正解";
}
else {
text = "不正解";
}

mylabel2.setText(text);

if (QUESTIONS.length > (number + 1)) {
number++;
}
else {
number = 0;
}

initialize(QUESTIONS[number]);
}
}

static {
final ArrayList question = new ArrayList();

question.add(new Question("今勉強しているのは何言語??",new Choice[]{new Choice("C言語",false),new Choice("Java言語",true),new Choice("Perl言語",false)}));
question.add(new Question("今の時間は??",new Choice[]{new Choice("1時",true),new Choice("2時",false),new Choice("3時",false)}));

QUESTIONS = (Question[])question.toArray(new Question[0]);
}
}
class Question {
private final String text;
private final Choice[] choices;

public Question(final String text,final Choice[] choices) {
this.text = text;
this.choices = choices;
}

public String getText() {
return this.text;
}

public String getChoiceTitle(final int index) {
return this.choices[index].getTitle();
}

public boolean isCorrect(final int index) {
return this.choices[index].isCorrect();
}
}
class Choice {
private final String title;
private final boolean correct;

public Choice(final String title,final boolean correct) {
this.title = title;
this.correct = correct;
}

public String getTitle() {
return this.title;
}

public boolean isCorrect() {
return this.correct;
}
}



[ メッセージ編集済み 編集者: ぽん 編集日時 2004-03-28 10:38 ]
grand_blue_x
会議室デビュー日: 2004/03/27
投稿数: 7
投稿日時: 2004-03-30 09:15
返事が遅くなりましてすみません。
ぼんさん、ご親切にありがとうございました。

まだ全然さわりの部分だと思いますが、Javaってパズルみたいで面白いですね^^;
まだ、ソースの全ては理解できませんがなんとなく、流れはわかりました。

また、質問があるのですが、
public class Test extends Frame{
//略
public static void main(String args[]){
new Test().show();
new Test1().show();
}
}
class Test1 extends Frame{
//略
}
というソースだとウィンドウが二つ現れてしまうのですが、クリックなどの情報を生かして同じウィンドウに表す(画面の切り替え?)場合はどうすればよろしいのですか?

質問ばかりで申し訳ありませんがどうぞご教授願います。

ぽん
大ベテラン
会議室デビュー日: 2003/05/13
投稿数: 157
投稿日時: 2004-03-30 12:35
引用:

grand_blue_xさんの書き込み (2004-03-30 09:15) より:

まだ全然さわりの部分だと思いますが、Javaってパズルみたいで面白いですね^^;


おっそれは良かったですね。

引用:

public class Test extends Frame{
//略
public static void main(String args[]){
new Test().show();
new Test1().show();
}
}
class Test1 extends Frame{
//略
}
というソースだとウィンドウが二つ現れてしまうのですが、クリックなどの情報を生かして同じウィンドウに表す(画面の切り替え?)場合はどうすればよろしいのですか?


下記のソースで如何でしょう?

ポイントはコンポーネントを直接ContentPaneに追加するのではなく、
いったんPanelに追加してからContentPaneに追加する事です。
そうする事によって追加or削除が容易になり、また追加or削除の範囲を決める事も出来ます。

コード:
public class Page {
    private final JFrame m_frame = new JFrame();
    private final JPanel m_firstPage = new JPanel();
    private final JPanel m_secondPage = new JPanel();
        
    public Page() {
        m_frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){System.exit(0);}
        });
                
        final JButton firstTrigger = new JButton("ページ2へ");
        firstTrigger.addActionListener(new NextPageAction(m_firstPage,m_secondPage));
        m_firstPage.add(firstTrigger);
        m_firstPage.add(new JTextField("first"));
    
        final JButton secondTrigger = new JButton("ページ1へ");
        secondTrigger.addActionListener(new NextPageAction(m_secondPage,m_firstPage));
        m_secondPage.add(secondTrigger);
        m_secondPage.add(new JCheckBox("second"));
    
        
        m_frame.getContentPane().add(new JLabel("ページ切り替え"),BorderLayout.NORTH);
        m_frame.getContentPane().add(m_firstPage);

        m_frame.pack();
        m_frame.show();
    }
    
    private class NextPageAction implements ActionListener {
        private final JPanel m_thisPage;
        private final JPanel m_nextPage;
        
        public NextPageAction(final JPanel thisPage,final JPanel nextPage) {
            m_thisPage = thisPage;
            m_nextPage = nextPage;
        }
        
        public void actionPerformed(ActionEvent e) {
            m_frame.getContentPane().remove(m_thisPage);
            
            m_frame.getContentPane().add(m_nextPage);
            
            m_frame.pack();
        }
    }
    
    public static void main(final String[] args) {
        final Page page = new Page();
        
    }
}

grand_blue_x
会議室デビュー日: 2004/03/27
投稿数: 7
投稿日時: 2004-03-31 23:57
度々、ありがとうございます。

まさに自分がやりたいことが実現できそうです^^;
前回頂いたソースと今回頂いたソースをある程度改良して動くようになりました。

もし、Javaを学習する上で読んでいたほうがいいという本などご存知でしたら教えていただけないでしょうか?

ぽん
大ベテラン
会議室デビュー日: 2003/05/13
投稿数: 157
投稿日時: 2004-04-01 12:01
引用:

grand_blue_xさんの書き込み (2004-03-31 23:57) より:

もし、Javaを学習する上で読んでいたほうがいいという本などご存知でしたら教えていただけないでしょうか?


取り合えずこのスレッドを見てみては?
他にも同様なスレッドがいくつかあったので、探してみると良さそうです。

スキルアップ/キャリアアップ(JOB@IT)