⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demojmenuone.java

📁 精通NetBeans光盘源代码,很好很好的资料
💻 JAVA
字号:
/*
 * DemoJMenuOne.java
 *
 * Created on 2006年3月31日, 上午4:33
 */

package org.netbeans.swing.menu;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.*;

/**
 *
 * @author  boyingking
 */
public class DemoJMenuOne extends javax.swing.JFrame {
    
    /** Creates new form DemoJMenuOne */
    public DemoJMenuOne() {
        initComponents();
        PopupAdapter menuAdapter=new PopupAdapter(this.jPopupMenuAboutIcon);
        this.jLabelIcon.addMouseListener(menuAdapter);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroupOne = new javax.swing.ButtonGroup();
        jPopupMenuAboutIcon = new javax.swing.JPopupMenu();
        jMenuItemNoIcon = new javax.swing.JMenuItem();
        jMenuItemNewIcon = new javax.swing.JMenuItem();
        jLabelIcon = new javax.swing.JLabel();
        jMenuBarGlobal = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuItemOpenFile = new javax.swing.JMenuItem();
        jMenuItemSaveFile = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuOthers = new javax.swing.JMenu();
        jMenuItemSetting = new javax.swing.JMenuItem();
        jMenuItemSaveA = new javax.swing.JMenuItem();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenuEdit = new javax.swing.JMenu();
        jCheckBoxMenuItemBold = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItemItalic = new javax.swing.JCheckBoxMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jRadioButtonMenuItemEighteen = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItemSixteen = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItemTwenty = new javax.swing.JRadioButtonMenuItem();
        jMenuAbout = new javax.swing.JMenu();

        jPopupMenuAboutIcon.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPopupMenuAboutIcon.setMinimumSize(new java.awt.Dimension(30, 60));
        jMenuItemNoIcon.setText("\u4e0d\u4f7f\u7528\u56fe\u7247");
        jMenuItemNoIcon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNoIconActionPerformed(evt);
            }
        });

        jPopupMenuAboutIcon.add(jMenuItemNoIcon);

        jMenuItemNewIcon.setText("\u66f4\u6539\u56fe\u7247");
        jMenuItemNewIcon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNewIconActionPerformed(evt);
            }
        });

        jPopupMenuAboutIcon.add(jMenuItemNewIcon);

        getContentPane().setLayout(new java.awt.CardLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("\u83dc\u5355\u5e94\u7528\u793a\u4f8b");
        jLabelIcon.setIcon(new javax.swing.ImageIcon("F:\\学习\\booking\\happy.JPG"));
        getContentPane().add(jLabelIcon, "card2");

        jMenuFile.setText("File");
        jMenuItemOpenFile.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemOpenFile.setIcon(new javax.swing.ImageIcon("F:\\学习\\booking\\截图\\第9章菜单和工具栏\\9-2.png"));
        jMenuItemOpenFile.setText("Open File");
        jMenuItemOpenFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemOpenFileActionPerformed(evt);
            }
        });

        jMenuFile.add(jMenuItemOpenFile);

        jMenuItemSaveFile.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemSaveFile.setIcon(new javax.swing.ImageIcon("F:\\学习\\booking\\截图\\第9章菜单和工具栏\\9-3.png"));
        jMenuItemSaveFile.setText("Save File");
        jMenuItemSaveFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSaveFileActionPerformed(evt);
            }
        });

        jMenuFile.add(jMenuItemSaveFile);

        jMenuFile.add(jSeparator1);

        jMenuOthers.setText("Others");
        jMenuItemSetting.setText("Setting");
        jMenuOthers.add(jMenuItemSetting);

        jMenuItemSaveA.setText("SaveAs");
        jMenuOthers.add(jMenuItemSaveA);

        jMenuFile.add(jMenuOthers);

        jMenuItemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemExit.setText("Exit");
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });

        jMenuFile.add(jMenuItemExit);

        jMenuBarGlobal.add(jMenuFile);

        jMenuEdit.setText("Edit");
        jCheckBoxMenuItemBold.setText("\u9ed1\u4f53");
        jCheckBoxMenuItemBold.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCheckBoxMenuItemBoldItemStateChanged(evt);
            }
        });

        jMenuEdit.add(jCheckBoxMenuItemBold);

        jCheckBoxMenuItemItalic.setText("\u659c\u4f53");
        jCheckBoxMenuItemItalic.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCheckBoxMenuItemItalicItemStateChanged(evt);
            }
        });

        jMenuEdit.add(jCheckBoxMenuItemItalic);

        jMenuEdit.add(jSeparator2);

        buttonGroupOne.add(jRadioButtonMenuItemEighteen);
        jRadioButtonMenuItemEighteen.setSelected(true);
        jRadioButtonMenuItemEighteen.setText("18\u53f7\u5b57");
        jRadioButtonMenuItemEighteen.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButtonMenuItemEighteenItemStateChanged(evt);
            }
        });

        jMenuEdit.add(jRadioButtonMenuItemEighteen);

        buttonGroupOne.add(jRadioButtonMenuItemSixteen);
        jRadioButtonMenuItemSixteen.setText("16\u53f7\u5b57");
        jRadioButtonMenuItemSixteen.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButtonMenuItemSixteenItemStateChanged(evt);
            }
        });

        jMenuEdit.add(jRadioButtonMenuItemSixteen);

        buttonGroupOne.add(jRadioButtonMenuItemTwenty);
        jRadioButtonMenuItemTwenty.setText("20\u53f7\u5b57");
        jRadioButtonMenuItemTwenty.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButtonMenuItemTwentyItemStateChanged(evt);
            }
        });

        jMenuEdit.add(jRadioButtonMenuItemTwenty);

        jMenuBarGlobal.add(jMenuEdit);

        jMenuAbout.setText("About");
        jMenuBarGlobal.add(jMenuAbout);

        setJMenuBar(jMenuBarGlobal);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemNewIconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNewIconActionPerformed
