我正在学习从按钮开始使用摇摆组件。点击其中一个按钮后,我试图移动几个按钮。要设置按钮的初始位置,我在主类的构造函数中有这样的代码:
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)
我尝试了我能想到的一切,包括谷歌搜索。 Google只提供了如何设置按钮初始位置的链接,在程序运行时无法找到有关如何更改其位置的任何内容。
提前致谢。
答案 0 :(得分:2)
您正尝试将null分配给内容窗格。
但实际上这种方法并不好。您不应该调用setBounds(),而是使用LayoutManager而不是更改按钮的约束。更改后调用
container.revalidate();
container.repaint();