diablo...aka...boss
23-03-2009, 17:06
Nella foto sotto, a sx c'è un applet che ho creato indipendente e a dx un frame che ho creato sempre in modo indipendente uno dall' altro.
http://img91.imageshack.us/img91/522/95206690.jpg (http://img91.imageshack.us/my.php?image=95206690.jpg)
Quello che volevo fare era vedere all' interno della finestra che genera il frame "titolo canzone" e i tasti play ecc, come nell' applet.
Il codice non genera errore però non riesco nell' intento. Secondo me sbaglio qualcosa nel comunicare cosa voglio inserire all' interno della finesta che genera il frame. Altrimenti non so spiegarmelo.
Dove sbaglio??
di seguito inserisco il codice:
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class NewFrame extends JFrame {
public NewFrame (String titolo, int posX, int posY, int width, int height) {
// titolo frame
this.setTitle(titolo);
// posizione frame
this.setLocation(posX, posY);
// dimensione frame
this.setSize(width, height);
// gestione chiusura
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
// creazione istanza
NewFrame myFrame =
new NewFrame("Titolo del frame", 20, 20, 300, 200);
// visualizzazione
myFrame.setVisible(true);
}
public class Audio extends JApplet implements ActionListener
{
private AudioClip clip;
private JButton play, stop, loop, ok;
private JPanel buttonPanel;
public void paint(Graphics g) {
g.drawString("titolo canzone", 20, 30);
}
public void init()
{
// se non mettessi il try catch l' audio non si sentirebbe
try
{
clip = getAudioClip(new URL( getDocumentBase(), "one for you, one for me.wav"));
}
catch(MalformedURLException muEx)
{
System.out.println("*** Invalid URL! ***");
System.exit(1);
}
play = new JButton("Play");
play.addActionListener(this);
stop = new JButton("Stop");
stop.addActionListener(this);
loop = new JButton("Loop");
loop.addActionListener(this);
ok = new JButton("ok");
ok.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.add(play);
buttonPanel.add(stop);
buttonPanel.add(loop);
buttonPanel.add(ok);
add(buttonPanel,BorderLayout.SOUTH);
}
public void stop()
{
clip.stop(); //Prevents sound from continuing
} //after applet has been stopped.
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == play)
clip.play();
if (event.getSource() == stop)
clip.stop();
if (event.getSource() == loop)
clip.loop();
}
}
}
Grazie.
http://img91.imageshack.us/img91/522/95206690.jpg (http://img91.imageshack.us/my.php?image=95206690.jpg)
Quello che volevo fare era vedere all' interno della finestra che genera il frame "titolo canzone" e i tasti play ecc, come nell' applet.
Il codice non genera errore però non riesco nell' intento. Secondo me sbaglio qualcosa nel comunicare cosa voglio inserire all' interno della finesta che genera il frame. Altrimenti non so spiegarmelo.
Dove sbaglio??
di seguito inserisco il codice:
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class NewFrame extends JFrame {
public NewFrame (String titolo, int posX, int posY, int width, int height) {
// titolo frame
this.setTitle(titolo);
// posizione frame
this.setLocation(posX, posY);
// dimensione frame
this.setSize(width, height);
// gestione chiusura
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
// creazione istanza
NewFrame myFrame =
new NewFrame("Titolo del frame", 20, 20, 300, 200);
// visualizzazione
myFrame.setVisible(true);
}
public class Audio extends JApplet implements ActionListener
{
private AudioClip clip;
private JButton play, stop, loop, ok;
private JPanel buttonPanel;
public void paint(Graphics g) {
g.drawString("titolo canzone", 20, 30);
}
public void init()
{
// se non mettessi il try catch l' audio non si sentirebbe
try
{
clip = getAudioClip(new URL( getDocumentBase(), "one for you, one for me.wav"));
}
catch(MalformedURLException muEx)
{
System.out.println("*** Invalid URL! ***");
System.exit(1);
}
play = new JButton("Play");
play.addActionListener(this);
stop = new JButton("Stop");
stop.addActionListener(this);
loop = new JButton("Loop");
loop.addActionListener(this);
ok = new JButton("ok");
ok.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.add(play);
buttonPanel.add(stop);
buttonPanel.add(loop);
buttonPanel.add(ok);
add(buttonPanel,BorderLayout.SOUTH);
}
public void stop()
{
clip.stop(); //Prevents sound from continuing
} //after applet has been stopped.
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == play)
clip.play();
if (event.getSource() == stop)
clip.stop();
if (event.getSource() == loop)
clip.loop();
}
}
}
Grazie.