// TODO add your handling code here:
        iconChooser.showOpenDialog(this);
        File iconFile=iconChooser.getSelectedFile();
        if(iconFile!=null)
        {
            ImageIcon labelIcon=new ImageIcon(iconFile.toString());
            this.jLabelIcon.setIcon(labelIcon);
        }
    }//GEN-LAST:event_jMenuItemNewIconActionPerformed

    private void jMenuItemNoIconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNoIconActionPerformed
// TODO add your handling code here:
        this.jLabelIcon.setIcon(null);
    }//GEN-LAST:event_jMenuItemNoIconActionPerformed

    private void jRadioButtonMenuItemSixteenItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItemSixteenItemStateChanged
// TODO add your handling code here:
        this.dealAction(evt);
    }//GEN-LAST:event_jRadioButtonMenuItemSixteenItemStateChanged

    private void jRadioButtonMenuItemEighteenItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItemEighteenItemStateChanged
// TODO add your handling code here:
        this.dealAction(evt);
    }//GEN-LAST:event_jRadioButtonMenuItemEighteenItemStateChanged

    private void jCheckBoxMenuItemItalicItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBoxMenuItemItalicItemStateChanged
// TODO add your handling code here:
        this.dealAction(evt);
    }//GEN-LAST:event_jCheckBoxMenuItemItalicItemStateChanged

    private void jCheckBoxMenuItemBoldItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBoxMenuItemBoldItemStateChanged
// TODO add your handling code here:
        this.dealAction(evt);
    }//GEN-LAST:event_jCheckBoxMenuItemBoldItemStateChanged

    private void jRadioButtonMenuItemTwentyItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItemTwentyItemStateChanged
// TODO add your handling code here:
        this.dealAction(evt);
    }//GEN-LAST:event_jRadioButtonMenuItemTwentyItemStateChanged

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
// TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jMenuItemExitActionPerformed

    private void jMenuItemSaveFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveFileActionPerformed
        JFileChooser fileChooser=new JFileChooser();
        fileChooser.showSaveDialog(this);
    }//GEN-LAST:event_jMenuItemSaveFileActionPerformed

    private void jMenuItemOpenFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemOpenFileActionPerformed
// TODO add your handling code here:
        JFileChooser fileChooser=new JFileChooser();
        fileChooser.showOpenDialog(this);
    }//GEN-LAST:event_jMenuItemOpenFileActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DemoJMenuOne().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupOne;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItemBold;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItemItalic;
    private javax.swing.JLabel jLabelIcon;
    private javax.swing.JMenu jMenuAbout;
    private javax.swing.JMenuBar jMenuBarGlobal;
    private javax.swing.JMenu jMenuEdit;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemNewIcon;
    private javax.swing.JMenuItem jMenuItemNoIcon;
    private javax.swing.JMenuItem jMenuItemOpenFile;
    private javax.swing.JMenuItem jMenuItemSaveA;
    private javax.swing.JMenuItem jMenuItemSaveFile;
    private javax.swing.JMenuItem jMenuItemSetting;
    private javax.swing.JMenu jMenuOthers;
    private javax.swing.JPopupMenu jPopupMenuAboutIcon;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItemEighteen;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItemSixteen;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItemTwenty;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    // End of variables declaration//GEN-END:variables
    
    public void dealAction(java.awt.event.ItemEvent evt)
    {
        if(evt.getSource()==jCheckBoxMenuItemBold)
        {
            if(jCheckBoxMenuItemBold.isSelected())
            {
                JOptionPane.showMessageDialog(this,"你选中了黑体复选菜单!","提示:",JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(this,"你取消了对黑体复选菜单的选择!","提示:",JOptionPane.INFORMATION_MESSAGE);
            }
        }
        else if(evt.getSource()==jCheckBoxMenuItemItalic)
        {
            if(jCheckBoxMenuItemItalic.isSelected())
            {
                JOptionPane.showMessageDialog(this,"你选中了斜体复选菜单!","提示:",JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(this,"你取消了对斜体复选菜单的选择!","提示:",JOptionPane.INFORMATION_MESSAGE);
            }
        }
        else if(evt.getSource()==jRadioButtonMenuItemEighteen)
        {
            if(jRadioButtonMenuItemEighteen.isSelected())
             JOptionPane.showMessageDialog(this,"“18号字”单选菜单被选中!","提示:",JOptionPane.INFORMATION_MESSAGE);
        }
        else if(evt.getSource()==jRadioButtonMenuItemSixteen)
        {
            if(jRadioButtonMenuItemSixteen.isSelected())
             JOptionPane.showMessageDialog(this,"“16号字”单选菜单被选中!","提示:",JOptionPane.INFORMATION_MESSAGE);
        }
        else if(evt.getSource()==jRadioButtonMenuItemTwenty)
        {
            if(jRadioButtonMenuItemTwenty.isSelected())
             JOptionPane.showMessageDialog(this,"“20号字”单选菜单被选中!","提示:",JOptionPane.INFORMATION_MESSAGE);
        }         
    }
     class PopupAdapter extends MouseAdapter {
        JPopupMenu popup;
        PopupAdapter(JPopupMenu popupMenu) {
            popup = popupMenu;
        }
        public void mousePressed(MouseEvent e) {
            showPopupMenu(e);
        }
        public void mouseReleased(MouseEvent e) {
            showPopupMenu(e);
        }
        private void showPopupMenu(MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(e.getComponent(),e.getX(), e.getY());
            }
        }
    }   

    private JFileChooser iconChooser = new JFileChooser();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -