Lecture

2019マルチメディアプログラミング実習冬休み宿題

このページは、学部2年生向け授業である、「マルチメディアプログラミング実習」 のために用意しました。

(Wikiの仕様で大文字小文字が混在した英単語に疑問符?が追加されるところがありますが、無視してください。)

冬休みに以下をやってください

下の内容を少しずつスクロールして自習してください。このうち、

ラジオボタンとチェックボックス

JRadioButton.png

ラジオボタンとチェックボックスの例です。ラジオボタンを排他制御するには、ボタングループを使います。以下のプログラムの動作を確認してください。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class JRadioButtonSample extends JFrame {

   public void initialize() {
       this.setTitle("Radio Buttons");

       JPanel panel1 = new JPanel();
       JPanel panel2 = new JPanel();

       JRadioButton radio1 = new JRadioButton("for here");
       JRadioButton radio2 = new JRadioButton("to go");
       ButtonGroup group = new ButtonGroup();
       group.add(radio1);
       group.add(radio2);
   
       panel1.setLayout(new GridLayout(2,1));
       panel2.setLayout(new GridLayout(3,1));
   
       panel1.add(radio1);
       panel1.add(radio2);
   
       panel2.add(new JCheckBox("with Drink"));
       panel2.add(new JCheckBox("with Salad"));
       panel2.add(new JCheckBox("with Cake"));
   
       Container container = this.getContentPane();
       container.add(panel1,BorderLayout.WEST);
       container.add(panel2,BorderLayout.EAST);
       
       this.pack();
       this.setVisible(true);
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   public static void main (String args[]) {
       JRadioButtonSample jrbs = new JRadioButtonSample();
       jrbs.initialize();
   }
   
}

ラジオボタンとラベル

演習:上で作ったプログラムに、JLabelのラベルを一枚追加しましょう。下部に追加します。

lunch1.png

ヒント:

ラベルはこんな感じで作ります。センタリングの指定はこのようにします。

       JLabel label = new JLabel("1000 yen");
       label.setHorizontalAlignment(JLabel.CENTER);

ラベルのためにパネルは用意しなくても良いようです。コンテナにaddすれば良いようです。この時、下部に追加するよう指定します。

       container.add(label,BorderLayout.SOUTH);














(スクロール注意:この先に解答例があります。できるだけ見ないで、自力で考えてください。)


























解答例:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class JRadioButtonSample2 extends JFrame {

	public void initialize() {
       this.setTitle("Radio Buttons");

       JPanel panel1 = new JPanel();
       JPanel panel2 = new JPanel();
       
       JLabel label = new JLabel("1000 yen");
       label.setHorizontalAlignment(JLabel.CENTER);
       
       JRadioButton radio1 = new JRadioButton("for here");
       JRadioButton radio2 = new JRadioButton("to go");
       ButtonGroup group = new ButtonGroup();
       group.add(radio1);
       group.add(radio2);
   
       panel1.setLayout(new GridLayout(2,1));
       panel2.setLayout(new GridLayout(3,1));
   
       panel1.add(radio1);
       panel1.add(radio2);
   
       panel2.add(new JCheckBox("with Drink"));
       panel2.add(new JCheckBox("with Salad"));
       panel2.add(new JCheckBox("with Cake"));
   
       Container container = this.getContentPane();
       container.add(panel1,BorderLayout.WEST);
       container.add(panel2,BorderLayout.EAST);
       container.add(label,BorderLayout.SOUTH);
       
       this.pack();
       this.setVisible(true);
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   public static void main (String args[]) {
       JRadioButtonSample2 jrbs2 = new JRadioButtonSample2();
       jrbs2.initialize();
   }
   
}

ラジオボタンとチェックボックスのイベントに対応する

演習:上の例で、ラジオボタンとチェックボックスがクリックされたら、System Out Printlnでターミナル画面上にhelloと表示するようプログラムしましょう。

ヒント:














(スクロール注意:この先に解答例があります。できるだけ見ないで、自力で考えてください。)


























解答例:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class JRadioButtonSample2 extends JFrame implements ActionListener {

	public void initialize() {
       this.setTitle("Radio Buttons");

       JPanel panel1 = new JPanel();
       JPanel panel2 = new JPanel();
       
       JLabel label = new JLabel("1000 yen");
       label.setHorizontalAlignment(JLabel.CENTER);
       
       JRadioButton radio1 = new JRadioButton("for here");
       JRadioButton radio2 = new JRadioButton("to go");
       ButtonGroup group = new ButtonGroup();
       group.add(radio1);
       group.add(radio2);
   
       panel1.setLayout(new GridLayout(2,1));
       panel2.setLayout(new GridLayout(3,1));
   
       panel1.add(radio1);
       panel1.add(radio2);
       radio1.addActionListener(this);
       radio2.addActionListener(this);
   
       JCheckBox check1 = new JCheckBox("with Drink");
       JCheckBox check2 = new JCheckBox("with Salad"); 
       JCheckBox check3 = new JCheckBox("with Cake");
       panel2.add(check1);
       panel2.add(check2);
       panel2.add(check3);
       check1.addActionListener(this);
       check2.addActionListener(this);
  
       Container container = this.getContentPane();
       container.add(panel1,BorderLayout.WEST);
       container.add(panel2,BorderLayout.EAST);
       container.add(label,BorderLayout.SOUTH);
       
       this.pack();
       this.setVisible(true);
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   public void actionPerformed(ActionEvent e) {
       System.out.println("hello");
   }

   public static void main (String args[]) {
       JRadioButtonSample2 jrbs2 = new JRadioButtonSample2();
       jrbs2.initialize();
   }
   
}

提出課題:軽減税率計算アプリ

以下のプログラムを作って、.javaと.classファイルを出席番号+ローマ字名前のフォルダにコピーして圧縮してください。新年最初の授業(2020年1月9日)の授業開始時間直後に、演習室からいつものように提出してください。

とあるレストランのランチは税別1,000円です。ランチにはオプションがあって、ドリンクを追加すると税別200円、サラダを追加すると税別300円、ケーキを追加すると税別500円です。全部追加すると税別2,000円です。この金額に消費税がかかります。消費税は、お店で食べると10%で、持ち帰りだと8%です。このレストランでアルバイトする友人のために、税込金額を計算するアプリを作ってあげることにしました。以下のように動くよう作ってください。

lunchcalc.png

ヒント:


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2020-11-18 (水) 20:52:03