GUIフォーム上に多数のボタンを配置し,処理を割り当てるサンプル。
//ボタンを多数配置し,それぞれのボタンに処理を割り当てるサンプル。
//この例では,3つのボタンA,B,Cにのみ,音を発生させる処理を
//割り当てている。他のボタンはクリックしても何も起こらない。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.sound.sampled.*;
public class SoundPlay extends javax.swing.JFrame {
/** Creates new form SoundPlay */
public SoundPlay() {
initComponents();
this.setSize(480,360);
this.setTitle("Sound Play");
moveCenter();
}
//ウィンドウを画面中央に表示する。
private void moveCenter(){
Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
setLocation( (dim.width-getSize().width)/2,(dim.height-getSize().height)/2);
}
//引数をファイル名としてサウンドファイルを開き,再生する。
public void SoundGenerate(String filePath){
AudioFormat format = null;
DataLine.Info info = null;
Clip line = null;
File audioFile = null;
try{
audioFile = new File(filePath); //"c:\\windows\\media\\tada.wav"
format = AudioSystem.getAudioFileFormat(audioFile).getFormat();
info = new DataLine.Info(Clip.class, format);
line = (Clip)AudioSystem.getLine(info);
line.open(AudioSystem.getAudioInputStream(audioFile));
line.start();
}
catch(Exception e){
e.printStackTrace();
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
jTextField1 = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton18 = new javax.swing.JButton();
jButton19 = new javax.swing.JButton();
jButton20 = new javax.swing.JButton();
jButton21 = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();
jButton23 = new javax.swing.JButton();
jButton24 = new javax.swing.JButton();
jButton25 = new javax.swing.JButton();
jButton26 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jButton27 = new javax.swing.JButton();
jButton28 = new javax.swing.JButton();
jButton29 = new javax.swing.JButton();
jButton30 = new javax.swing.JButton();
getContentPane().setLayout(null);
setTitle("SoundPlay");
setFont(new java.awt.Font("Arial", 0, 10));
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jTextField1.setText("jTextField1");
getContentPane().add(jTextField1);
jTextField1.setBounds(30, 40, 390, 18);
jPanel1.setBackground(new java.awt.Color(255, 255, 153));
jButton1.setText("A");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1);
jButton2.setText("B");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jPanel1.add(jButton2);
jButton3.setText("C");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jPanel1.add(jButton3);
jButton4.setText("D");
jPanel1.add(jButton4);
jButton5.setText("E");
jPanel1.add(jButton5);
jButton6.setText("F");
jPanel1.add(jButton6);
jButton7.setText("G");
jPanel1.add(jButton7);
jButton8.setText("H");
jPanel1.add(jButton8);
jButton9.setText("I");
jPanel1.add(jButton9);
jButton10.setText("J");
jPanel1.add(jButton10);
jButton11.setLabel("K");
jPanel1.add(jButton11);
jButton12.setLabel("L");
jPanel1.add(jButton12);
jButton13.setLabel("M");
jPanel1.add(jButton13);
jButton14.setLabel("N");
jPanel1.add(jButton14);
jButton15.setLabel("O");
jPanel1.add(jButton15);
jButton16.setLabel("P");
jPanel1.add(jButton16);
jButton17.setLabel("Q");
jPanel1.add(jButton17);
jButton18.setLabel("R");
jPanel1.add(jButton18);
jButton19.setLabel("S");
jPanel1.add(jButton19);
jButton20.setLabel("T");
jPanel1.add(jButton20);
jButton21.setText("U");
jPanel1.add(jButton21);
jButton22.setText("V");
jPanel1.add(jButton22);
jButton23.setText("W");
jPanel1.add(jButton23);
jButton24.setText("X");
jPanel1.add(jButton24);
jButton25.setText("Y");
jPanel1.add(jButton25);
jButton26.setText("Z");
jPanel1.add(jButton26);
getContentPane().add(jPanel1);
jPanel1.setBounds(50, 80, 360, 140);
jPanel2.setBackground(new java.awt.Color(204, 255, 204));
jButton27.setText("+");
jPanel2.add(jButton27);
jButton28.setText("-");
jPanel2.add(jButton28);
jButton29.setText("*");
jPanel2.add(jButton29);
jButton30.setText("/");
jPanel2.add(jButton30);
getContentPane().add(jPanel2);
jPanel2.setBounds(100, 240, 250, 70);
pack();
}//GEN-END:initComponents
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// ボタンCが押されたときの処理コード:
jTextField1.setText("c:\\windows\\media\\notify.wav");
SoundGenerate("c:\\windows\\media\\notify.wav");
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// ボタンBが押されたときの処理コード:
jTextField1.setText("c:\\windows\\media\\chimes.wav");
SoundGenerate("c:\\windows\\media\\chimes.wav");
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
//ボタンAが押されたときの処理コード:
jTextField1.setText("c:\\windows\\media\\tada.wav");
SoundGenerate("c:\\windows\\media\\tada.wav");
}//GEN-LAST:event_jButton1ActionPerformed
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new SoundPlay().show();
}
// 変数宣言 - 編集不可//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
private javax.swing.JButton jButton19;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton20;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton23;
private javax.swing.JButton jButton24;
private javax.swing.JButton jButton25;
private javax.swing.JButton jButton26;
private javax.swing.JButton jButton27;
private javax.swing.JButton jButton28;
private javax.swing.JButton jButton29;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton30;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextField1;
// 変数宣言の終わり//GEN-END:variables
}
▼研究演習ホームに戻る
▼小松原研究室公開ページ
▼経営学科詳細紹介ページ
|
|