gpt4 book ai didi

java - 如何编写一个 [递归?] 函数来迭代 JMenu 层次结构中的(子)MenuElements?

转载 作者:行者123 更新时间:2023-12-01 19:33:49 25 4
gpt4 key购买 nike

我有一个应用程序,它实现了带有子 JMenu 和 JCheckBoxMenuItem 的子菜单项的 JMenu:

       JMenuBar menuBar = new JMenuBar();
menuBar.setEnabled(true);

JMenu toolsMenu = new JMenu("Tools");
toolsMenu.setEnabled(true);
menuBar.add(toolsMenu);

JMenu checkMenu = new JMenu("Checks");
checkMenu.setEnabled(true);
toolsMenu.add(checkMenu);

JMenu checkOneMenu = new JMenu("Check One");
checkOneMenu.setEnabled(true);
checkMenu.add(checkOneMenu);

JMenu checkTwoMenu = new JMenu("Check Two");
checkTwoMenu.setEnabled(true);
checkMenu.add(checkTwoMenu);

JMenu checkThreeMenu = new JMenu("Check Three");
checkThreeMenu.setEnabled(true);
checkMenu.add(checkThreeMenu);

每个检查[One,Two,Three]菜单都有子JCheckBoxMenuItem:

        checkOneMenu.add(new JCheckBoxMenuItem("One-one"));
checkOneMenu.add(new JCheckBoxMenuItem("One-two"));
checkOneMenu.add(new JCheckBoxMenuItem("One-three"));

checkTwoMenu.add(new JCheckBoxMenuItem("Two-one"));
checkTwoMenu.add(new JCheckBoxMenuItem("Two-two"));
checkTwoMenu.add(new JCheckBoxMenuItem("Two-three"));

checkThreeMenu.add(new JCheckBoxMenuItem("Three-one"));
checkThreeMenu.add(new JCheckBoxMenuItem("Three-two"));
checkThreeMenu.add(new JCheckBoxMenuItem("Three-three"));

所有这些都按预期在应用程序内运行:我可以选择(/[取消]检查)任何一个 JCheckBoxMenuItem。

作为一名优秀程序员,我现在想编写适当的 JUnit4 测试:

    @Test
public void testCheckOneMenu() {
Assume.assumeFalse(GraphicsEnvironment.isHeadless());
efo = new EditorFrameOperator(myJFrame);
JMenuOperator toolsJMO = new JMenuOperator(efo, toolsMenuTitle);
//this populates the checkOneMenu sub-JCheckBoxMenuItem's
toolsJMO.pushMenuNoBlock("Tools/Checks/Check One", "/");
new QueueTool().waitEmpty();

JPopupMenu toolsPopupMenu = toolsJMO.getPopupMenu();
//TODO:validate results
dumpMenuElement(toolsPopupMenu);
JMenuItem checkMenuItem = (JMenuItem) toolsPopupMenu.getComponent(0);
dumpMenuElement(checkMenuItem);

在“TODO:”步骤中,我需要迭代工具菜单的所有子菜单。我编写了一个递归函数来执行此操作:


private int tab_level = 1;
private void dumpMenuElement(MenuElement jMenuElement) {
System.out.println(StringUtils.leftPad(((JMenuItem) jMenuElement).getText(), tab_level, " "));
tab_level += 4;
for (MenuElement subMenuElement : jMenuElement.getSubElements()) {
dumpMenuElement(subMenuElement);
}
tab_level -= 4;
}

当我运行它时,我在工具菜单中得到了这个:

 Tools
Tools
Tools
Tools

这是检查菜单:

 Checks
Checks
Checks

我希望看到这个(对于工具菜单):

 Tools
Checks
Check One
One-One
One-Two
One-Three
Check Two
Two-One
Two-Two
Two-Three
Check Three
Three-One
Three-Two
Three-Three

有什么线索吗?提示?侮辱?(注意:我目前只是在调试 JUnit4 代码时转储标题。最终代码将仅访问标题并将它们与预期内容进行比较(无输出)。)

这是一个minimal reproducible example .

package dumpmenuelementtestapp;

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

/**
*
* @author geowar
*/
public class DumpMenuElementTestApp {

/**
* @param args the command line arguments
*/
public static JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
menuBar.setEnabled(true);

JMenu toolsMenu = new JMenu("Tools");
toolsMenu.setEnabled(true);
menuBar.add(toolsMenu);

JMenu checkMenu = new JMenu("Checks");
checkMenu.setEnabled(true);
toolsMenu.add(checkMenu);

JMenu checkOneMenu = new JMenu("Check One");
checkOneMenu.setEnabled(true);
checkMenu.add(checkOneMenu);

JMenu checkTwoMenu = new JMenu("Check Two");
checkTwoMenu.setEnabled(true);
checkMenu.add(checkTwoMenu);

JMenu checkThreeMenu = new JMenu("Check Three");
checkThreeMenu.setEnabled(true);
checkMenu.add(checkThreeMenu);

checkOneMenu.add(new JCheckBoxMenuItem("One-one"));
checkOneMenu.add(new JCheckBoxMenuItem("One-two"));
checkOneMenu.add(new JCheckBoxMenuItem("One-three"));

checkTwoMenu.add(new JCheckBoxMenuItem("Two-one"));
checkTwoMenu.add(new JCheckBoxMenuItem("Two-two"));
checkTwoMenu.add(new JCheckBoxMenuItem("Two-three"));

checkThreeMenu.add(new JCheckBoxMenuItem("Three-one"));
checkThreeMenu.add(new JCheckBoxMenuItem("Three-two"));
checkThreeMenu.add(new JCheckBoxMenuItem("Three-three"));

return menuBar;
}

public static Container createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);

//Create a scrolled text area.
JTextArea output = new JTextArea(5, 30);
output.setEditable(false);
JScrollPane scrollPane = new JScrollPane(output);

//Add the text area to the content pane.
contentPane.add(scrollPane, BorderLayout.CENTER);

return contentPane;
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("PopupMenuDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create/set menu bar and content pane.
frame.setJMenuBar(createMenuBar());
frame.setContentPane(createContentPane());

//Display the window.
frame.setSize(450, 260);
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

最佳答案

为了转储菜单栏的所有元素,我这样做了:

private static void dumpMenuBar(JMenuBar menuBar)
{
for (MenuElement element : menuBar.getSubElements())
{
JMenuItem menuItem = (JMenuItem)element;
System.out.println( menuItem.getText() );
dumpMenuElement( element );
}
}

private static void dumpMenuElement(MenuElement subElement)
{
for (MenuElement element : subElement.getSubElements())
{
if (element instanceof JMenuItem)
{
System.out.println( ((JMenuItem)element).getText() );
}

dumpMenuElement( element );
}
}

我明白了:

Tools
Checks
Check One
One-one
One-two
One-three
Check Two
Two-one
Two-two
Two-three
Check Three
Three-one
Three-two
Three-three

我无法访问StringUtils,因此我将让您找出正确的对齐方式。

关于java - 如何编写一个 [递归?] 函数来迭代 JMenu 层次结构中的(子)MenuElements?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59240061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com