電子書籍の厳選無料作品が豊富!

retrun;を取ると動作がおかしくなるんですが
return;にはどういう働きがあるんでしょうか?
お願いします。


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
//<applet code = "a.class" width = "300" height = "300"></applet>

public class a extends Applet implements ActionListener{
Dialog d;
Frame f;

public void init() {
setLayout( new GridLayout( 1, 1 ) );
Button b = ( Button )add( new Button( "frame" ) );
b.addActionListener( this );
}

public void actionPerformed( ActionEvent e ){
if( e.getActionCommand() == "frame" ){
if ( f == null ){
f = new Frame( "Kitty on your lap" );
Button fb = (Button)f.add( new Button( "Kitty " ) );
fb.addActionListener( this );
f.setSize( 200 , 200 );
f.setVisible( true );

}else if( d == null ){
f.dispose();
f = null;
}
return;
}

if( e.getActionCommand() == "OK" ) {
d.dispose();
d = null;
return;
}

d = new Dialog( f, "Kitty", true );
d.setLayout( new GridLayout( 2, 1 ) );
d.setResizable( false );
d.add( new Label( "Kitty on your lap" ) );

Button b = (Button)d.add( new Button( "OK" ) );
b.addActionListener( this );

d.setSize( 400 , 200 );
d.setVisible( true );
}
}

A 回答 (2件)

return;は、現在実行中のメソッドを「そこで」終了させる役割があります。



今回記述されているreturn;は、2箇所ありますが、共にメソッド

public void actionPerformed( ActionEvent e ){
}

の中にあります。この場合、return;を実行すると以下のコマンドを実行せずにメソッドactionPerformed()を終了します。(この場合メソッドはイベントハンドラですが。)

必要な処理が終わったのに、return;を省いてしまうと、余分なことをしてしまうことになります。
    • good
    • 0
この回答へのお礼

早速の回答ありがとうございまあす。
>現在実行中のメソッドを「そこで」終了させる役割があります。
そういうことだったんですね!
どう調べていいのかも見当付かず
困ってました。
ありがとうございます!

お礼日時:2008/04/02 15:45

> d = new Dialog( f, "Kitty", true );


> d.setLayout( new GridLayout( 2, 1 ) );
> d.setResizable( false );
> d.add( new Label( "Kitty on your lap" ) );
>
> Button b = (Button)d.add( new Button( "OK" ) );
> b.addActionListener( this );
>
> d.setSize( 400 , 200 );
> d.setVisible( true );
この部分が何のアクションに対応しているのか、はっきりさせてください。そうすれば、途中にreturnを書く必要はなくなるでしょう。
    • good
    • 0
この回答へのお礼

早速の回答ありがとうございます
なるほど 提示された部分があるため
return;が必要なんですね。
プログラムは深いです。
どうもです!

お礼日時:2008/04/02 15:48

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!