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();
setJMenuBar(bar);

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

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
foregroundItems.addActionListener(itemHandler);
}//end for loop
foregroundItems[0].setSelected(true);//select first color item

colorMenu.add(foregroundMenu);
colorMenu.addSeparator();

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
backgroundItems.addActionListener(itemHandler2);
}//end for loop
backgroundItems[0].setSelected(true);//select first color item

colorMenu.add(backgroundMenu);
colorMenu.addSeparator();

private class ItemHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
for (int i=0;i<foregroundItems.length;i++){
if(foregroundItems.isSelected())
{displayJLabel.setForeground(colorValues);
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++){
if(backgroundItems.isSelected())
{displayJLabel.setBackground(colorValues2);
break;}//end of if
}//end for loop
}//end method actionPerformed
}//end class ItemHandler
//*************************************MENUS*************************************************************************//

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

exitMenu.addActionListener(
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
frameMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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, Color.red), gbc);
        panel.add(getPanel(d,  4, Color.green.darker()), gbc);
        panel.add(getPanel(d, 12, Color.blue), 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());
        panel.setBackground(color);
        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);
        scrollPane.setPreferredSize(d);
        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,Color.red,
                                                     0,h,Color.cyan);
                ((Graphics2D)g).setPaint(gp);
                g.fillRect(0,0,w,h);
            }
        };
        // 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);
        scrollPane.setPreferredSize(d);
        return scrollPane;
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new MultiPanels().getContent());
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.