- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个应用程序,它实现了带有子 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/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!