为什么我会从 JComboBox 获得截然不同的行为,具体取决于平台 (Windows/Linux) 和语言 (Java/Jython)
我正在编写一个最终将在 Linux 上运行的 Jython 应用程序。我在 Windows 计算机上进行开发,在 Linux 上测试时出现不一致的行为。
- 开发机 - Windows XP、Java 6、Jython 2.5.2
- 测试机 - Red Hat 3.4.3、Java 6、Jython 2.5.2
在 Windows 中,使用 Java 或 Jython,我得到了我认为“正常”的 JComboBox行为:
- 单击文本或箭头,列表下拉。
- 单击所需的项目,即可进行选择。
在 Linux 上,我从 Java 中得到以下行为:
- 单击并按住,列表会下拉。
- 释放所需的项目,即可进行选择。
- 我也遇到了一些非常奇怪的行为,比如
- 组合框本身呈现在窗口中应有的位置,但我单击,列表从屏幕左上角下拉,或者
- 当我单击并按住时,列表会呈现在正确的位置,但随后我必须将鼠标拖动到左上角才能进行选择,就好像列表仍在从那里下拉一样。
- 进行选择的唯一方法是使用向上/向下箭头键,即使如此,我仍然可以从 (0, 0) 下拉列表。
在 Linux 上,我从 Jython 获得以下行为:
- 单击并按住,列表会下拉。
- 当我向下移动鼠标时,列表项会突出显示,但它们偏离了大约 1.5 行。即,直到鼠标位于第三个项目的中间时,第二个项目才会突出显示。
- 松开鼠标按钮,列表按其应有的方式消失,但原始项目仍处于选中状态,就好像什么也没发生一样。
- 同样,正确使用 ComboBox 的唯一方法是使用键盘,尽管我没有因为列表从错误的位置下拉而感到同样的奇怪。
这是我的 Java SSCCE:
package foo;
import javax.swing.*;
import java.awt.*;
public class Foo extends JPanel{
public Foo(){
super(new BorderLayout());
String[] contents = {"Foo", "Bar", "Baz"};
JComboBox combo = new JComboBox(contents);
add(combo, BorderLayout.PAGE_START);
}
private static void BuildAndShow(){
JFrame mainWindow = new JFrame("Testing");
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent contentPane = new Foo();
contentPane.setOpaque(true); //content panes must be opaque
mainWindow.setContentPane(contentPane);
//Display the window.
mainWindow.pack();
mainWindow.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BuildAndShow();
}
});
}
}
这是我的 Jython SSCCE:
from java.lang import Runnable
from javax.swing import SwingUtilities, JFrame, JComboBox
from java.awt import BorderLayout
import time
class Foo(Runnable):
def run(self):
mainWindow = JFrame('Test', defaultCloseOperation = JFrame.EXIT_ON_CLOSE)
mainWindow.setLayout(BorderLayout())
mainWindow.add(JComboBox(["Foo", "Bar", "Baz"]), BorderLayout.PAGE_START)
mainWindow.visible = True
if __name__ == "__main__":
app = Foo()
SwingUtilities.invokeLater(app)
while True:
time.sleep(5)
I'm writing a Jython application that will ultimately run on Linux. I develop on a Windows machine, and I'm getting inconsistent behaviour when I test on Linux.
- Dev machine- Windows XP, Java 6, Jython 2.5.2
- Test machine- Red Hat 3.4.3, Java 6, Jython 2.5.2
In Windows, using either Java or Jython, I get what I would consider to be 'normal' JComboBox behavior:
- Click on the text or arrow, the list drops down.
- Click on the desired item, the selection is made.
On Linux, I get the following behaviour from Java:
- Click and hold, the list drops down.
- Release on the desired item, the selection is made.
- I also get some really bizarre behaviour, like
- the ComboBox itself is rendered in the window where it should be, but I click and the list drops down from the top-left of the screen, or
- the list is rendered in the right place when I click and hold, but I then have to drag the mouse to the top left to make the selection, as if the list is still dropping down from there.
- The only way to make a selection is with the up/down arrow keys, and even with this I still get the list dropping down from (0, 0).
On Linux, I get the following behaviour from Jython:
- Click and hold, the list drops down.
- As I move the mouse downwards, the list items are highlighted, but they're off by about 1.5 lines. I.e, the second item isn't highlighted until the mouse is halfway down the third item.
- Release the mouse button, and the list disappears as it should, but the original item is still selected, as if nothing happened.
- Again, the only way to use the ComboBox properly is with the keyboard, although I don't get the same weirdness with the list dropping down from the wrong place.
Here is my Java SSCCE:
package foo;
import javax.swing.*;
import java.awt.*;
public class Foo extends JPanel{
public Foo(){
super(new BorderLayout());
String[] contents = {"Foo", "Bar", "Baz"};
JComboBox combo = new JComboBox(contents);
add(combo, BorderLayout.PAGE_START);
}
private static void BuildAndShow(){
JFrame mainWindow = new JFrame("Testing");
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent contentPane = new Foo();
contentPane.setOpaque(true); //content panes must be opaque
mainWindow.setContentPane(contentPane);
//Display the window.
mainWindow.pack();
mainWindow.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BuildAndShow();
}
});
}
}
And here is my Jython SSCCE:
from java.lang import Runnable
from javax.swing import SwingUtilities, JFrame, JComboBox
from java.awt import BorderLayout
import time
class Foo(Runnable):
def run(self):
mainWindow = JFrame('Test', defaultCloseOperation = JFrame.EXIT_ON_CLOSE)
mainWindow.setLayout(BorderLayout())
mainWindow.add(JComboBox(["Foo", "Bar", "Baz"]), BorderLayout.PAGE_START)
mainWindow.visible = True
if __name__ == "__main__":
app = Foo()
SwingUtilities.invokeLater(app)
while True:
time.sleep(5)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在最终实际物理访问我一直在远程测试的 Linux 机器后,一切工作正常。
事实证明,问题与 SSH、XMing 以及在 Linux 计算机上进行显示转发的任何设置的组合有关。
向那些花时间研究这个无法回答的问题的人致歉!
After finally actually getting physical access to the Linux machine I had been testing on remotely, everything worked fine.
So it turns out the problem was something to do with the combination of SSH, XMing, and whatever was set up to do the display forwarding on the Linux machine.
Apologies to anyone who spent time on what turned out to be an unanswerable question!