0

我正在学习使用从按钮开始的摆动组件。单击其中一个按钮后,我试图让一些按钮移动。要设置按钮的初始位置,我在主类的构造函数中有这样的代码:

    setLayout(null);
    b1 = new JButton("Host");
    b1.setActionCommand("openServer");
    b1.addActionListener(this);
    b1.setBounds(300,10,200,30);
    add(b1);

还有两个按钮。在主要方法中我

frame.setContentPane(newContentPane);

其中 NewContentPane 被设置为主类的新对象。这一切都很好,很漂亮。但是然后我单击按钮,动作侦听器开始工作。在它的美好追求中,它再次使用

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)

我尝试了所有我能想到的方法,包括谷歌搜索。谷歌只是提供了如何设置按钮初始位置的链接,在程序运行时找不到任何关于如何更改其位置的信息。

提前致谢。

4

1 回答 1

2

您正在尝试将 null 分配给内容窗格。

但实际上这种方法并不好。您不应该调用 setBounds() 而是使用 LayoutManager 来更改按钮的约束。变更电话后

container.revalidate();
container.repaint();
于 2013-02-19T09:41:16.843 回答