back

Panelを極めよう! (3/8)

■ボタンを使ってみる

では、実際の利用例をあげておきましょう。ボタンをクリックすると、ラベルにメッセージを表示するというものです。

ここでは、ComponentListenerを使うようなものはボタンが1つあるだけですから、いちいちswitchで分岐させる必要はありませんが、まぁ書き方の基本にしたがって書いておきました。


(by. SYODA-Tuyano.)

※リストが表示されない場合

AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。

※MainPanelの修正
class MainPanel extends Panel implements ComponentListener {
    private Label label;
    private Button button;
    
    MainPanel() {
        setTitle("Start IApplication");
        HTMLLayout layout = new HTMLLayout();
        this.setLayoutManager(layout);
        label = new Label("List Sample");
        this.add(label);
        layout.br();
        button = new Button("Click");
        this.add(button);
        this.setComponentListener(this);
    }

    public void componentAction(Component source, int type, int param) {
        switch(type){
        case ComponentListener.BUTTON_PRESSED:
            if (source == button){
                label.setText("buttonを押した。");
            }
            break;
        }
    }
}




   

記事のリストに戻る



PC Site G+ mail