当前位置: 技术问答>java相关
我想在applet里动态添加按钮,出现问题了
来源: 互联网 发布时间:2015-05-09
本文导语: button1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) {b.setText("sadfadf"); panel2.add(b,new FlowLayout()); //panel2.repaint(); } }); 当点击...
button1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e)
{b.setText("sadfadf");
panel2.add(b,new FlowLayout());
//panel2.repaint();
}
});
当点击这个按钮后,要最大化或者最小化之后才能看到添加德按钮b,而且出了一堆违例
Exception occurred during event dispatching:
java.lang.NullPointerException
at javax.swing.plaf.metal.MetalLookAndFeel.getControlDarkShadow(MetalLookAndFeel.java:1076)
at javax.swing.plaf.metal.MetalUtils.drawFlush3DBorder(MetalUtils.java:35)
at javax.swing.plaf.metal.MetalUtils.drawButtonBorder(MetalUtils.java:106)
at javax.swing.plaf.metal.MetalBorders$ButtonBorder.paintBorder(MetalBorders.java:70)
at javax.swing.border.CompoundBorder.paintBorder(CompoundBorder.java:95)
at javax.swing.JComponent.paintBorder(JComponent.java:549)
at javax.swing.AbstractButton.paintBorder(AbstractButton.java:1095)
at javax.swing.JComponent.paint(JComponent.java:688)
at javax.swing.JComponent._paintImmediately(JComponent.java:3829)
at javax.swing.JComponent.paintImmediately(JComponent.java:3672)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:370)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:124)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:154)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:337)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)
public void mouseClicked(MouseEvent e)
{b.setText("sadfadf");
panel2.add(b,new FlowLayout());
//panel2.repaint();
}
});
当点击这个按钮后,要最大化或者最小化之后才能看到添加德按钮b,而且出了一堆违例
Exception occurred during event dispatching:
java.lang.NullPointerException
at javax.swing.plaf.metal.MetalLookAndFeel.getControlDarkShadow(MetalLookAndFeel.java:1076)
at javax.swing.plaf.metal.MetalUtils.drawFlush3DBorder(MetalUtils.java:35)
at javax.swing.plaf.metal.MetalUtils.drawButtonBorder(MetalUtils.java:106)
at javax.swing.plaf.metal.MetalBorders$ButtonBorder.paintBorder(MetalBorders.java:70)
at javax.swing.border.CompoundBorder.paintBorder(CompoundBorder.java:95)
at javax.swing.JComponent.paintBorder(JComponent.java:549)
at javax.swing.AbstractButton.paintBorder(AbstractButton.java:1095)
at javax.swing.JComponent.paint(JComponent.java:688)
at javax.swing.JComponent._paintImmediately(JComponent.java:3829)
at javax.swing.JComponent.paintImmediately(JComponent.java:3672)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:370)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:124)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:154)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:337)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)
|
如果你要加新component到container中,可以调用
container.add(...);
container.invalidate();
container.validate();
container.add(...);
container.invalidate();
container.validate();