- GUIプログラムを作成するために、Swingを利用することができる
- Swingのウィンドウ部品を、コンポーネントと呼びます
- コンポーネントを含めることができるコンポーネントを、コンテナと呼ぶ
- ユーザーが行う操作などは、イベントとして処理される
- リスナをあらかじめソースに登録しておくと、イベントが発生したときに、ソースからリスナにイベントが渡されて処理される
Last active
December 28, 2015 04:29
-
-
Save shigemk2/7442185 to your computer and use it in GitHub Desktop.
やさしいJava 活用編 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>サンプル</title> | |
</head> | |
<body> | |
<applet code = "Sample1.class" width = 200 height = 200> | |
</applet> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Swingと呼ばれるクラスの集まりを使うと、ボタンやアイコン、メニューを取り入れた | |
// グラフィカルなプログラムを作成できる | |
import javax.swing.*; | |
// 1. ラベルオブジェクトを作成する | |
// 2. アプレットのadd()メソッドを呼び出してコンテナ(コンテンツペイン)にコンポーネント(ラベル)を追加する | |
public class Sample1 extends JApplet | |
{ | |
private JLabel lb; | |
public void init() | |
{ | |
// コンポーネントの作成 | |
lb = new JLabel(); | |
// コンポーネントの設定 | |
lb.setText("いらっしゃいませ"); | |
// コンテナへ追加 | |
add(lb); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- アプレットを実行する場合には、かならずHTML文書が必要となる --> | |
<html> | |
<head> | |
<title>サンプル</title> | |
</head> | |
<body> | |
<applet code = "Sample2.class" width = 200 height = 200> | |
</applet> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Swingと呼ばれるクラスの集まりを使うと、ボタンやアイコン、メニューを取り入れた | |
// グラフィカルなプログラムを作成できる | |
import javax.swing.*; | |
public class Sample2 extends JApplet | |
{ | |
private JButton bt; | |
public void init() | |
{ | |
// コンポーネントの作成 | |
bt = new JButton("購入"); | |
// コンテナへ追加 | |
add(bt); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>サンプル</title> | |
</head> | |
<body> | |
<applet code = "Sample3.class" width = 200 height = 200> | |
</applet> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import javax.swing.*; | |
public class Sample3 extends JApplet | |
{ | |
private JLabel lb; | |
private JButton bt; | |
public void init() | |
{ | |
// コンポーネントの作成 | |
lb = new JLabel("いらっしゃいませ"); | |
bt = new JButton("購入"); | |
// コンテナへ追加 | |
// 上にボタンを配置する | |
add(lb, BorderLayout.NORTH); | |
// 下にボタンを配置する | |
add(bt, BorderLayout.SOUTH); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>サンプル</title> | |
</head> | |
<body> | |
<applet code = "Sample4.class" width = 200 height = 200> | |
</applet> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. リスナを宣言 | |
// 2. ソースにリスナを登録する | |
// →イベントがおきたときにリスナでイベントの処理が行われる | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class Sample4 extends JApplet | |
{ | |
private JLabel lb; | |
private JButton bt; | |
public void init() | |
{ | |
// コンポーネントの作成 | |
lb = new JLabel("いらっしゃいませ"); | |
bt = new JButton("購入"); | |
// コンテナへ追加 | |
// 上にボタンを配置する | |
add(lb, BorderLayout.NORTH); | |
// 下にボタンを配置する | |
add(bt, BorderLayout.SOUTH); | |
// リスナの登録 | |
bt.addActionListener(new SampleActionListener()); | |
} | |
class SampleActionListener implements ActionListener | |
{ | |
public void actionPerformed(ActionEvent e) | |
{ | |
lb.setText("ご購入ありがとうございます"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>サンプル</title> | |
</head> | |
<body> | |
<applet code = "Sample5.class" width = 200 height = 200> | |
</applet> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// マウスをクリックしたときに処理をする | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class Sample5 extends JApplet | |
{ | |
private JLabel lb; | |
private JButton bt; | |
public void init() | |
{ | |
// コンポーネントの作成 | |
lb = new JLabel("いらっしゃいませ"); | |
// コンテナへ追加 | |
add(lb, BorderLayout.NORTH); | |
// リスナの登録 | |
addMouseListener(new SampleMouseListener()); | |
} | |
class SampleMouseListener extends MouseAdapter | |
{ | |
public void mouseClicked(MouseEvent e) | |
{ | |
lb.setText("ご購入ありがとうございます"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>サンプル</title> | |
</head> | |
<body> | |
<applet code = "Sample6.class" width = 200 height = 200> | |
</applet> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// マウス処理 | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class Sample6 extends JApplet | |
{ | |
private JLabel lb1, lb2; | |
public void init() | |
{ | |
// コンポーネントの作成 | |
lb = new JLabel("ようこそ"); | |
// コンテナへ追加 | |
add(lb, BorderLayout.NORTH); | |
// リスナの登録 | |
addMouseListener(new SampleMouseListener()); | |
} | |
class SampleMouseListener extends MouseAdapter | |
{ | |
public void mouseEntered(MouseEvent e) | |
{ | |
lb.setText("いらっしゃいませ"); | |
} | |
public void mouseExited(MouseEvent e) | |
{ | |
lb.setText("ようこそ"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>サンプル</title> | |
</head> | |
<body> | |
<applet code = "Sample7.class" width = 200 height = 200> | |
</applet> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// キーボード処理 | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class Sample7 extends JApplet | |
{ | |
private JLabel lb1, lb2; | |
public void init() | |
{ | |
// コンポーネントの作成 | |
lb1 = new JLabel("矢印キーでお選びください"); | |
lb2 = new JLabel(); | |
// コンテナへ追加 | |
add(lb1, BorderLayout.NORTH); | |
add(lb2, BorderLayout.SOUTH); | |
// リスナの登録 | |
addKeyListener(new SampleKeyListener()); | |
setFocusable(true); | |
} | |
// リスナクラス | |
class SampleKeyListener extends KeyAdapter | |
{ | |
public void keyPressed(KeyEvent e) | |
{ | |
String str; | |
int k = e.getKeyCode(); | |
switch(k) { | |
case KeyEvent.VK_UP: | |
str = "上"; break; | |
case KeyEvent.VK_DOWN: | |
str = "下"; break; | |
case KeyEvent.VK_LEFT: | |
str = "左"; break; | |
case KeyEvent.VK_RIGHT: | |
str = "右"; break; | |
default: | |
str = "他のキー"; | |
} | |
lb2.setText(str+"ですね"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>サンプル</title> | |
</head> | |
<body> | |
<applet code = "SampleP1.class" width = 200 height = 200> | |
</applet> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. リスナを宣言 | |
// 2. ソースにリスナを登録する | |
// →イベントがおきたときにリスナでイベントの処理が行われる | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class SampleP1 extends JApplet | |
{ | |
private JLabel lb; | |
private JButton bt; | |
public void init() | |
{ | |
// コンポーネントの作成 | |
lb = new JLabel("いらっしゃいませ"); | |
bt = new JButton("購入"); | |
// コンテナへ追加 | |
// 上にボタンを配置する | |
add(lb, BorderLayout.NORTH); | |
// 下にボタンを配置する | |
add(bt, BorderLayout.SOUTH); | |
// リスナの登録 | |
bt.addActionListener(new SampleActionListener()); | |
} | |
class SampleActionListener implements ActionListener | |
{ | |
public void actionPerformed(ActionEvent e) | |
{ | |
bt.setText("Thanks!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>サンプル</title> | |
</head> | |
<body> | |
<applet code = "SampleP2.class" width = 200 height = 200> | |
</applet> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. リスナを宣言 | |
// 2. ソースにリスナを登録する | |
// →イベントがおきたときにリスナでイベントの処理が行われる | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class SampleP2 extends JApplet | |
{ | |
private JButton bt; | |
public void init() | |
{ | |
// コンポーネントの作成 | |
bt = new JButton("ようこそ"); | |
// コンテナへ追加 | |
// 上にボタンを配置する | |
add(bt, BorderLayout.NORTH); | |
// リスナの登録 | |
bt.addMouseListener(new SampleMouseListener()); | |
} | |
class SampleMouseListener extends MouseAdapter | |
{ | |
public void mouseEntered(MouseEvent e) | |
{ | |
bt.setText("いらっしゃいませ"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>サンプル</title> | |
</head> | |
<body> | |
<applet code = "SampleP3.class" width = 200 height = 200> | |
</applet> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// キーボード処理 | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class SampleP3 extends JApplet | |
{ | |
private JLabel lb1, lb2; | |
public void init() | |
{ | |
// コンポーネントの作成 | |
lb1 = new JLabel("矢印キーでお選びください"); | |
lb2 = new JLabel(); | |
// コンテナへ追加 | |
add(lb1, BorderLayout.NORTH); | |
add(lb2, BorderLayout.SOUTH); | |
// リスナの登録 | |
addKeyListener(new SampleKeyListener()); | |
setFocusable(true); | |
} | |
// リスナクラス | |
class SampleKeyListener extends KeyAdapter | |
{ | |
public void keyPressed(KeyEvent e) | |
{ | |
char ch; | |
ch = e.getKeyChar(); | |
lb2.setText(ch+"ですね"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment