・ このブログの記事(テキスト・画像)について

ラベル swing の投稿を表示しています。 すべての投稿を表示
ラベル swing の投稿を表示しています。 すべての投稿を表示

2011年12月9日金曜日

【swing】Eclipse3.7の「WindowBuilder 」でSwingでレッツアプリ開発!! その4

前回の続き
プログレスバーを使用してみる。

1.追加ソース
 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {
  frame = new JFrame();
  frame.setBounds(100, 100, 450, 300);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(new BorderLayout(0, 0));
  JButton btnNewButton = new JButton("開始");
  frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);


  progressBar = new JProgressBar(0 , 100);

  frame.getContentPane().add(progressBar, BorderLayout.NORTH);
  btnNewButton.addActionListener(this);
  timer = new Timer(100 , this);
  timer.start();


 }

 /**
  * 汎用String格納用メンバ変数
  * メモリ効率を考えてStringBuilderを使用する。
  * */
 StringBuilder mStringBuilder = new StringBuilder();
 /* (非 Javadoc)
  * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
  */
 public void actionPerformed(ActionEvent e) {
  if(mStringBuilder !=null){
   mStringBuilder.delete(0, mStringBuilder.length());
  }
  mStringBuilder.append(e.getActionCommand());
  //開始のボタンイベントかどうか
  if(mStringBuilder.toString().equals("開始")){
   //警告ダイアログを表示する。
      JLabel label = new JLabel("Message");
      label.setForeground(Color.RED);
      JOptionPane.showMessageDialog(frame, label);



  }else{
   //ActionEventがクリックボタンではない場合,プログレスバーの値変更処理開始
   int value = progressBar.getValue();
   if (value == 100) {
    timer.stop();
    return;
   }else{
    progressBar.setValue(value + 1);
   }
  }
 } 
 
 2.プログレスバーが表示された。
プログレスダイアログが表示された。
 

3.プロジェクトについて
ここのサイトに格納しております。何かのお役に立てたら幸いです。


 

2011年12月8日木曜日

【swing】Eclipse3.7の「WindowBuilder 」でSwingでレッツアプリ開発!! その3

前回の続きです。

前回作成した、Buttonにクリックイベントを追加してみます。
下記がソースです。色を変更しているところが追加したところです。


package jp.co.hanatann.swing_hello;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;

public class Swing_Hello implements ActionListener{
 private JFrame frame;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     Swing_Hello window = new Swing_Hello();
     window.frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the application.
  */
 public Swing_Hello() {
  initialize();
 }

 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {
  frame = new JFrame();
  frame.setBounds(100, 100, 450, 300);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(new BorderLayout(0, 0));


  JButton btnNewButton = new JButton("開始");
  btnNewButton.addActionListener(this);
  frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);

 }

 /* (非 Javadoc)
  * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
  */
 public void actionPerformed(ActionEvent e) {
  //開始のボタンイベントかどうか
  if(e.getActionCommand().equals("開始")){
   //警告ダイアログを表示する。
      JLabel label = new JLabel("Message");
      label.setForeground(Color.RED);
      JOptionPane.showMessageDialog(frame, label);



  }

 }

}

アプリの使用としては、開始ボタンを押して警告ダイアログを表示させる仕様です。

開始ボタン押下

警告ダイアログが表示される。
簡単ですね ^^











2011年12月7日水曜日

【swing】Eclipse3.7の「WindowBuilder 」でSwingでレッツアプリ開発!! その2

前回の続きです。
前回は、swingのベースとなるframeを作成しました。
今回は、ボタンを追加してみました。

1.frameにBorderLayoutを追加する。
これを追加すると、レイアウトが綺麗になるっぽいので追加しますww

frameへBoarderLayoutを追加
 2.Buttonを追加
frameへButtonを追加する。

 3.Buttonのプロパティをいじってみる。
Buttonのプロパティ:textを変更してみる。
 4.アプリを実行してみる。


アプリの下部にButtonが追加されたことが確認できる。



2011年12月6日火曜日

【swing】Eclipse3.7の「WindowBuilder 」でSwingでレッツアプリ開発!! その1

eclipse3.7からWindowBuilderというプラグインが追加されて用です!!!

swingについては、耳にした程度で・・・・具体的にこれで開発したことはありません。

このswingは、
ぱっとみ、マルチスレッド向きでは、無い?って印象です。
シングルスレッド向きのアプリは、作成しやすいのかな?



百聞は一見にしかず!!!
何かアプリを作成してみます。



eclipse3.7のWindowBuilderプラグインはデフォルトでは、無効になっているので、有効にする。




WindowBuilderのプラグインをインストールする。


新規->その他を押下
javaプロジェクトを選択

プロジェクトが生成された
新規->その他->WindowBuilder->Application Windowを選択


パッケージ名と名前を入力する。
swingで必要なフレーム類がプロジェクトへ生成された
プロジェクトをJavaアプリとして起動する。

アプリが起動した。

下記は、生成されたソースの貼り付け

package jp.co.hanatann.swing_hello;

import java.awt.EventQueue;

public class Swing_Hello {

 private JFrame frame;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     Swing_Hello window = new Swing_Hello();
     window.frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the application.
  */
 public Swing_Hello() {
  initialize();
 }

 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {
  frame = new JFrame();
  frame.setBounds(100, 100, 450, 300);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

}


ほほー、
あっという間に作成できました ^^