1.Exception in thread "AWT
2.å¦ä½ç¨SwingBuilder使ç¨èªå®ä¹çswingç»ä»¶
Exception in thread "AWT
çæ¥åæ¯è¿å°æ¹çé误
at resourceInfoManage.FCJFrame.jIndexTreeValueChanged(FCJFrame.java:) at resourceInfoManage.FCJFrame.access$0(FCJFrame.java:) at
å¦ä½ç¨SwingBuilder使ç¨èªå®ä¹çswingç»ä»¶
groovyå·²ç»å¨SwingBuilderé为æ们é¢è®¾äºå¾å¤swingç»ä»¶ï¼ä»¥ä½¿æ们å¯ä»¥ç¨ä¸é¢è¿æ ·çå¿«æ·å½¢å¼æ¥æ建çé¢ï¼
swingBuilder.frame(
//å±æ§é
){
//åç»ä»¶
}
对äºä» ä» ä½¿ç¨swingçåçç»ä»¶ï¼è¿æ²¡æä»»ä½é®é¢ï¼ä½å¨å®é å¼åä¸å¯è½ä¼éå°éè¦ä½¿ç¨å¯¹åçç»ä»¶æ©å±åçèªå®ä¹ç»ä»¶ï¼å¦ä¸ä¸ªç»§æ¿äºJPanelçMyPanelç±»ï¼çæ åµï¼èè¿ä¸ªç±»ææ¾ä¸å¯è½åºç°å¨SwingBuilderçé¢è®¾å表éï¼é£è¿ç§æ åµä¸æ¯ä¸æ¯å°±æå³çä¸å¯ä»¥ä½¿ç¨groovy为æ们æä¾ç便å©ï¼åªè½ç¨åä¼ ç»çjavaå½¢å¼å»æ建çé¢å¢ï¼
çæ¡æ¯å¦å®çãSwingBuilderè½ç¶ä¸ä¼é¢è®¾ç¨æ·èªå®ä¹çç»ä»¶ï¼ä½å®æä¾äºå 个æ¥å£å¯ä»¥è®©ç¨æ·æèªå®ä¹çç»ä»¶è®¾ç½®è¿å»ï¼è¿æ ·å°±ä¾ç¶å¯ä»¥ç¨groovyçå¿«æ·å½¢å¼æ¥æ建çé¢äºãè¿å 个æ¥å£åå«æ¯ï¼
public void registerFactory(String name,中华万年历 源码 Factory factory)
public void registerBeanFactory(String theName, Class beanClass)
registerFactory()
registerFactoryæ¹æ³å¯ä»¥æ³¨åä¸ä¸ªèªå®ä¹ç»ä»¶ï¼nameæå®äºéè¿swingBuilderæ建ç»ä»¶çå称ï¼å¦JFrameå¨swingBuilderéé¢è®¾çnameæ¯"frame"ï¼è¿å°±ä½¿å¾æ们å¯ä»¥éè¿swingBuilder.frame()æ建ä¸ä¸ªJFrameã
第äºä¸ªåæ°æ¥æ¶ä¸ä¸ªå®ç°äºFactoryæ¥å£çç±»ï¼è¿ä¸ªç±»éææï¼å®ç°ï¼å¦ä½å®ä¾åèªå®ä¹ç»ä»¶ï¼newInstanceï¼ï¼å¦ä½å¤çaddè¿æ¥çåç»ä»¶ï¼setChildï¼çä¸ç³»åçç¥ãé常并ä¸éè¦ç´æ¥å®ç°Factoryæ¥å£ï¼groovy为æ们æä¾äºä¸ä¸ªæ½è±¡ç±»ï¼AbstractFactoryï¼ï¼æ们åºä¼å 继æ¿è¿ä¸ªç±»ï¼ç¶ååæéæç¸åºçæ¹æ³è¿è¡éåãä¸é¢æ¥çä¸ä¸ªå¦ä½ä½¿ç¨è¿ä¸ªæ¥å£çä¾åï¼
/
*** èªå®ä¹äºä¸ä¸ªå¯ä»¥æ¾ç¤ºèæ¯å¾ççé¢æ¿
* @author keenlight
*
*/
class ImagePanel extends JPanel{
/
***
*/
private static final long serialVersionUID = 1L
private BufferedImage image
public ImagePanel(BufferedImage image){
super()
this.image = image
}
public ImagePanel(BufferedImage image, LayoutManager layout) {
super(layout)
this.image = image
}
public void paintComponent(Graphics g)
{
super.paintComponent(g)
if(image != null){
g.drawImage(image, 0, 0, this)
}
}
}
/
*** ImagePanelçFactory
* @author keenlight
*/
class ImagePanelFactory extends AbstractFactory{
private BufferedImage image
ImagePanelFactory(BufferedImage image){
this.image = image
}
@Override
public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
def layout = properties.remove("layout")
new ImagePanel(image, layout)
}
public void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
if (!(child instanceof Component) || (child instanceof Window)) {
return
}
parent = parent as ImagePanel
try {
def constraints = builder.context.constraints
if (constraints != null) {
parent.add(child, constraints)
if (child instanceof JComponent) {
child.putClientProperty(LayoutFactory.DEFAULT_DELEGATE_PROPERTY_CONSTRAINT, constraints)
}
builder.context.remove('constraints')
} else {
parent.add(child)
}
} catch (MissingPropertyException mpe) {
parent.add(child)
}
}
}
å®ä¹å¥½è¿äºç±»åå¦ä½ä½¿å ¶çæï¼åªéä¸æ¥ï¼
def bgImage = ImageIO.read(new File(image_path))
swingBuilder.registerFactory("imagePanel", new ImagePanelFactory(bgImage))
注å好ä¹åï¼ImagePanelç»ä»¶ä¾¿å¯åå ¶ä»åçç»ä»¶ä¸æ ·ä½¿ç¨äºï¼å¦ä¸æçJFrameä¸æ ·ï¼ã
registerBeanFactory()
registerBeanFactoryæ¯registerFactoryçä¸ç§ä¾¿å©å½¢å¼ï¼è°ç¨æ¤æ¥å£æ éæä¾Factoryï¼åªéæä¾èªå®ä¹ç±»çç±»åå³å¯ãæ¯å¦åçç»ä»¶éçJPanelå°±æ¯ç¨è¿ä¸ªæ¥å£æ³¨åçï¼SwingBuilderç±»ä¸æ³¨åJPanelçæºç å¦ä¸ï¼
registerBeanFactory("panel", JPanel)
è¿æ ·å°±å¯ä»¥æJPanel注å为"panel"æ¥ä½¿ç¨äºãå ¶å®registerBeanFactoryè¿ä¸ªæ¹æ³éé¢ä¸æ ·ä¹å®ä¾åäºä¸ä¸ªç»§æ¿äºAbstractFactoryç±»ï¼å®ä¼æè°ç¨è¿ä¸ªæ¹æ³æ³¨åçç»ä»¶å½åä¸ä¸ªjavaBeanï¼æ¹æ³åå·²ç»å¾ææ¾äºï¼ï¼æ以å®å¨ç»§æ¿AbstractFactoryç±»æ¶éåçnewInstanceæ¹æ³éæ¯ç´æ¥è¿åè°ç¨class.newInstance()çå®ä¾åç»æï¼classç±ç¬¬äºä¸ªå ¥åæä¾ãæ以对äºé£äºå¨å®ä¾åæ¶è°ç¨é»è®¤æé å¨å°±è¶³å¤çç»ä»¶ï¼ç¨registerBeanFactory()æ¹æ³æ¥æ³¨åä¼æ´ä¸ºä¾¿å©ãå½ç¶ï¼å¯¹äºæä¸é¢å®ä¹çImagePanelæ¥è¯´å°±ä¸è¡äºï¼å 为ImagePanelç±»å¨å®ä¾åæ¶éè¦æ¥åä¸ä¸ªå¾ç对象ï¼æä»¥å¿ é¡»ä½¿ç¨ç¥éº»ç¦ä¸ç¹çregisterFactory()ã
2024-12-24 10:54
2024-12-24 10:44
2024-12-24 10:16
2024-12-24 09:55
2024-12-24 09:43
2024-12-24 09:22
2024-12-24 09:11
2024-12-24 09:03