我正在学习从按钮开始使用摆动组件。单击其中一个按钮后,我正在尝试移动几个按钮。为了设置按钮的初始位置,我在主类的构造函数中有这样的代码:
setLayout(null);
b1 = new JButton("Host");
b1.setActionCommand("openServer");
b1.addActionListener(this);
b1.setBounds(300,10,200,30);
add(b1);
还有两个按钮。在主要方法I
frame.setContentPane(newContentPane);
其中NewContentPane被设置为主类的新对象。这一切都很好,很幸福。但当我点击按钮时,actionlistener就开始工作了。在其精细的探索中,它再次使用
b1.setBounds(-1,-1,0,0);
b3.setBounds(30,30,50,30);
或者不管怎样,隐藏b1,让b3出现在之前隐藏的地方。正如我所料,光靠这一点也无济于事。所以我试着重新粉刷框架或面板,但没有效果。所以我想,为什么不试试同样的老方法:setContentPane。它工作正常,按钮移动正常。但我得到了一堆错误如下:
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at javax.swing.JRootPane.setContentPane(JRootPane.java:620)
at javax.swing.JFrame.setContentPane(JFrame.java:693)
at packtion.ButtStallion.actionPerformed(ButtStallion.java:86)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
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:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
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)
我尝试了我能想到的一切,包括谷歌搜索。谷歌只提供了如何设置按钮初始位置的链接,但在程序运行时却找不到任何关于如何更改按钮位置的信息。
提前谢谢。
您正在尝试为内容窗格分配null。
但实际上这种方法并不好。您不应该调用setBounds(),而应该使用LayoutManager来更改按钮的约束。更改后呼叫
container.revalidate();
container.repaint();