gpt4 book ai didi

java - JToolBar IllegalArgumentException 放回 GridBagLayout 时

转载 作者:搜寻专家 更新时间:2023-11-01 02:09:18 38 4
gpt4 key购买 nike

当工具栏被拖离 GUI 然后关闭(将其返回到 GUI)时,为什么这段代码会抛出 IllegalArgumentException

我能理解为什么在没有约束的情况下添加组件可能是不合适的,但在这种情况下,最初将工具栏添加到面板(使用 GridBagLayout)而没有约束导致没有这样的问题。为什么它会在第二次和后续添加时出现?

代码改编自this answer , 但两个代码都显示相同的问题。

代码

import java.awt.*;
import javax.swing.*;

public class GridBagToolBarOddity {

public static void main(String[] args) {
Runnable r = new Runnable() {

@Override
public void run() {
JPanel gui = new JPanel(new GridBagLayout());
JToolBar tb = new JToolBar();
tb.add(new JLabel("Drag me off, then drop me back!"));
gui.add(tb);

gui.setPreferredSize(new Dimension(300, 100));
gui.setBackground(Color.RED);

JFrame f = new JFrame("Demo");
f.add(gui);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.pack();
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

异常

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: 
cannot add to layout: constraints must be a GridBagConstraint
at java.awt.GridBagLayout.addLayoutComponent(GridBagLayout.java:702)
at java.awt.Container.addImpl(Container.java:1120)
at java.awt.Container.add(Container.java:966)
at javax.swing.plaf.basic.BasicToolBarUI$FrameListener.windowClosing(BasicToolBarUI.java:1265)
at java.awt.Window.processWindowEvent(Window.java:2051)
at javax.swing.JDialog.processWindowEvent(JDialog.java:681)
at java.awt.Window.processEvent(Window.java:2009)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

最佳答案

这是 BasicToolBarUI 源代码的片段

    public void windowClosing(WindowEvent w) {
if (toolBar.isFloatable()) {
if (dragWindow != null)
dragWindow.setVisible(false);
floating = false;
if (floatingToolBar == null)
floatingToolBar = createFloatingWindow(toolBar);
if (floatingToolBar instanceof Window) ((Window)floatingToolBar).setVisible(false);
floatingToolBar.getContentPane().remove(toolBar);
String constraint = constraintBeforeFloating;
if (toolBar.getOrientation() == JToolBar.HORIZONTAL) {
if (constraint == "West" || constraint == "East") {
constraint = "North";
}
} else {
if (constraint == "North" || constraint == "South") {
constraint = "West";
}
}
if (dockingSource == null)
dockingSource = toolBar.getParent();
if (propertyListener != null)
UIManager.removePropertyChangeListener(propertyListener);
dockingSource.add(toolBar, constraint);

如您所见,它尝试将 constraintBeforeFloating String 作为约束传递,但 GridBagLayout 需要 GridBagConstraints。

constraintBeforeFloating = calculateConstraint();

private String calculateConstraint() {
String constraint = null;
LayoutManager lm = dockingSource.getLayout();
if (lm instanceof BorderLayout) {
constraint = (String)((BorderLayout)lm).getConstraints(toolBar);
}
return (constraint != null) ? constraint : constraintBeforeFloating;
}

因此,当 float 关闭时,UI 尝试将当前约束(字符串)传递给 GridBagLayout 并失败。

关于java - JToolBar IllegalArgumentException 放回 GridBagLayout 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21559552/

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