如何确定挥杆组件?

时间:2021-09-23 20:44:30

I have a problem.

我有个问题。

I'm working on another guy's code and there is a JFrame with lots of JSeparators(he used them as borders for 'panels') now I'm replacing them for a JBorderedPanel class that follows the same border style of the whole application.

我正在研究另一个人的代码,并且有一个带有大量JSeparator的JFrame(他将它们用作'面板'的边框)现在我正在为一个遵循整个应用程序相同边框样式的JBorderedPanel类替换它们。

The problem is that some of his separators are not clear to determine where they are in the code, there are lots of jSeparator#, replace with for any number between 0 and 999.

问题是他的一些分隔符不清楚它们在代码中的位置,有很多jSeparator#,替换为0到999之间的任何数字。

Is there any way to determine which variable correspond to which border other than testing all jSeparators one by one?

除了逐个测试所有jSeparator之外,有没有办法确定哪个变量对应哪个边界?

In before 'Don't replace them!' I'm obligated to replace them. I wouldn't be doing this if I could.

在'不要替换它们'之前!我有义务更换它们。如果可以的话,我不会这样做。

Thanks in advance.

提前致谢。

7 个解决方案

#1


8  

Take a look at Swing Explorer. It's quite a handy swing debugging tool. There's a plugin for Eclipse that will instrument your code on the fly and launch.

看看Swing Explorer。这是一个非常方便的摇摆调试工具。有一个Eclipse插件可以动态检测代码并启动。

With it you can view the swing object heirarchy, right click on it, and render any part of it in another window that highlights each component and lets you see their boundaries, as well as select them. Once selected, you can right click the component in the tree and print a stacktrace that will lead you to where that component gets created...

使用它,您可以查看swing对象层次结构,右键单击它,并在另一个窗口中渲染它的任何部分,突出显示每个组件并让您查看它们的边界,以及选择它们。选择后,您可以右键单击树中的组件并打印一个堆栈跟踪,它将引导您到创建该组件的位置...

#2


2  

You could add a color to each of those separators in the code (green, red, yellow, and so on) and see where those colored JSeparator end up being displayed in your application...

您可以在代码中为每个分隔符添加一种颜色(绿色,红色,黄色等),并查看那些彩色JSeparator最终在应用程序中显示的位置...

#3


2  

Walk the children of the JFrame and add the mouse listener to each JSeparator inside of it:

遍历JFrame的子节点并将鼠标侦听器添加到其中的每个JSeparator:

public void installListeners (java.awt.Container parent) {
    for (Component child: parent.getComponents()) {
        if (child instanceof JSeparator) {
            child.addMouseListener (...
                hover(event);
            }
        }
        if (child instanceof java.awt.Container) {
            installListeners ((java.awt.Container)child);
        }
    }
}

Now implement hover() to compare the event source with all the fields in the current class and print the one that matches:

现在实现hover()将事件源与当前类中的所有字段进行比较,并打印匹配的字段:

public void hover (MouseEvent event) {
    for (Field f: getClass().getFields()) {
        if (f.get(this) == event.getSource()) {
            System.out.println(f.getname());
            break;
        }
    }
}

You'll have to handle a bazillion of Exceptions but that's basically it.

你将不得不处理大量的例外情况,但基本上就是这样。

#4


1  

You could install a MouseListener on each JSeparator. When the mouse enter its area, turn its background red and print a line identifying the object, preferrably by printing its variable name. This probably requires you to change the constructor calls but your IDE should support you doing it.

您可以在每个JSeparator上安装MouseListener。当鼠标进入其区域时,将其背景变为红色并打印标识该对象的线条,最好通过打印其变量名称。这可能需要您更改构造函数调用,但您的IDE应该支持您执行此操作。

#5


0  

I guess the previous guy used somekind of GUI editor.

我想之前的家伙使用了一些GUI编辑器。

My first try will be the GUI editor in Netbeans or Eclipse. They may able to parse and render it correctly, unless the code is really ugly.

我的第一次尝试将是Netbeans或Eclipse中的GUI编辑器。他们可能能够正确地解析和渲染它,除非代码真的很难看。

If it can be open, you can trace where they are by select them on UI.

如果它可以打开,您可以通过在UI上选择它们来跟踪它们的位置。

#6


0  

Op here, I liked both of your ideas, but all the jSeparators are initializated like this:

在这里,我喜欢你的两个想法,但是所有的jSeparator都是这样初始化的:

public JSeparator getJSeparatorArvore01() {
    if (jSeparatorArvore01 == null) {
        jSeparatorArvore01 = new JSeparator();
        jSeparatorArvore01.setLocation(new Point(14, 38));
        jSeparatorArvore01.setSize(new Dimension(72, 10));
    }
    return jSeparatorArvore01;
}

