• 2799閱讀
  • 9回復

呢度有無人識java?help [復制鏈接]

上一主題 下一主題
離線MatthewB
 
發帖
18722
好友元
17
閱讀權限
18922
貢獻值
3
只看樓主 倒序閱讀 使用道具 樓主   發表于: 2013-05-01
as title!! help!
[ 此帖被KaiMatthewBot在2013-05-02 02:14重新編輯 ]
離線真秘密
發帖
12556
好友元
23697
閱讀權限
12557
貢獻值
1
只看該作者 1  發表于: 2013-05-02
試java?
離線MatthewB
發帖
18722
好友元
17
閱讀權限
18922
貢獻值
3
只看該作者 2  發表于: 2013-05-02
識! sor!
離線fatman
發帖
16072
好友元
487
閱讀權限
16076
貢獻值
0
只看該作者 3  發表于: 2013-05-02
你想問咩?
離線MatthewB
發帖
18722
好友元
17
閱讀權限
18922
貢獻值
3
只看該作者 4  發表于: 2013-05-03
我呀sir比左個waveplayer class我(用黎play .wav file)
package textreader;

/**
* CSCI1530 Java Programming Assignment
*
* --- Declaration ---
*
* I declare that the assignment here submitted is original except for source
* material explicitly acknowledged. I also acknowledge that I am aware of
* University policy and regulations on honesty in academic work, and of the
* disciplinary guidelines and procedures applicable to breaches of such policy
* and regulations, as contained in the website
*
*     http://www.cuhk.edu.hk/policy/academichonesty/
*
* Assignment 6
*
* Web Reader
* Created :  3 Apr 2013
* Modified: 23 Apr 2013 (workaround JDK7 bug on Audio and InputStream)
*  
* Implement a Wave Player class.  DO NOT Modify this file!!!
*
*/

import java.io.*;                   // for file opening and stream handling
import java.net.URL;
import javax.sound.sampled.*;       // for WAV stream playing

public class WavePlayer {
    private AudioInputStream wavStream;

    /**
     * Default Constructor does nothing
     */
    public WavePlayer() {
    }

    /**
     * WavePlayer Constructor with a Wave filename
     * @param wavFilename is the filename of a WAVE sound file
     */
    public WavePlayer(String wavFilename) {
        setFilename(wavFilename);
    }

    /**
     * WavePlayer Constructor with a URL source
     * @param wavLink is a WAVE URL sound source
     */
    public WavePlayer(URL wavLink) {
        try {
            wavStream = AudioSystem.getAudioInputStream(wavLink);
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }

    /**
     * WavePlayer Constructor with a Wave InputStream source
     * There is a BUG in JDK7 because the parser requires mark/reset on the InputStream
     * Workaround: use BufferedInputStream or another constructor with a URL
     * @param wavInputStream is a WAVE InputStream sound source
     */
    public WavePlayer(InputStream wavInputStream) {
        try {
            BufferedInputStream bufferedWavInputStream = new BufferedInputStream(wavInputStream);
            wavStream = AudioSystem.getAudioInputStream(bufferedWavInputStream);
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }

    /**
     * Set filename into this WavePlayer object.
     * @param wavFilename is the filename of a WAVE sound file
     */
    public void setFilename(String wavFilename) {
        try {
            File wavFile = new File(wavFilename);
            // Class AudioSystem internally creates and returns some objects for us
            // for example, it creates and handles AudioInputStream object
            //              it also creates and handles Clip object
            // It is called a "object-factory class"
            wavStream = AudioSystem.getAudioInputStream(wavFile);
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }

    /**
     * Make the CPU busy to slow down the program, a delay loop.
     * @param sleepInMS is the CPU sleeping time in milli-second
     */
    private void delay(long sleepInMS) {
        for (long startTime = System.currentTimeMillis();
             System.currentTimeMillis() < startTime + sleepInMS;
             )
        {
            ; // nothing to do
        }
    }

    /**
     * Play a WAV file for 1 second.
     */
    public void play() {
        try {
            Clip wavClip = AudioSystem.getClip();
            wavClip.open(wavStream);
            wavClip.start();
            delay(1500);
        }
        catch (Exception ex) {
            System.err.println(ex);
        }
    }
}

[ 此帖被KaiMatthewBot在2013-05-03 11:49重新編輯 ]
離線MatthewB
發帖
18722
好友元
17
閱讀權限
18922
貢獻值
3
只看該作者 5  發表于: 2013-05-03
A WavePlayer class will be provided for playing a Wave InputStream, sample usage:

URL link;
link = new URL("http://humanum.arts.cuhk.edu.hk/Lexis/lexi-can/sound/ngo5.wav");
WavePlayer p = new WavePlayer( link.openStream() );
p.play();
________________________________________________
1.我唔係好明URL InputStream 同 openstream ();

2.我識過放上面呢4行code 入去但係唔work, 有d咩問題?

3.我依家有個textfield (under JFrame 既GUI)

如果我係textfield度打左d字

之後禁enter, 點樣將我打左既字變成string 同埋 點樣係禁enter之後perform action?

離線智者
發帖
14653
好友元
46535
閱讀權限
37357
貢獻值
2
只看該作者 6  發表于: 2013-05-03
個class名係JTextField

String s = textField.getText();

listener:
http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html

果4行code我得喎.....記住要放響main method
[ 此帖被智者在2013-05-03 12:51重新編輯 ]
  
離線MatthewB
發帖
18722
好友元
17
閱讀權限
18922
貢獻值
3
只看該作者 7  發表于: 2013-05-03
thx

Your Task:
1. Create a new NetBeans project named TextReader, with NO main class, but a new
JFrame Form called TextReader. Put it under a package named textreader.

2. Before you code, draw (drag-and-drop) your GUI design first under the New JFrame Form!

離線MatthewB
發帖
18722
好友元
17
閱讀權限
18922
貢獻值
3
只看該作者 8  發表于: 2013-05-03
My current TextReader.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package textreader;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
*
* @author mbb
*/
public class TextReader extends javax.swing.JFrame {

    /**
     * Creates new form TextReader
     */
    public TextReader() {
        initComponents();
        
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane2 = new javax.swing.JTextPane();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(jTextPane1);

        jScrollPane2.setViewportView(jTextPane2);

        jButton1.setText("Read!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Read!");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Cantonese Reader");

        jLabel2.setText("Type Cantonese syllables here (e.g. ngo5 mun4):");

        jLabel3.setText("Type Cantonese characters here (e.g. 我們):");

        jLabel4.setText("Read a local syllable file:");

        jLabel5.setText("Read a web syllable file:");

        jButton3.setText("Browse...");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(28, 28, 28)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jTextField2)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 666, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 666, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 594, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION)
{
String filename = chooser.getSelectedFile().getAbsolutePath();
jTextField1.setText(filename);
}
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                        

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                          

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TextReader.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TextReader.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TextReader.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TextReader.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TextReader().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextPane jTextPane2;
    // End of variables declaration                  
}
離線MatthewB
發帖
18722
好友元
17
閱讀權限
18922
貢獻值
3
只看該作者 9  發表于: 2013-05-03
個class名係JTextField

String s = textField.getText();

呢個得左thx~~

========================
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
       URL link;
link = new URL("http://humanum.arts.cuhk.edu.hk/Lexis/lexi-can/sound/ngo5.wav");
WavePlayer p = new WavePlayer( link.openStream() );
p.play();

    }                      
========================
呢度唔得....