□"Action01.java"
- import java.awt.event.*;
- public class MyActionListener01 implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- }
- import java.awt.*;
- public class Action01 {
- Frame frame;
- Button button;
- public Action01() {
- frame = new Frame("フレーム");
- button = new Button("ボタン");
- button.addActionListener(new MyActionListener01());
- frame.add(button);
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- Action01 action01 = new Action01();
- }
- }
□ 実行結果
- import java.awt.*;
- import java.awt.event.*;
- public class Action02 {
- Frame frame;
- Button button;
- public Action02() {
- frame = new Frame("フレーム");
- button = new Button("ボタン");
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- frame.add(button);
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- Action02 action02 = new Action02();
- }
- }
□ 実行結果
- import java.awt.*;
- import java.awt.event.*;
- public class Mouse02 {
- Frame frame;
- Mouse02() {
- frame = new Frame("フレーム");
- frame.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- System.out.println(e.getX() + ":" + e.getY());
- }
- } );
- frame.setSize(400, 200);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- Mouse02 mouse02 = new Mouse02();
- }
- }
□ 実行結果
- // 1901
- // package application;
- import java.awt.*;
- import java.awt.event.*;
- // 商品システムGUIクライアント・アプリケーションです。
- public class GUIProduct {
- private Frame frame;
- private Panel northPanel, westPanel, centerPanel, eastPanel, southPanel;
- private Label codeLabel, nameLabel, priceLabel, discountLabel, messageLabel;
- private TextField codeField, nameField, priceField, discountField;
- private TextArea messageArea;
- private Button searchButton, updateButton, deleteButton, resetButton, exitButton;
- private String code;
- private String name;
- private int price;
- private String supplierId;
- private String message;
- public GUIProduct() {
- frame = new Frame("商品情報");
- northPanel = new Panel();
- westPanel = new Panel();
- centerPanel = new Panel();
- eastPanel = new Panel();
- southPanel = new Panel();
- westPanel.setLayout(new GridLayout(0, 1));
- centerPanel.setLayout(new GridLayout(0, 1));
- searchButton = new Button("検索");
- searchButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("clecked search button");
- System.out.println(" product code: " + codeField.getText());
- messageArea.setText("clecked search button"
- + System.getProperty("line.separator")
- + " product code: " + codeField.getText()
- + System.getProperty("line.separator")
- );
- }
- } );
- northPanel.add(searchButton);
- updateButton = new Button("新規/更新");
- updateButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println(" product code: " + codeField.getText());
- System.out.println(" product name: " + nameField.getText());
- System.out.println(" price: " + Integer.parseInt(priceField.getText()));
- System.out.println(" message: " + messageArea.getText());
- System.out.println();
- }
- } );
- northPanel.add(updateButton);
- deleteButton = new Button("削除");
- deleteButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("clecked delete button");
- System.out.println(" product code: " + codeField.getText());
- System.out.println();
- }
- } );
- northPanel.add(deleteButton);
- resetButton = new Button("取消");
- resetButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- codeField.setText("");
- nameField.setText("");
- priceField.setText("");
- messageArea.setText("");
- System.out.println("clecked reset button");
- System.out.println();
- }
- } );
- northPanel.add(resetButton);
- exitButton = new Button("終了");
- exitButton.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- }
- );
- northPanel.add(exitButton);
- codeLabel = new Label("商品コード:");
- codeField = new TextField(8);
- westPanel.add(codeLabel);
- centerPanel.add(codeField);
- nameLabel = new Label("商品名:");
- nameField = new TextField(30);
- westPanel.add(nameLabel);
- centerPanel.add(nameField);
- priceLabel = new Label("定価:");
- priceField = new TextField(8);
- westPanel.add(priceLabel);
- centerPanel.add(priceField);
- discountLabel = new Label("割引率:");
- discountField = new TextField(8);
- westPanel.add(discountLabel);
- centerPanel.add(discountField);
- messageLabel = new Label("メッセージ:");
- messageArea = new TextArea();
- southPanel.add(messageArea);
- frame.add(northPanel, BorderLayout.NORTH);
- frame.add(westPanel, BorderLayout.WEST);
- frame.add(centerPanel, BorderLayout.CENTER);
- frame.add(eastPanel, BorderLayout.EAST);
- frame.add(southPanel, BorderLayout.SOUTH);
- frame.setSize(480, 320);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- GUIProduct gp = new GUIProduct();
- }
- }
□ 実行結果
- // 1902
- // package application;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- // 商品システムGUIクライアント・アプリケーションです。
- public class GUIProduct {
- private static final int SERVER_PORT = 54321;
- private static String serverName = null;
- private static Socket sock = null;
- private Frame frame;
- private Panel northPanel, westPanel, centerPanel, eastPanel, southPanel;
- private Label codeLabel, nameLabel, priceLabel, discountLabel, messageLabel;
- private TextField codeField, nameField, priceField, discountField;
- private TextArea messageArea;
- private Button searchButton, updateButton, deleteButton, resetButton, exitButton;
- private String code;
- private String name;
- private int price;
- private String message;
- public GUIProduct() {
- frame = new Frame("商品情報");
- northPanel = new Panel();
- westPanel = new Panel();
- centerPanel = new Panel();
- eastPanel = new Panel();
- southPanel = new Panel();
- westPanel.setLayout(new GridLayout(0, 1));
- centerPanel.setLayout(new GridLayout(0, 1));
- searchButton = new Button("検索");
- searchButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("clecked search button");
- System.out.println(" product code: " + codeField.getText());
- messageArea.setText("clecked search button"
- + System.getProperty("line.separator")
- + " product code: " + codeField.getText()
- + System.getProperty("line.separator")
- );
- query(codeField.getText());
- }
- } );
- northPanel.add(searchButton);
- updateButton = new Button("新規/更新");
- updateButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println(" product code: " + codeField.getText());
- System.out.println(" product name: " + nameField.getText());
- System.out.println(" price: " + Integer.parseInt(priceField.getText()));
- System.out.println(" message: " + messageArea.getText());
- System.out.println();
- }
- } );
- northPanel.add(updateButton);
- deleteButton = new Button("削除");
- deleteButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("clecked delete button");
- System.out.println(" product code: " + codeField.getText());
- System.out.println();
- }
- } );
- northPanel.add(deleteButton);
- resetButton = new Button("取消");
- resetButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- codeField.setText("");
- nameField.setText("");
- priceField.setText("");
- messageArea.setText("");
- System.out.println("clecked reset button");
- System.out.println();
- }
- } );
- northPanel.add(resetButton);
- exitButton = new Button("終了");
- exitButton.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- }
- );
- northPanel.add(exitButton);
- codeLabel = new Label("商品コード:");
- codeField = new TextField(8);
- westPanel.add(codeLabel);
- centerPanel.add(codeField);
- nameLabel = new Label("商品名:");
- nameField = new TextField(30);
- westPanel.add(nameLabel);
- centerPanel.add(nameField);
- priceLabel = new Label("定価:");
- priceField = new TextField(8);
- westPanel.add(priceLabel);
- centerPanel.add(priceField);
- discountLabel = new Label("割引率:");
- discountField = new TextField(8);
- westPanel.add(discountLabel);
- centerPanel.add(discountField);
- messageLabel = new Label("メッセージ:");
- messageArea = new TextArea();
- southPanel.add(messageArea);
- frame.add(northPanel, BorderLayout.NORTH);
- frame.add(westPanel, BorderLayout.WEST);
- frame.add(centerPanel, BorderLayout.CENTER);
- frame.add(eastPanel, BorderLayout.EAST);
- frame.add(southPanel, BorderLayout.SOUTH);
- frame.setSize(480, 320);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- serverName = args[0];
- GUIProduct gp = new GUIProduct();
- }
- void query(String code) {
- try {
- // クライアントと対話処理するストリーム、ソケットを生成
- sock = new Socket(serverName, SERVER_PORT);
- DataInputStream dis =
- new DataInputStream(sock.getInputStream());
- DataOutputStream dos =
- new DataOutputStream(sock.getOutputStream());
- dos.writeUTF(code);
- String str = dis.readUTF();
- System.out.println("商品コード " + code + " の商品は " + str + " です。");
- if (str.equals("[Not Found]")) {
- return;
- }
- String prods[] = str.substring(1, str.length()-1).split(",");
- nameField.setText(prods[1].trim());
- priceField.setText(prods[2].trim());
- // ストリーム、ソケットをクローズ
- dos.close();
- dis.close();
- sock.close();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- sock.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
□ 実行結果
□ 実行結果
- import java.awt.*;
- import java.awt.event.*;
- public class ClickCircle01 extends Canvas {
- Frame frame;
- private int x = 0, y = 0;
- private int w = 10, h = w;
- ClickCircle01(){
- // ここにコードを記述してください。
- addMouseListener(ml);
- setSize(300, 200);
- setBackground(Color.WHITE);
- frame = new Frame("クリックしてください");
- frame.add(this);
- frame.pack();
- frame.setVisible(true);
- }
- public void paint(Graphics g){
- // ここにコードを記述してください。
- }
- public static void main(String[] args){
- ClickCircle01 d = new ClickCircle01();
- }
- }
□ 実行結果
- import java.awt.*;
- import java.awt.event.*;
- public class DragDraw01 extends Frame {
- private int x = 0, y = 0;
- private int w = 5, h = 5;
- DragDraw01(String s){
- super(s);
- addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
- addMouseMotionListener(new MouseMotionAdapter(){
- public void mouseDragged(MouseEvent e){
- // ここにコードを記述してください。
- }
- });
- }
- public void update(Graphics g){
- // ここにコードを記述してください。
- }
- public void paint(Graphics g){
- // ここにコードを記述してください。
- }
- public static void main(String[] args){
- DragDraw01 d = new DragDraw01("ドラッグしてください");
- d.setSize(600, 400);
- d.setBackground(Color.white);
- d.setVisible(true);
- }
- }
□ 実行結果