次の演習課題 (1),(2) のプログラムを作成せよ. 課題 (1) ボタン押下時の処理を追加し以下の実行結果となるようにプログラムを作成しなさい ( ボタン押下時の処理 ) import java.lang.*; class Figure extends JFrame implements ActionListener{ JPanel panel; JScrollPane scroll; JTextArea tarea; JButton botn1; JButton botn2; int pos = 0; panel = new JPanel(new FlowLayout(FlowLayout.CENTER)); tarea = new JTextArea(); scroll = new JScrollPane(tarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); botn1 = new JButton(" 表示 "); botn2 = new JButton(" クリア "); panel.add(botn1); panel.add(botn2); // JFrame のコンテントペインの取得 Container con = frame.getcontentpane(); // コンテントペインに JPanel, JScrollPane を配置 con.setlayout(new BorderLayout()); con.add(panel, BorderLayout.SOUTH); con.add(scroll, BorderLayout.CENTER); botn1.addactionlistener(this); botn2.addactionlistener(this); // フレームのセットアップ frame.setbackground(color.white); frame.setsize(320, 240); frame.settitle(title); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); public void actionperformed(actionevent e) { if (botn1 == e.getsource()) { /* ここに処理を追加 */ else if (botn2 == e.getsource()){
tarea.settext(""); pos = 0; public class Kadai1201_01 { static public void main(string[] args) { Figure figure = new Figure(" ボタン処理 "); なお テキストエリアへの文字の書き込みは str で表示文字を作り tarea.append(str); tarea.setcaretposition(tarea.gettext().length()); で実現される ヒント pos= 1; Sting str = pos+"\n"; などとしてどのように表示されるかまず 試してみよ プログラム仕様 : 表示のボタンを押すと 1 回実行 2 回実行 と表示される クリアボタンを押すと表示が消える 再度実行できる ( 表示のボタンを押すと 1 回実行 2 回実行 と表示される ) 解答例.GUI の問題というよりは テキスト表示の問題? となってしまいましたが
import java.lang.*; class Figure extends JFrame implements ActionListener{ JPanel panel; JScrollPane scroll; JTextArea tarea; JButton botn1; JButton botn2; int pos = 0; panel = new JPanel(new FlowLayout(FlowLayout.CENTER)); tarea = new JTextArea(); scroll = new JScrollPane(tarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); botn1 = new JButton(" 表示 "); botn2 = new JButton(" クリア "); panel.add(botn1); panel.add(botn2); // JFrame のコンテントペインの取得 Container con = frame.getcontentpane(); // コンテントペインに JPanel, JScrollPane を配置 con.setlayout(new BorderLayout()); con.add(panel, BorderLayout.SOUTH); con.add(scroll, BorderLayout.CENTER); botn1.addactionlistener(this); botn2.addactionlistener(this); // フレームのセットアップ frame.setbackground(color.white); frame.setsize(320, 240); frame.settitle(title); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true);
public void actionperformed(actionevent e) { if (botn1 == e.getsource()) { // String str = String.format("%d 回実行 %n", ++pos);// 左でもよい pos++; String str = pos+" 回実行 \n"; // pos = 1; // String str = pos+"\n"; tarea.append(str); tarea.setcaretposition(tarea.gettext().length()); else if (botn2 == e.getsource()){ tarea.settext(""); pos = 0; public class Kadai1201_01 { static public void main(string args[]) { Figure figure = new Figure(" ボタン処理 "); (2) 以下の実行結果となるように 2 箇所 処理を追加し プログラムを作成しなさい ( マ ウス処理 ) import java.lang.*; class Figure extends JFrame { frame.add(new DrawPanel());
frame.settitle(title); frame.setdefaultcloseoperation(jframe.exit_on_close);// フレーム閉じるボタン frame.setbackground(color.white);// フレーム背景 frame.setsize(320, 240);// フレーム大きさ frame.setvisible(true);// フレーム表示 class DrawPanel extends JPanel implements MouseListener {// マウスイベント int posh;// 水平位置 int posv;// 垂直位置 public DrawPanel() { setbackground(color.white);// パネル背景 addmouselistener(this);// マウスリスナー public void paintcomponent(graphics g) {// 描画 super.paintcomponent(g); String paint = String.format("(%d, %d)", posh, posv);// 文字表示 g.drawstring(paint, posh, posv);// 文字描画 // コンポーネントの上にマウスカーソルが入ってきたときの処理 public void mouseentered(mouseevent e) { // コンポーネントの上にマウスカーソルが出るときの処理 public void mouseexited(mouseevent e) {// // コンポーネントの上でマウスボタンが押されたときの処理 public void mousepressed(mouseevent e) {// // コンポーネントの上でマウスボタンがクリックされたときの処理 public void mouseclicked(mouseevent e) {// /* 処理追加 */ // コンポーネントの上でマウボタンが離されたされたときの処理 public void mousereleased(mouseevent e) {// public class Kadai1201_02 { public static void main(string[] args) { Figure figure = new Figure(" マウスの座標を表示 ");// インスタンス生成 プログラム仕様 : マウスをクリックすると 座標が表示される
解答例. 講義資料では JFrame で表示するようにしましたが ここでは JPanel でそれを再現したおります やっている事は同じと認識できれるか? を問うこととしました import java.lang.*; class Figure extends JFrame { frame.add(new DrawPanel()); frame.settitle(title); frame.setdefaultcloseoperation(jframe.exit_on_close);// フレーム閉じるボタン frame.setbackground(color.white);// フレーム背景 frame.setsize(320, 240);// フレーム大きさ frame.setvisible(true);// フレーム表示 class DrawPanel extends JPanel implements MouseListener {// マウスイベント int posh;// 水平位置 int posv;// 垂直位置
public DrawPanel() { setbackground(color.white);// パネル背景 addmouselistener(this);// マウスリスナー public void paintcomponent(graphics g) {// 描画 super.paintcomponent(g); String paint = String.format("(%d, %d)", posh, posv);// 文字表示 g.drawstring(paint, posh, posv);// 文字描画 public void mouseentered(mouseevent e) { public void mouseexited(mouseevent e) { public void mousepressed(mouseevent e) { public void mouseclicked(mouseevent e) { posh = e.getx();// 水平位置取得 posv = e.gety();// 垂直位置取得 repaint();// 描画 public void mousereleased(mouseevent e) { public class Kadai1201_02 { public static void main(string[] args) { Figure frame = new Figure(" マウスの座標を表示 ");// インスタンス生成