【问题标题】:how to make JButton show a list of JButtons with just few items如何让 JButton 显示只有几个项目的 JButton 列表
【发布时间】:2014-07-16 23:54:59
【问题描述】:

大家好,我的问题是当我将鼠标放在 JFrame 中的 JButton 上时,我希望它在左侧显示 JButton 列表。

我真的不知道该怎么做,我觉得我被阻止了,我的项目无法取得任何进展。

如果您能帮助我,我将不胜感激并提前致谢。

【问题讨论】:

  • 你上传的图片在哪里?
  • 您能否更详细地解释您正在尝试做什么以及您遇到的问题?
  • 我想当我将鼠标放在第一个 JButton 上时,例如它的关联项目列表。你现在明白我了吗?
  • 不,不是。您是否希望设置 JButton 的工具提示?您确实需要花一点时间来向我们提供您的问题的详细信息。恐怕你掩饰得太多了。
  • Tooltips Tutorial,但同样,让我们​​远离这个痛苦而令人沮丧的游戏,我们试图从你那里窃取信息,而你却给了我们一些不完整的小片段。如果您希望有人努力给您一个完整的答案,请努力提出一个完整而彻底的问题。

标签: java swing jbutton


【解决方案1】:

您能否在JPanel 中创建按钮列表,将其添加到您的JFrame,然后调用myPanel.setVisible(false)。当你点击你的按钮然后打电话给myPanel.setVisible(true)?

至于确保myPanel 的位置正确,您需要使用Layout Manager

或者你想要更复杂的行为?

【讨论】:

  • 不多,我会试试这个,我会告诉你它是否有效@Richard
【解决方案2】:

基本选项是使用MouseListenerCardLayoutMouseListener 将用于确定鼠标光标何时进入/存在给定组件,CardLayout 将用于显示每个“菜单”元素的适当子组件。

我不得不说,JButton 将是我“菜单”项的最后选择,在大多数情况下,JLabel 将是首选,甚至可能使用JMenu,它可以有子菜单,可以自动显示可能是更好的选择,甚至是JComboBox....

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ShowStuff {

    public static void main(String[] args) {
        new ShowStuff();
    }

    public ShowStuff() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                MenuPane menu = new MenuPane();
                menu.addMenu("Fruit", new FruitPane());
                menu.addMenu("Meat", new MeatPane());
                menu.addMenu("Dairy", new DairyPane());

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(menu);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MenuPane extends JPanel {

        private JPanel subMenu;
        private JPanel menu;
        private CardLayout cardLayout;
        private MouseListener mouseHandler;

        public MenuPane() {
            setLayout(new BorderLayout());
            cardLayout = new CardLayout();
            subMenu = new JPanel(cardLayout);

            menu = new JPanel(new GridBagLayout());
            add(subMenu);
            add(menu, BorderLayout.WEST);

            subMenu.add(new JPanel(), "BLANK");

            mouseHandler = new MouseAdapter() {

                @Override
                public void mouseEntered(MouseEvent e) {
                    if (e.getSource() instanceof JButton) {
                        JButton btn = (JButton) e.getSource();
                        cardLayout.show(subMenu, btn.getText());
                    }
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    cardLayout.show(subMenu, "BLANK");
                }

            };
        }

        public void addMenu(String name, JPanel subMenuPane) {
            JButton button = new JButton(name);
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            menu.add(button, gbc);
            subMenu.add(subMenuPane, name);
            button.addMouseListener(mouseHandler);
        }

    }

    public abstract class ButtonPane extends JPanel {

        private int gridy = 0;

        public ButtonPane() {
            setLayout(new GridBagLayout());
        }

        protected void addButton(String name) {
            JButton btn = new JButton(name);
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = gridy++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(btn, gbc);
        }

    }

    public class FruitPane extends ButtonPane {

        public FruitPane() {
            addButton("Banana");
            addButton("Grapes");
            addButton("Apples");
            addButton("Tomatoes");
        }

    }

    public class MeatPane extends ButtonPane {

        public MeatPane() {
            addButton("Lamb");
            addButton("Beef");
            addButton("Pork");
            addButton("Mince");
        }

    }

    public class DairyPane extends ButtonPane {

        public DairyPane() {
            addButton("Milk");
            addButton("Cream");
            addButton("Cheese");
            addButton("Yoghurt");
        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多