Making a JButton act like a JMenu

别说谁变了你拦得住时间么 提交于 2019-12-01 12:10:13

问题


I have the following code for a JMenuBar (This code has been taken from a free java program call JGuiD and edited for personal purposes)

import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Dimension;
import java.awt.Color;

public class GuiDMenuBar extends JMenuBar
{
    JMenu m_file,m_edit,m_help;
    JMenuItem mi_f_new,mi_f_open,mi_f_save,mi_f_saveas,mi_f_exit;
    JMenuItem mi_e_cut,mi_e_copy,mi_e_paste,mi_e_delete;
    JMenuItem mi_v_motif,mi_v_java,mi_v_windows,mi_v_nimbus;
    JMenuItem mi_h_help,mi_h_about;
    JButton m_code;

    public GuiDMenuBar()
    {
        setBorderPainted(true);
        makeFileMenu();
        makeEditMenu();
        makeCodeButton();
        makeHelpMenu();
    }

    void makeFileMenu()
    {
        m_file = new JMenu("File");
        m_file.setMnemonic('F');

        mi_f_new = new JMenuItem("New",new ImageIcon("icons/new_project.png"));
        mi_f_new.setMnemonic('N');
        mi_f_open = new JMenuItem("Open",new ImageIcon("icons/open_project.png"));
        mi_f_open.setMnemonic('O');
        mi_f_save = new JMenuItem("Save",new ImageIcon("icons/save.png"));
        mi_f_save.setMnemonic('S');
        mi_f_saveas = new JMenuItem("Save Java File",new ImageIcon("icons/saveas.png"));
        mi_f_saveas.setMnemonic('J');
        mi_f_exit = new JMenuItem("Exit",new ImageIcon("icons/exit.png"));
        mi_f_exit.setMnemonic('X');

        mi_f_new.setAccelerator(KeyStroke.getKeyStroke("control N"));
        mi_f_open.setAccelerator(KeyStroke.getKeyStroke("control O"));
        mi_f_save.setAccelerator(KeyStroke.getKeyStroke("control S"));
        mi_f_saveas.setAccelerator(KeyStroke.getKeyStroke("control J"));
        mi_f_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK));

        m_file.add(mi_f_new);
        m_file.add(mi_f_open);
        m_file.addSeparator();
        m_file.add(mi_f_save);
        m_file.add(mi_f_saveas);
        m_file.addSeparator();
        m_file.add(mi_f_exit);

        add(m_file);
    }

    void makeEditMenu()
    {
        m_edit = new JMenu("Edit");
        m_edit.setMnemonic('E');

        mi_e_cut = new JMenuItem("Cut",new ImageIcon("icons/cut.png"));
        mi_e_cut.setMnemonic('X');
        mi_e_copy = new JMenuItem("Copy",new ImageIcon("icons/copy.png"));
        mi_e_copy.setMnemonic('C');
        mi_e_paste = new JMenuItem("Paste",new ImageIcon("icons/paste.png"));
        mi_e_paste.setMnemonic('P');
        mi_e_delete = new JMenuItem("Delete",new ImageIcon("icons/delete.png"));
        mi_e_delete.setMnemonic('D');


        mi_e_cut.setAccelerator(KeyStroke.getKeyStroke("control X"));
        mi_e_copy.setAccelerator(KeyStroke.getKeyStroke("control C"));
        mi_e_paste.setAccelerator(KeyStroke.getKeyStroke("control V"));
        mi_e_delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));

        m_edit.add(mi_e_cut);
        m_edit.add(mi_e_copy);
        m_edit.add(mi_e_paste);
        m_edit.add(mi_e_delete);

        add(m_edit);
    }

    void makeHelpMenu()
    {
        m_help = new JMenu("Help");
        m_help.setMnemonic('H');

        mi_h_help = new JMenuItem("Help",new ImageIcon("icons/help.png"));
        mi_h_help.setMnemonic('H');
        mi_h_about = new JMenuItem("About");
        mi_h_about.setMnemonic('A');

        mi_h_help.setAccelerator(KeyStroke.getKeyStroke("F1"));
        mi_h_about.setAccelerator(KeyStroke.getKeyStroke("control A"));

        m_help.add(mi_h_help);
        m_help.addSeparator();
        m_help.add(mi_h_about);

        add(m_help);
    }

    void makeCodeButton()
    {
        m_code = new JButton();

        m_code.setOpaque(false);
        m_code.setContentAreaFilled(false);
        m_code.setBorder(null);
        m_code.setFocusable(false);

        m_code.setText("Code   Shift C");

        Dimension dBt = new Dimension(75,25);
        m_code.setMinimumSize(dBt);
        m_code.setPreferredSize(dBt);
        m_code.setMaximumSize(dBt);

        m_code.getModel().addChangeListener(new ChangeListener()
        {
            @Override
            public void stateChanged(ChangeEvent e) 
            {
                ButtonModel model = (ButtonModel) e.getSource();

                if(model.isRollover())
                {
                    m_code.setBackground(Color.RED);
                    m_code.setOpaque(true);
                } 

                else 
                {
                    m_code.setBackground(null);
                    m_code.setOpaque(false);
                    m_code.setContentAreaFilled(false);
                }
             }
        });

        m_code.setMnemonic('C');

        add(m_code);
    }

    public void addListeners(ActionListener al)
    {
        mi_f_new.addActionListener(al);
        mi_f_open.addActionListener(al);
        mi_f_save.addActionListener(al);
        mi_f_saveas.addActionListener(al);
        mi_f_exit.addActionListener(al);

        mi_e_cut.addActionListener(al);
        mi_e_copy.addActionListener(al);
        mi_e_paste.addActionListener(al);
        mi_e_delete.addActionListener(al);

        mi_h_help.addActionListener(al);
        mi_h_about.addActionListener(al);

        m_code.addActionListener(al);
    }

}

My aim is to make the JButton to appear like it is a JMenu. This would entail that the button only changes colour when I am interacting with the rest of the JMenuBar not just when I hover over it. For example if I had clicked on the JMenu m_file and then hovered over the JButton the background would change, however if I was not previously interacting with the JMenuBar it should not change the background when I hover over the JButton. The next thing required would be the JMenuBar treats it as a JMenu. I mean this in the sense that when F10 is clicked and the first JMenu is selected. After which you can then click on the right arrow on the arrow key pad on your keyboard, this will select the next JMenu. However using this method of navigation skips the JButton and does not allow you to interact with the JButton in anyway. I also mean this in the sense that if you interact with a JMenu and then hover over the JButton the JMenuBar shows you are still hovering over the JMenu as well (See Image).

So I suppose I have three questions.

  1. How do you get the JButton to only change colour if the JMenuBar is already being interacted with?

  2. How do you get the JMenuBar to treat the JButton as a JMenu in the senses that I described?

  3. Is anyone aware of the exact colour of the background of the JMenu when you hover over it? Since I would prefer to change my JButton's background to the same colour of the JMenu's background instead of it just being red.

Thanks,

Dan

来源:https://stackoverflow.com/questions/30605685/making-a-jbutton-act-like-a-jmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!