am trying to make a menu with a frame en its not working.
and how can i introduce multiple panels in the same frame?
here is my code.
import java.awt.*;
import javax.awt.event.*;
import javax.swing.*;

public class FrameMenu extends Jframe{

private final Color colorValues[]={Color.YELLOW,Color.RED,Color.BLUE,Color.GREEN};
private final Color colorValues2[]={Color.GRAY,Color.CYAN,Color.MAGENTA,Color.WHITE};
private RadioButtonMenuItem colorItems[];//color menu items

public FrameMenu(){
super("MY MENUS");

JMenuBar bar=new JMenuBar();

JMenu musicMenu=JMenu("Music");// create music menu
musicMenu.setMnemonic('M');//set mnemonic to M

JMenu colorMenu=JMenu("Color"); //create color menu
colorMenu.setMnemonic('C');//set mnemonic to O
bar.add(colorMenu);//add colorMenu to Menubar

//array listing string colors
String colors[]={"Yellow","Red","Blue","Green"};

JMenu foregroundMenu=new JMenu("Foregroud");//create foregroundMenu
foregroundMenu.setMnemonic('F');//set Mnemonic to F

foregroundItems=new JRadioButtonMenuItem[colors.length];
foregroundButtonGroup= new ButtonGroup();//manages forground colors
ItemHandler itemHandler=new ItemHandler();//handler for foreground colors

//create forgrond radio button menu items
for(int i=0;i<colors.length;i++){
foregroundItems=new JRadioButtonMenuItem(colors);//create item
foregroundMenu.addd(foregroundItems);//add item to forground menu
foregroundButtonGroup.add(foregroundItems);//add to group
}//end for loop
foregroundItems[0].setSelected(true);//select first color item


String colors2[]={"Gray","Cyan","Magenta","White"};

JMenu backgroundMenu=new JMenu("Background");//create backgroundMenu
backgroundMenu.setMnemonic('B');//set Mnemonic to B

backgroundItems=new JRadioButtonMenuItem[colors2.length];
backgroundButtonGroup= new ButtonGroup();//manages background colors
ItemHandler itemHandler2=new ItemHandler();//handler for background colors

//create forgrond radio button menu items
for(int i=0;i<colors.length;i++){
backgroundItems=new JRadioButtonMenuItem(colors);//create item
backgroundMenu.addd(backgroundItems);//add item to background menu
backgroundButtonGroup.add(backgroundItems);//add to group
}//end for loop
backgroundItems[0].setSelected(true);//select first color item


private class ItemHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
for (int i=0;i<foregroundItems.length;i++){
break;}//end of if
}//end for loop
}//end method actionPerformed
}//end class ItemHandler

private class ItemHandler implements ActionListener{
public void actionPerformed(ActionEvent event2){
for (int i=0;i<backgroundItems.length;i++){
break;}//end of if
}//end for loop
}//end method actionPerformed
}//end class ItemHandler

JMenu exitMenu=JMenu("Exit");//create exit menu
exitMenu.setMnemonic('x');//set mnemonic to M

new ActionListener(){
public void actionPerformed(ActionEvent event ){
System.exit(0);//exit application
}//end actionPerformed
}//end inner class
);//end call to addActionListener

public static void main(String arg[]){
FrameMenu frameMenu=new FrameMenu();//create FrameMenu
frameMenusetSize(1000,1000);//set frame size
frameMenu.setVisible(true);//display frame
}//end main
}//end FrameMenu

how can i introduce multiple panels in the same frame

Look at how to use layout managers.

Try this below code.

import java.awt.*;
import javax.swing.*;

public class MultiPanels {
    private JScrollPane getContent() {
        Dimension d = new Dimension(300,200);
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc= new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        panel.add(getPanel(d,  6,, gbc);
        panel.add(getPanel(d,  4,, gbc);
        panel.add(getPanel(d, 12,, gbc);
        panel.add(getEmptyPanel(d), gbc);
        return new JScrollPane(panel);

    private JScrollPane getPanel(Dimension d, int rows, Color color) {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc= new GridBagConstraints();
        gbc.insets = new Insets(10,5,10,5);
        gbc.weightx = 1.0;
        for(int i = 0, j = 1; i < rows; i++) {
            gbc.gridwidth = GridBagConstraints.RELATIVE;
            panel.add(new JButton(String.valueOf(j++)), gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(new JButton(String.valueOf(j++)), gbc);
        JScrollPane scrollPane = new JScrollPane(panel);
        return scrollPane;

    private JScrollPane getEmptyPanel(Dimension d) {
        JPanel panel = new JPanel() {
            protected void paintComponent(Graphics g) {
                int w = getWidth();
                int h = getHeight();
                GradientPaint gp = new GradientPaint(0,0,,
        // Default size for a JPanel is (10,10). Its layout
        // manager computes its preferredSize while laying
        // out the child components. If there are no children
        // the panel reports its default size to its parent.
        // Use the setPreferredSize method to provide
        // a size hint to the parent of this component.
        panel.setPreferredSize(new Dimension(300,400));
        JScrollPane scrollPane = new JScrollPane(panel);
        return scrollPane;

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.add(new MultiPanels().getContent());

It was working fine for me. If you want to do without a layout manager then go to this Absolute partitioning.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.