Buttons acting like menus in Java. It's just like what you have seen in Windows 7 and in most software. It is like Menus without a MenuBar. It is dead simple.
java.awt.*: Contains the layout classes. FlowLayout here.
borderPainted(): Make the border painted false. It is your wish!
import javax.swing.*;javax.swing.*: Contains JFrame, JMenuBar, JMenu, JMenuItem.
import java.awt.*;
class MenuButton extends JFrame
{
JMenuBar menuBar;
JMenu menu;
JMenuItem newFile,open,save,saveas,exit;
public MenuButton()
{
setTitle("Menu Button");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// Set a background for menubutton to have a visible look
getContentPane().setBackground(Color.darkGray);
menuBar=new JMenuBar();
menuBar.setBorderPainted(false);
menu=new JMenu("File");
// It's my style!
menu.setBorderPainted(false);
newFile=new JMenuItem("New");
open=new JMenuItem("Open");
save=new JMenuItem("Save");
saveas=new JMenuItem("Save As");
exit=new JMenuItem("Exit");
menu.add(newFile);
menu.add(open);
menu.add(save);
menu.add(saveas);
menu.add(exit);
// Add menu to menubar
menuBar.add(menu);
// Add(don't set) menubar to frame
add(menuBar);
// Make frame maximized for a good look
//setExtendedState(MAXIMIZED_BOTH);
// or pack
pack();
}
public static void main(String args[])
{
new MenuButton();
}
}
java.awt.*: Contains the layout classes. FlowLayout here.
borderPainted(): Make the border painted false. It is your wish!