Java swing:如何对齐行和列中的菜单项?

发布于 2024-12-11 22:13:23 字数 368 浏览 0 评论 0原文

创建菜单栏及其项目时我没有遇到任何问题。但是现在,当我遇到一个问题如何使菜单项显示为列和菜单项时行状的桌子形状,我真的不知道。

目标是使用 java 创建此类菜单项。 检查此链接。

现在,我只是认为我应该使用 jpanel 作为菜单项,然后应用流程布局,然后添加许多 jlabel,就像我可以作为网格内的菜单项一样。但这不是最糟糕的吗? 创建菜单项(例如上述链接上的图像预览)的最佳方案是什么?

我尝试google了一下,没有发现相关案例。 CMIIW。

I'm have no trouble when creating menu bar and its item. But now, when I get a question how to make the menu items appeared as column & rows-like table shaped, I really don't know about that.

The goals is to create this kind of menu items using java.
Check this link.

And right now, I just think that I should use a jpanel as menu item, and then applying a flowlayout and then adding many jlabel(s) as I could as menuitem inside the grid. But wouldn't it worst?
What's the best deal to create the menu items such as the image preview on the above links?

I tried google, but found no related cases. CMIIW.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人 2024-12-18 22:13:23

JMenu 实例的弹出菜单是一个标准容器,因此您可以向其中添加任何您想要的内容。它有默认布局,但您可以更改它。

您的模型中的类似内容是由以下代码创建的:

import javax.swing.*;
import java.awt.*;

public class Test {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Menu test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(new Dimension(600, 400));
                JMenuBar menuBar = new JMenuBar();
                JMenu menu = new JMenu("Test");
                JPopupMenu popupMenu = menu.getPopupMenu();
                popupMenu.setLayout(new GridLayout(5, 5));
                for (int r = 0; r < 5; r++) {
                    for (int c = 0; c < 5; c++) {
                        popupMenu.add(new JMenuItem("(" + (r + 1) + ", " + (c + 1) + ")"));
                    }
                }

                menuBar.add(menu);
                frame.setJMenuBar(menuBar);

                frame.setVisible(true);
            }
        });
    }

}

The popup menu of a JMenu instance is a standard container, so you can add to it whatever you want. It has a default layout, but you can change it.

Something like in your mockup is created by this code:

import javax.swing.*;
import java.awt.*;

public class Test {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Menu test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(new Dimension(600, 400));
                JMenuBar menuBar = new JMenuBar();
                JMenu menu = new JMenu("Test");
                JPopupMenu popupMenu = menu.getPopupMenu();
                popupMenu.setLayout(new GridLayout(5, 5));
                for (int r = 0; r < 5; r++) {
                    for (int c = 0; c < 5; c++) {
                        popupMenu.add(new JMenuItem("(" + (r + 1) + ", " + (c + 1) + ")"));
                    }
                }

                menuBar.add(menu);
                frame.setJMenuBar(menuBar);

                frame.setVisible(true);
            }
        });
    }

}
不喜欢何必死缠烂打 2024-12-18 22:13:23

我还没有看到像这样的现成组件。所以我认为你只能靠自己了。

我看到两种可能性:

  1. JMenuItem 是一个 JComponent,因此您可以向其中添加其他组件。您可能想使用某种基于网格的布局并为数字添加按钮或标签。

  2. 实现您自己的 JMenuItem 来显示您的网格组件而不是普通的 JPopupMenu

无论如何,请查看 JMenu(Item) 的源代码以了解这些组件的工作原理。

I haven't seen a ready made component for anything like this. So I think you are on your own.

I see two possibilities:

  1. JMenuItem is a JComponent, so you can add other components to it. You probably want to use some kind of grid based layout and add buttons or labels for the numbers.

  2. Implement you own JMenuItem that displays your grid component instead of the normal JPopupMenu

In any case have a look at the source code of JMenu(Item) in order to understand how these components work.

中二柚 2024-12-18 22:13:23

最简单的解决方案就是设置 JMenu 的 JPopupMenu 的布局,然后像平常一样添加项目。无需创建子类。

例子:

import javax.swing.*;
import java.awt.*;

public class menu {
    public static void main(String ... args) {
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JMenuBar menuBar = new JMenuBar();

                    JMenu menu = new JMenu("A regular menu");
                    menu.add(new JMenuItem("Menu item"));

                    JMenu gridMenu = new JMenu("Menu with grid");
                    // This does the trick:
                    gridMenu.getPopupMenu().setLayout(new GridLayout(2, 2));
                    gridMenu.add("Top left");
                    gridMenu.add("Top right");
                    gridMenu.add("Bottom left");
                    gridMenu.add("Bottom right");
                    menu.add(gridMenu);

                    menuBar.add(menu);

                    JFrame frame = new JFrame("Menus");
                    frame.setJMenuBar(menuBar);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
    }
}

The simplest solution is just set the layout of JMenu's JPopupMenu and then add items like you normally would. There's no need to create a subclass.

Example:

import javax.swing.*;
import java.awt.*;

public class menu {
    public static void main(String ... args) {
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JMenuBar menuBar = new JMenuBar();

                    JMenu menu = new JMenu("A regular menu");
                    menu.add(new JMenuItem("Menu item"));

                    JMenu gridMenu = new JMenu("Menu with grid");
                    // This does the trick:
                    gridMenu.getPopupMenu().setLayout(new GridLayout(2, 2));
                    gridMenu.add("Top left");
                    gridMenu.add("Top right");
                    gridMenu.add("Bottom left");
                    gridMenu.add("Bottom right");
                    menu.add(gridMenu);

                    menuBar.add(menu);

                    JFrame frame = new JFrame("Menus");
                    frame.setJMenuBar(menuBar);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文