趣味のIT
自宅サーバーの構築
ApacheとTomcatの連携
Javaのインストール
JavaWebStartの実装
Webプログラミング
JavaScript
Java Applet←
PHP
Java Web Start
エクセルプログラミング
VBAでお絵かき
値を貼り付け
セル参照

Webプログラミング

Applet編

Javaのアプレットはクライアントで実行されるが処理の内容を見られることはない。
しかし、Javaによるコンパイルが必要になる。


【ソースファイル】pcalc_applet.html
<html>
<body topmargin="5" leftMargin="10" marginheight="2" marginwidth="10" bgcolor="#ffffff" text="#000000">
  <table bgcolor="#000000" border="0" cellpadding="0" cellspacing="1" borderwidth="0">
    <tr gbcolor="#ffffff">
      <td gbcolor="#9999ff">
        <applet codebase="." code="pcalc.AppletPcalc.class" name="TestApplet"
            width="200" height="170" hspace="0" vspace="0" align="top">
        </applet>
      </td>
    </tr>
  </table>
</html>
            

【ソースファイル】AppletPcalc.java
package pcalc;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class AppletPcalc extends Applet {
  boolean isStandalone = false;
  TextField textHeight = new TextField();
  TextField textWeight = new TextField();
  Button buttonCalc = new Button();
  Label labelHeight = new Label();
  Label labelWeight = new Label();
  Label labelNormalWeight = new Label();
  Label labelNormalWeightValue = new Label();
  Label labelObesityRate = new Label();
  Label labelObesityRateValue = new Label();
  /**引数値の取得*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**アプレットの構築*/
  public AppletPcalc() {
  }
  /**アプレットの初期化*/
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  /**コンポーネントの初期化*/
  private void jbInit() throws Exception {
    textHeight.setBounds(new Rectangle(115, 65, 65, 20));
    this.setLayout(null);
    textWeight.setBounds(new Rectangle(115, 95, 65, 20));
    buttonCalc.setLabel("計算");
    buttonCalc.setBounds(new Rectangle(115, 125, 65, 25));
    buttonCalc.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        buttonCalc_actionPerformed(e);
      }
    });
    labelNormalWeight.setText("標準体重(kg)");
    labelNormalWeight.setBounds(new Rectangle(10, 5, 65, 20));
    labelObesityRate.setText("肥満度(%)");
    labelObesityRate.setBounds(new Rectangle(10, 35, 65, 20));
    labelNormalWeightValue.setText("-");
    labelNormalWeightValue.setBounds(new Rectangle(115, 5, 65, 20));
    labelObesityRateValue.setText("-");
    labelObesityRateValue.setBounds(new Rectangle(115, 35, 65, 20));
    labelHeight.setText("身長(cm)");
    labelHeight.setBounds(new Rectangle(10, 65, 65, 20));
    labelWeight.setText("体重(kg)");
    labelWeight.setBounds(new Rectangle(10, 95, 65, 20));
    this.add(textHeight, null);
    this.add(textWeight, null);
    this.add(buttonCalc, null);
    this.add(labelHeight, null);
    this.add(labelWeight, null);
    this.add(labelNormalWeight, null);
    this.add(labelNormalWeightValue, null);
    this.add(labelObesityRate, null);
    this.add(labelObesityRateValue, null);
  }
  /**アプレットの開始*/
  public void start() {
  }
  /**アプレットの停止*/
  public void stop() {
  }
  /**アプレットの破棄*/
  public void destroy() {
  }
  /**アプレットの情報取得*/
  public String getAppletInfo() {
    return "Applet Information";
  }
  /**引数情報の取得*/
  public String[][] getParameterInfo() {
    return null;
  }
  /**Main メソッド*/
  public static void main(String[] args) {
    AppletPcalc pcalc = new AppletPcalc();
    pcalc.isStandalone = true;
    Frame frame;
    frame = new Frame() {
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }
      public synchronized void setTitle(String title) {
        super.setTitle(title);
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      }
    };
    frame.setTitle("アプレット フレーム");
    frame.add(pcalc, BorderLayout.CENTER);
    pcalc.init();
    pcalc.start();
    frame.setSize(200,150);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  }

  void buttonCalc_actionPerformed(ActionEvent e) {
    String strHeight, strWeight, strNormalWeight, strObesityRate;
    float height, weight, normalWeight, obesityRate;

    strHeight=textHeight.getText();
    strWeight=textWeight.getText();

    height=Float.parseFloat(strHeight);
    weight=Float.parseFloat(strWeight);

    normalWeight = (height - 100) * (float)0.9;
    obesityRate = (weight - normalWeight) / normalWeight * 100;

    strNormalWeight = Float.toString(normalWeight);
    strObesityRate = Float.toString(obesityRate);

    labelNormalWeightValue.setText(strNormalWeight);
    labelObesityRateValue.setText(strObesityRate);

  }

}
            

Copyright (C) 2006 T.Fujiwara , All rights reserved.