Hi, I am having a terrible time getting to grips with layout managers, and am hoping someone can shed some light onto my misery.My problem is this:
I have created a simple GUI, that contains a button panel, a picture panel and a radio button panel.All these panels are put into one Container called mainPanel,which has a BorderLayout and this is set as the content pane.
This is what I would like my panel to look like:
[IMG]http://img831.imageshack.us/img831/2415/examplea.jpg[/IMG]
Uploaded with ImageShack.us
The button panel and picture panel work fine, the problem arises with the radio buttons having their own panel, underneath the pictures.
When I try to set them to BorderLayout.SOUTH, it does not work(this area is already occupied by the button panel, but i thought maybe I could place it ontop of it.)
I also tried to have my picture panel as a borderLayout, and set the pictures in the center and radio panel in the south, but this too gets rid of the picute display.
Here is my code, and help is much appreciated.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;
public class GUI extends JFrame implements ActionListener {
private Container mainPanel;
private JPanel buttonPanel;
private JPanel picturePanel;
private JPanel radioButtonPanel;
private JButton showPicture;
// Constructor for the GUI.
GUI(){
setTitle("Simple Picture Printout");
setSize(649,480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = createPane();
setContentPane(mainPanel);
setVisible(true);
}
private Container createPane() {
Container contentPane = new Container();
contentPane.setLayout(new BorderLayout());
contentPane.setBackground(Color.BLUE);
picturePanel = createPicturePanel();
buttonPanel = createButtonPanel();
radioButtonPanel = createRadioButtonPanel();
contentPane.add(picturePanel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
// This does not work!
contentPane.add(radioButtonPanel, BorderLayout.SOUTH);
return contentPane;
}
public JPanel createButtonPanel() {
// Create pane, set layout & color.
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.CENTER));
pane.setBackground(Color.BLUE);
// Add descriptions to buttons.
showPicture = new JButton("Show Pictures");
// Add actionListener functionality to deal button.
deal.addActionListener(this);
// Add buttons to pane.
pane.add(deal);
// return pane into buttonPanel object.
return pane;
}
public JPanel createPicturePanel() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout());
pane.setBackground(Color.BLUE);
return pane;
}
// Creates a radio button panel for choosing which picture you would like to keep.
public JPanel createRadioButtonPanel() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout());
pane.setBackground(Color.BLUE);
JRadioButton b1 = new JRadioButton();
JRadioButton b2 = new JRadioButton();
JRadioButton b3 = new JRadioButton();
pane.add(b1);
pane.add(b2);
pane.add(b3);
return pane;
}
// The actionPerformed method.
public void actionPerformed(ActionEvent e){
// Functionality of when showPicture button is pressed.
if(e.getSource() == showPicture) {
// Clear the card panel.
clearPicturePanel();
String pictureSource[] = new String[4]
// This is the code for setting the card image to the panel
// can probably be made shorter with an array and loop.
String imgsrc1 = pictureSource[0];
String imgsrc2 = pictureSource[1];
String imgsrc3 = pictureSource[2];
String imgsrc4 = pictureSource[3];
ImageIcon picIcon1 = new ImageIcon(imgsrc1);
ImageIcon picIcon2 = new ImageIcon(imgsrc2);
ImageIcon picIcon3 = new ImageIcon(imgsrc3);
ImageIcon picIcon4 = new ImageIcon(imgsrc4);
// Add images to picturePanel
picturePanel.add(new JLabel(picIcon1));
picturePanel.add(new JLabel(picIcon2));
picturePanel.add(new JLabel(picIcon3));
picturePanel.add(new JLabel(picIcon4));
// Add picture panel to mainPanel(the container).
mainPanel.add(picturePanel);
setContentPane(mainPanel);
}
}
// Removes all picture images from the panel, so new deal can be shown.
public void clearPicturePanel() {
picturePanel.removeAll();
}
}// end GUI.