关于我的问题(可能是),我发现了另一种我无法从SwingWorker
线程中捕获和打印的异常类型。
如何生成RepaintManager
异常?
我通过 阅读了这个CheckThreadViolationRepaintManager
和这个方法,Alexander Potochkin
但似乎没有什么能解决我的问题。
关于我的问题(可能是),我发现了另一种我无法从SwingWorker
线程中捕获和打印的异常类型。
如何生成RepaintManager
异常?
我通过 阅读了这个CheckThreadViolationRepaintManager
和这个方法,Alexander Potochkin
但似乎没有什么能解决我的问题。
如果有帮助,下面的示例将打印以下的多个变体Exception
,主要用于框架的 UI 委托初始化的每个阶段。我用过CheckThreadViolationRepaintManager
,但变化看起来也很有趣。AspectJ
java.lang.异常 在 EDTViolation$CheckThreadViolationRepaintManager.checkThreadViolations(EDTViolation.java:43) 在 EDTViolation$CheckThreadViolationRepaintManager.addDirtyRegion(EDTViolation.java:37) 在 javax.swing.JComponent.repaint(JComponent.java:4734) 在 java.awt.Component.repaint(Component.java:3168) 在 javax.swing.JComponent.setFont(JComponent.java:2727) 在 javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:191) 在 javax.swing.plaf.basic.BasicPanelUI.installDefaults(BasicPanelUI.java:49) 在 javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:39) 在 javax.swing.JComponent.setUI(JComponent.java:662) 在 javax.swing.JPanel.setUI(JPanel.java:136) 在 javax.swing.JPanel.updateUI(JPanel.java:109) 在 javax.swing.JPanel.(JPanel.java:69) 在 javax.swing.JPanel.(JPanel.java:92) 在 javax.swing.JPanel.(JPanel.java:100) 在 javax.swing.JRootPane.createGlassPane(JRootPane.java:528) 在 javax.swing.JRootPane.(JRootPane.java:348) 在 javax.swing.JFrame.createRootPane(JFrame.java:255) 在 javax.swing.JFrame.frameInit(JFrame.java:236) 在 javax.swing.JFrame.(JFrame.java:159) 在 EDTViolation.main(EDTViolation.java:12) ...
import java.lang.ref.WeakReference;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.RepaintManager;
import javax.swing.SwingUtilities;
/** @see https://stackoverflow.com/questions/7787998 */
public class EDTViolation {
public static void main(String args[]) {
RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
private static class CheckThreadViolationRepaintManager extends RepaintManager {
//http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html
private boolean completeCheck = true;
private WeakReference<JComponent> lastComponent;
public CheckThreadViolationRepaintManager(boolean completeCheck) {
this.completeCheck = completeCheck;
}
public CheckThreadViolationRepaintManager() {
this(true);
}
public boolean isCompleteCheck() {
return completeCheck;
}
public void setCompleteCheck(boolean completeCheck) {
this.completeCheck = completeCheck;
}
@Override
public synchronized void addInvalidComponent(JComponent component) {
checkThreadViolations(component);
super.addInvalidComponent(component);
}
@Override
public void addDirtyRegion(JComponent component, int x, int y, int w, int h) {
checkThreadViolations(component);
super.addDirtyRegion(component, x, y, w, h);
}
private void checkThreadViolations(JComponent c) {
if (!SwingUtilities.isEventDispatchThread() && (completeCheck || c.isShowing())) {
boolean repaint = false;
boolean fromSwing = false;
boolean imageUpdate = false;
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
for (StackTraceElement st : stackTrace) {
if (repaint && st.getClassName().startsWith("javax.swing.")
&& // for details see
// https://swinghelper.dev.java.net/issues/show_bug.cgi?id=1
!st.getClassName().startsWith("javax.swing.SwingWorker")) {
fromSwing = true;
}
if (repaint && "imageUpdate".equals(st.getMethodName())) {
imageUpdate = true;
}
if ("repaint".equals(st.getMethodName())) {
repaint = true;
fromSwing = false;
}
if ("read".equals(st.getMethodName()) && "javax.swing.JEditorPane".equals(st.getClassName())) {
// Swing reads html from a background thread
return;
}
}
if (imageUpdate) {
//assuming it is java.awt.image.ImageObserver.imageUpdate(...)
//image was asynchronously updated, that's ok
return;
}
if (repaint && !fromSwing) {
//no problems here, since repaint() is thread safe
return;
}
//ignore the last processed component
if (lastComponent != null && c == lastComponent.get()) {
return;
}
lastComponent = new WeakReference<JComponent>(c);
violationFound(c, stackTrace);
}
}
protected void violationFound(JComponent c, StackTraceElement[] stackTrace) {
System.out.println();
System.out.println("EDT violation detected");
System.out.println(c);
for (StackTraceElement st : stackTrace) {
System.out.println("\tat " + st);
}
}
}
}
创建自己的异常
class RepaintManagerException extends Exception
{
String msg;
RepaintManagerException()
{
msg = new String("type message here");
}
}
用法
public class My_Exception
{
public static void main (String args [ ])
{
try
{
// your code
if (expression) throw new RepaintManagerException( );
}
catch (RepaintManagerException e)
{
System.out.println (e);
}
}
}