Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- class MenuExample extends JFrame {
- public MenuExample() {
- setTitle("Menu Example");
- setSize(300, 200);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JMenuBar menuBar = new JMenuBar();
- setJMenuBar(menuBar);
- JMenu optionsMenu = new JMenu("Options");
- menuBar.add(optionsMenu);
- JCheckBoxMenuItem checkBoxItem = new JCheckBoxMenuItem("Option 1");
- optionsMenu.add(checkBoxItem);
- JRadioButtonMenuItem radioItem1 = new JRadioButtonMenuItem("Option 2");
- optionsMenu.add(radioItem1);
- JRadioButtonMenuItem radioItem2 = new JRadioButtonMenuItem("Option 3");
- optionsMenu.add(radioItem2);
- ButtonGroup radioGroup = new ButtonGroup();
- radioGroup.add(radioItem1);
- radioGroup.add(radioItem2);
- ActionListener listener = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JMenuItem item = (JMenuItem)e.getSource();
- String text = item.getText();
- boolean selected = item.isSelected();
- System.out.println(text + " is " + (selected ? "selected" : "deselected"));
- }
- };
- checkBoxItem.addActionListener(listener);
- radioItem1.addActionListener(listener);
- radioItem2.addActionListener(listener);
- }
- public static void main(String[] args) {
- MenuExample example = new MenuExample();
- example.setVisible(true);
- }
- }
- public class GUI{
- public static void main(String[] args) {
- MenuExample example = new MenuExample();
- example.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement