import java.awt.*; import javax.swing.*; public class Demo1GUIMain extends JFrame { private String[] songs = { "Drive My Car -Beatles", "Kid Charlemagne -Steely Dan", "This Love - Maroon 5", "Soul Meets Body -Death Cab for Cutie", "Float On - Modest Mouse", "Feelings - Gemini", "Stayin' Alive -Bee Gees", "Doctor My Eyes - Jackson Browne", "Peaceful Easy Feeling - The Eagles", "Spooky - Classics IV", "Tush - ZZ-top", "Who's Been Talking - Howlin Wolf" }; //constants for volumne slider final int MINIMUM_VOLUME = 0; final int MAXIMUM_VOLUME = 11; final int INITIAL_VOLUME = 3; //Panel dimensions for main window int PANEL_WIDTH = 500; //width of the main window panel int PANEL_HEIGHT = 300; //height of the main window panel public Demo1GUIMain(String title) { super(title); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); //make window non-resizeable getContentPane().setLayout(null); //use no layout management getContentPane().setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT)); pack(); //set window size to fit content pane System.out.println("CONTENT PANE WIDTH:" + getContentPane().getWidth()); System.out.println("CONTENT PANE HEIGHT:" + getContentPane().getHeight()); //constants useful for layout int BUTTON_WIDTH = 80; int BUTTON_HEIGHT = 80; int VOLUME_SLIDER_WIDTH = 100; int VOLUME_SLIDER_HEIGHT = PANEL_HEIGHT; Font buttonLabelFont = new Font("SansSerif", Font.BOLD, 20); Color buttonColor = new Color(0,0,0); System.out.println("PANEL_WIDTH " + PANEL_WIDTH); System.out.println("PANEL_HEIGHT " + PANEL_HEIGHT); //Add the components to the frame's panel, JList songList = new JList(songs); songList.setSize(PANEL_WIDTH*2/3,PANEL_HEIGHT-200); songList.setLocation(0,0); this.getContentPane().add(songList); //add(songList); also works JButton playButton = new JButton(new ImageIcon("play.gif")); playButton.setFont(buttonLabelFont); playButton.setBackground(buttonColor); playButton.setLocation(0,PANEL_HEIGHT - BUTTON_HEIGHT); //set location of component playButton.setSize(BUTTON_WIDTH,BUTTON_HEIGHT); //set width and height of component this.getContentPane().add(playButton); //add component to content pane //add(playButton); //also works JSlider volumeSlider = new JSlider(JSlider.VERTICAL, MINIMUM_VOLUME, MAXIMUM_VOLUME, INITIAL_VOLUME); volumeSlider.setLocation(PANEL_WIDTH-VOLUME_SLIDER_WIDTH,0); volumeSlider.setSize(VOLUME_SLIDER_WIDTH,VOLUME_SLIDER_HEIGHT); //Turn on labels at major tick marks. volumeSlider.setMajorTickSpacing(5); volumeSlider.setMinorTickSpacing(1); volumeSlider.setPaintTicks(true); volumeSlider.setPaintLabels(true); this.getContentPane().add(volumeSlider); //add volume slider to panel } public static void main(String args[]) { Demo1GUIMain frame = new Demo1GUIMain("ROCK-OLA Media Player"); //create the window frame.setVisible(true); //show the window } }