In this post, we will see Java Swing Menu Bar | Menu Bar in Java Swing Example | Java Swing JMenuBar, JMenu, JMenuItem.
Watch following video:
Watch on YouTube: https://www.youtube.com/watch?v=4VqnSgB1fOs
Go through the following program.
Program (SwingMenu.java)
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingMenu { public static void main(String args[]) { Abc obj=new Abc(); } } class Abc extends JFrame implements ActionListener { JMenuBar mb=new JMenuBar(); JMenu m1=new JMenu("File"); JMenu m2=new JMenu("Edit"); JMenu m3=new JMenu("Help"); JMenuItem mi1=new JMenuItem("Cut"); JMenuItem mi2=new JMenuItem("Copy"); JMenuItem mi3=new JMenuItem("Paste"); JMenuItem mi4=new JMenuItem("Select All"); JTextArea ta=new JTextArea(20,20); public Abc() { setVisible(true); setSize(250,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); add(mb); mb.add(m1); mb.add(m2); mb.add(m3); m2.add(mi1); m2.add(mi1); m2.add(mi2); m2.add(mi3); m2.add(mi4); add(ta); mi1.addActionListener(this); mi2.addActionListener(this); mi3.addActionListener(this); mi4.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==mi1) { ta.cut(); } if(e.getSource()==mi2) { ta.copy(); } if(e.getSource()==mi3) { ta.paste(); } if(e.getSource()==mi4) { ta.selectAll(); } } } |
How To Run:
parag@parag-Inspiron-N4010:~/Desktop/swing$ javac SwingMenu.java parag@parag-Inspiron-N4010:~/Desktop/swing$ java SwingMenu |
Output:
After Edit -> Select All -> Copy -> Paste
No comments:
Post a Comment