Java Gridbag Layout Not Working
Hi,
I have a java sound applet that works, with buttons and icons and combo boxes. I have just added a label and I want the label to appear beneath the buttons and combo boxes, so I decided that Gribag is the best way of achieving this. However, I have set all the co-ordinates to the right places, and suddenly, everything is in the center in one line. Here is the code:
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
publicclass SoundAppletextends JApplet
implements ActionListener,
ItemListener{
AppletSoundList soundList;
String auFile ="spacemusic.au";
String aiffFile ="flute+hrn+mrmba.aif";
String midiFile ="trippygaia1.mid";
String wavFile ="monkwebstune.wav";
String chosenFile;
AudioClip onceClip, loopClip;
JComboBox formats;
JButton playButton, loopButton, stopButton;
boolean looping =false;
publicvoid init(){
String [] fileTypes ={auFile,
aiffFile,
midiFile,
wavFile,};
Container contentArea = getContentPane();
contentArea.setBackground(Color.black);
GridBagLayout flowManager =new GridBagLayout();
GridBagConstraints pos =new GridBagConstraints();
contentArea.setLayout (flowManager);
formats =new JComboBox(fileTypes);
formats.setSelectedIndex(0);
chosenFile = (String)formats.getSelectedItem();
formats.addItemListener(this);
formats.setBackground(Color.blue);
formats.setForeground(Color.white);
pos.gridx = 1; pos.gridy = 1;
contentArea.add(formats, pos);
Image playImage = getImage( getCodeBase () ,"play.gif" );
ImageIcon playIcon =new ImageIcon( playImage );
playButton =new JButton("Play", playIcon);
playButton.addActionListener(this);
playButton.setBackground(Color.green);
playButton.setForeground(Color.black);
pos.gridx = 1; pos.gridy = 1;
contentArea.add(playButton, pos);
Image loopImage = getImage( getCodeBase () ,"loop.gif" );
ImageIcon loopIcon =new ImageIcon( loopImage );
loopButton =new JButton("Loop", loopIcon);
loopButton.addActionListener(this);
loopButton.setBackground(Color.yellow);
loopButton.setForeground(Color.black);
pos.gridx = 2; pos.gridy = 1;
contentArea.add(loopButton, pos);
Image stopImage = getImage( getCodeBase () ,"stop.gif" );
ImageIcon stopIcon =new ImageIcon( stopImage );
stopButton =new JButton("Stop", stopIcon);
stopButton.addActionListener(this);
stopButton.setEnabled(false);
stopButton.setBackground(Color.red);
stopButton.setForeground(Color.black);
pos.gridx = 3; pos.gridy = 1;
contentArea.add(stopButton, pos);
JLabel Occult =new JLabel ("Occult");
pos.gridx = 2; pos.gridy = 3;
contentArea.add(Occult, pos);
setContentPane(contentArea);
JPanel controlPanel =new JPanel();
controlPanel.add(formats);
controlPanel.add(playButton);
controlPanel.add(loopButton);
controlPanel.add(stopButton);
controlPanel.add(Occult);
getContentPane().add(controlPanel);
controlPanel.setBackground(Color.black);
setContentPane(contentArea);
startLoadingSounds();
}
publicvoid itemStateChanged(ItemEvent e){
chosenFile = (String)formats.getSelectedItem();
soundList.startLoading(chosenFile);
}
void startLoadingSounds(){
//Start asynchronous sound loading.
soundList =new AppletSoundList(this, getCodeBase());
soundList.startLoading(auFile);
soundList.startLoading(aiffFile);
soundList.startLoading(midiFile);
soundList.startLoading(wavFile);
}
publicvoid stop(){
onceClip.stop();//Cut short the one-time sound.
if (looping){
loopClip.stop();//Stop the sound loop.
}
}
publicvoid start(){
if (looping){
loopClip.loop();//Restart the sound loop.
}
}
publicvoid actionPerformed(ActionEvent event){
//PLAY BUTTON
Object source = event.getSource();
if (source == playButton){
//Try to get the AudioClip.
onceClip = soundList.getClip(chosenFile);
onceClip.play();//Play it once.
stopButton.setEnabled(true);
showStatus("Playing sound " + chosenFile +".");
if (onceClip ==null){
showStatus("Sound " + chosenFile +" not loaded yet.");
}
return;
}
//START LOOP BUTTON
if (source == loopButton){
loopClip = soundList.getClip(chosenFile);
looping =true;
loopClip.loop();//Start the sound loop.
loopButton.setEnabled(false);//Disable loop button.
stopButton.setEnabled(true);
showStatus("Playing sound " + chosenFile +" continuously.");
if (loopClip ==null){
showStatus("Sound " + chosenFile +" not loaded yet.");
}
return;
}
//STOP LOOP BUTTON
if (source == stopButton){
if (looping){
looping =false;
loopClip.stop();//Stop the sound loop.
loopButton.setEnabled(true);//Enable start button.
}
elseif (onceClip !=null){
onceClip.stop();
}
stopButton.setEnabled(false);
showStatus("Stopped playing " + chosenFile +".");
return;
}
}
}
How can I get everything to follow the co-ordinates I specified?
Thanks in advance.

