Header Ads

[Java] - JMenuBar, JMenu, JRadioButtonMenuItem, JCheckBoxMenuItem


package thanhcs.bai14;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingConstants;

public class MenuBar extends JFrame implements ActionListener, ItemListener {
    
    JMenuItem itAbout, itExit;
    JRadioButtonMenuItem blue, red,  green;
    JRadioButtonMenuItem serif, monospaced, sansserif;
    JCheckBoxMenuItem bold, italic;
    
    JLabel jlabel;
    public MenuBar() {
            
        JMenuBar jmenubar = new JMenuBar();
        this.setJMenuBar(jmenubar);
        
        JMenu mnFile = new JMenu("File");
        JMenu mnFormat = new JMenu("Format");
        
        jmenubar.add(mnFile);
        jmenubar.add(mnFormat);
        
        
        mnFile.add(itAbout = new JMenuItem("About"));
        mnFile.add(itExit = new JMenuItem("Exit"));
        itAbout.addActionListener(this);
        itExit.addActionListener(this);
        
        
        JMenu color = new JMenu("Color");
        JMenu font = new JMenu("Font");
        mnFormat.add(color);
        mnFormat.addSeparator();
        mnFormat.add(font);
        
        ButtonGroup btg1 = new ButtonGroup();
        color.add(red = new JRadioButtonMenuItem("Red"));
        color.add(blue = new JRadioButtonMenuItem("Blue"));
        color.add(green = new JRadioButtonMenuItem("Green"));
        btg1.add(red);
        btg1.add(green);
        btg1.add(blue);
        red.addActionListener(this);
        blue.addActionListener(this);
        green.addActionListener(this);
        
        
        
        ButtonGroup btg2 = new ButtonGroup();
        font.add(serif = new JRadioButtonMenuItem("serif"));
        font.add(monospaced = new JRadioButtonMenuItem("monospaced"));
        font.add(sansserif = new JRadioButtonMenuItem("sansserif"));
        btg2.add(serif);
        btg2.add(monospaced);
        btg2.add(sansserif);
        serif.addActionListener(this);
        monospaced.addActionListener(this);
        sansserif.addActionListener(this);
        
        font.addSeparator();
        
        font.add(bold = new JCheckBoxMenuItem("blod"));
        font.add(italic = new JCheckBoxMenuItem("italic"));
        bold.addActionListener(this);
        italic.addActionListener(this);
        
        
        jlabel = new JLabel("Sample Text");
        add(jlabel, SwingConstants.CENTER);
        
        setTitle("Menu Bar");
        setSize(500, 400);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        new MenuBar().setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==itAbout)
        {
            JOptionPane.showMessageDialog(null, "Author : \nthanhcs.blogspot.com");;
        }
        
        if(e.getSource()==itExit)
        {
            System.exit(0);
        }
        
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getStateChange()==ItemEvent.SELECTED)
        {
            
        }
        else
        {
            
        }
        
    }

}

No comments:

Powered by Blogger.