How do I add mouse listeners(or different colores) on over 50 jSeparators, without spending 24h? :(

如何在超过50个jSeparator上添加鼠标监听器(或不同颜色),而不花费24小时? :(

#7


0  

I would advise you to not use borders much. Borders are probably the most misused component in GUI history. Originally it was intended to group together a very small set of related components, usually checkboxes or radio buttons. Then someone invted the titled border and it turned into the lazy programmers way of naming sections, which ideally should be done with the use of a label and white space.

我会建议你不要使用很多边框。边框可能是GUI历史中最被滥用的组件。最初它的目的是将一小组相关组件组合在一起,通常是复选框或单选按钮。然后有人邀请了标题边框,它变成了懒惰程序员命名部分的方式,理想情况下应该使用标签和空格来完成。

A border will just add visual noise instead of the intended separation. Less is more.

边框只会增加视觉噪音而不是预期的分离。少即是多。

#1


8  

Take a look at Swing Explorer. It's quite a handy swing debugging tool. There's a plugin for Eclipse that will instrument your code on the fly and launch.

看看Swing Explorer。这是一个非常方便的摇摆调试工具。有一个Eclipse插件可以动态检测代码并启动。

With it you can view the swing object heirarchy, right click on it, and render any part of it in another window that highlights each component and lets you see their boundaries, as well as select them. Once selected, you can right click the component in the tree and print a stacktrace that will lead you to where that component gets created...

使用它,您可以查看swing对象层次结构,右键单击它,并在另一个窗口中渲染它的任何部分,突出显示每个组件并让您查看它们的边界,以及选择它们。选择后,您可以右键单击树中的组件并打印一个堆栈跟踪,它将引导您到创建该组件的位置...

#2


2  

You could add a color to each of those separators in the code (green, red, yellow, and so on) and see where those colored JSeparator end up being displayed in your application...

您可以在代码中为每个分隔符添加一种颜色(绿色,红色,黄色等),并查看那些彩色JSeparator最终在应用程序中显示的位置...

#3


2  

Walk the children of the JFrame and add the mouse listener to each JSeparator inside of it:

遍历JFrame的子节点并将鼠标侦听器添加到其中的每个JSeparator:

public void installListeners (java.awt.Container parent) {
    for (Component child: parent.getComponents()) {
        if (child instanceof JSeparator) {
            child.addMouseListener (...
                hover(event);
            }
        }
        if (child instanceof java.awt.Container) {
            installListeners ((java.awt.Container)child);
        }
    }
}

Now implement hover() to compare the event source with all the fields in the current class and print the one that matches:

现在实现hover()将事件源与当前类中的所有字段进行比较,并打印匹配的字段:

public void hover (MouseEvent event) {
    for (Field f: getClass().getFields()) {
        if (f.get(this) == event.getSource()) {
            System.out.println(f.getname());
            break;
        }
    }
}

You'll have to handle a bazillion of Exceptions but that's basically it.

你将不得不处理大量的例外情况,但基本上就是这样。

#4


1  

You could install a MouseListener on each JSeparator. When the mouse enter its area, turn its background red and print a line identifying the object, preferrably by printing its variable name. This probably requires you to change the constructor calls but your IDE should support you doing it.

您可以在每个JSeparator上安装MouseListener。当鼠标进入其区域时,将其背景变为红色并打印标识该对象的线条,最好通过打印其变量名称。这可能需要您更改构造函数调用,但您的IDE应该支持您执行此操作。

#5


0  

I guess the previous guy used somekind of GUI editor.

我想之前的家伙使用了一些GUI编辑器。

My first try will be the GUI editor in Netbeans or Eclipse. They may able to parse and render it correctly, unless the code is really ugly.

我的第一次尝试将是Netbeans或Eclipse中的GUI编辑器。他们可能能够正确地解析和渲染它,除非代码真的很难看。

If it can be open, you can trace where they are by select them on UI.

如果它可以打开,您可以通过在UI上选择它们来跟踪它们的位置。

#6


0  

Op here, I liked both of your ideas, but all the jSeparators are initializated like this:

在这里,我喜欢你的两个想法,但是所有的jSeparator都是这样初始化的:

public JSeparator getJSeparatorArvore01() {
    if (jSeparatorArvore01 == null) {
        jSeparatorArvore01 = new JSeparator();
        jSeparatorArvore01.setLocation(new Point(14, 38));
        jSeparatorArvore01.setSize(new Dimension(72, 10));
    }
    return jSeparatorArvore01;
}

How do I add mouse listeners(or different colores) on over 50 jSeparators, without spending 24h? :(

如何在超过50个jSeparator上添加鼠标监听器(或不同颜色),而不花费24小时? :(

#7


0  

I would advise you to not use borders much. Borders are probably the most misused component in GUI history. Originally it was intended to group together a very small set of related components, usually checkboxes or radio buttons. Then someone invted the titled border and it turned into the lazy programmers way of naming sections, which ideally should be done with the use of a label and white space.

我会建议你不要使用很多边框。边框可能是GUI历史中最被滥用的组件。最初它的目的是将一小组相关组件组合在一起,通常是复选框或单选按钮。然后有人邀请了标题边框,它变成了懒惰程序员命名部分的方式,理想情况下应该使用标签和空格来完成。

A border will just add visual noise instead of the intended separation. Less is more.

边框只会增加视觉噪音而不是预期的分离。少即是多。