gpt4 book ai didi

java - Java TrayIcon.setToolTip()使具有128个字符的String上的JVM崩溃

转载 作者:行者123 更新时间:2023-12-03 17:08:17 26 4
gpt4 key购买 nike

在运行mp3播放器时,它在某些歌曲上崩溃了。
考虑到这一点,当我用str.length()== 128调用setToolTip(str)时,它不是播放器本身,而是java TrayIcon类。

我写了一个简短的例子来证明会发生什么:

package jc.javaexceptions;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;



public class Tray_setToolTip {

// testing switches

// if set to true, shows chrash screen
// if set to false, app crashes quietly
static private final boolean SHOW_GUI_FAIL = true;

// does not play a role if run in- or outside EDT
static private final boolean RUN_IN_SWING_DISPATCH_THREAD = true;

/**
* Demonstrates, how setting a 128-char ToolTipText to a TrayIcon will crash the JVM
*/
public static void main(final String[] args) throws AWTException, InvocationTargetException, InterruptedException {
if (SHOW_GUI_FAIL) {
final JFrame f = new JFrame("Test Window, fails at 128 chars ToolTipText length");
f.setBounds(200, 200, 400, 400);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setVisible(true);
}

// create tray image
final int SIZE = 16;
final BufferedImage bi = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
final Graphics g = bi.getGraphics();
g.setColor(Color.CYAN);
g.fillRect(0, 0, SIZE, SIZE);
g.setColor(Color.RED);
g.drawLine(0, 0, SIZE, SIZE);
g.drawLine(0, SIZE, SIZE, 0);

// set up tray and tray icon
final TrayIcon trayIcon = new TrayIcon(bi);
final SystemTray tray = SystemTray.getSystemTray();
trayIcon.addMouseListener(new MouseAdapter() {
@Override public void mouseClicked(final MouseEvent pE) {
tray.remove(trayIcon);
System.exit(0);
}
});
tray.add(trayIcon);

// works fine
System.out.println("1 OK");
trayIcon.setToolTip("Funny Test!"); // -> OK

setTTT(trayIcon, 127); // try 127 -> OK
setTTT(trayIcon, 129); // try 129 -> OK
setTTT(trayIcon, 128); // try 128 -> CRASH // <--------------------------- CRASH

// will not be reached!
System.out.println("End reached!");
}

static private void setTTT(final TrayIcon pTrayIcon, final int pStringLength) throws InvocationTargetException, InterruptedException {
System.out.println("Tray_setToolTip.setTTT(" + pStringLength + ")");

// construct bad-ass string
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < pStringLength; i++) {
sb.append("1");
}
final String str = sb.toString();
System.out.println("\tString len=" + str.length());

if (RUN_IN_SWING_DISPATCH_THREAD) {
// SwingUtilities.invokeLater(() -> pTrayIcon.setToolTip(str)); // cannot use invokeLater(), would allow the output of "End reached!"
SwingUtilities.invokeAndWait(() -> pTrayIcon.setToolTip(str));
} else {
pTrayIcon.setToolTip(str);
}
System.out.println("\tOK");

try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}

}

太...这在您的PC上也会发生吗?
还是只是我的?
我在最新的Windows 7上运行Oracle jdk-8u40-windows-x64 ...

如果发现这是Java固有的错误,我应该如何进一步进行?

最佳答案

是的。已修复。如果我的Eclipse停留在Java的旧版本上,则当前版本(1.8.0_151-b12)可以毫无问题地运行它。

傻面

关于java - Java TrayIcon.setToolTip()使具有128个字符的String上的JVM崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46897212/

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