
how to create menubar and the below background color/image in java swing

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.event.*;
public class MenuBarExample_BackgroundImage implements ActionListener{
private static BufferedImage image;
private static JPanel panel = new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(image != null){
g.drawImage(image, 0, 0, this);
}
}
};
MenuBarExample_BackgroundImage(){
panel.setLayout(null);
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Select");
JMenuItem Item1 = new JMenuItem("c:\\flower1.jpg");
JMenuItem Item2 = new JMenuItem("c:\\flower2.jpg");
JMenuItem Item3 = new JMenuItem("c:\\flower3.jpg");
JMenuItem Item4 = new JMenuItem("c:\\flower4.jpg");
menu.add(Item1);
menu.add(Item2);
menu.add(Item3);
menu.add(Item4);
menubar.add(menu);
Item1.addActionListener(this);
Item2.addActionListener(this);
Item3.addActionListener(this);
Item4.addActionListener(this);
JFrame f=new JFrame();
f.setJMenuBar(menubar);
f.add(MenuBarExample_BackgroundImage.getPanel());
f.setSize(300,200);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String st=e.getActionCommand();
try{
File file=new File(st);
image = ImageIO.read(file);
Dimension imageSize = new Dimension(image.getWidth(), image.getHeight());
panel.setPreferredSize(imageSize);
panel.repaint();
}
catch(Exception ex){}
}
public static JPanel getPanel(){
return panel;
}
public static void main(String[] args){
new MenuBarExample_BackgroundImage();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.