📄 ui.java
字号:
Component[] cc = ((Container) c).getComponents(); for (int i=0; i<cc.length; i++) { dumpComponent (cc[i]); } } } private JFrame initFrame (Element ui) { Element caption = ui.getChild ("caption"); mainFrame = caption == null ? new JFrame () : new JFrame (caption.getText()); JOptionPane.setRootFrame (mainFrame); mainFrame.getContentPane().setLayout(new BorderLayout()); String close = ui.getAttributeValue ("close"); if ("false".equals (close)) mainFrame.setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE); else if ("exit".equals (close)) mainFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); mainFrame.setSize(getDimension (ui, screenSize)); locateOnScreen (mainFrame); return mainFrame; } private void locateOnScreen(Frame frame) { Dimension paneSize = frame.getSize(); Dimension screenSize = frame.getToolkit().getScreenSize(); frame.setLocation( (screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2); } private JMenuBar buildMenuBar (Element ui) { JMenuBar mb = new JMenuBar (); Iterator iter = ui.getChildren ("menu").iterator(); while (iter.hasNext()) mb.add (menu ((Element) iter.next())); return mb; } private JMenu menu (Element m) { JMenu menu = new JMenu (m.getAttributeValue ("id")); setItemAttributes (menu, m); Iterator iter = m.getChildren ().iterator(); while (iter.hasNext()) addMenuItem (menu, (Element) iter.next()); return menu; } private void addMenuItem (JMenu menu, Element m) { String tag = m.getName (); if ("menuitem".equals (tag)) { JMenuItem item = new JMenuItem (m.getAttributeValue ("id")); setItemAttributes (item, m); menu.add (item); } else if ("menuseparator".equals (tag)) { menu.addSeparator (); } else if ("button-group".equals (tag)) { addButtonGroup (menu, m); } else if ("check-box".equals (tag)) { JCheckBoxMenuItem item = new JCheckBoxMenuItem ( m.getAttributeValue ("id") ); setItemAttributes (item, m); item.setState ( "true".equals (m.getAttributeValue ("state")) ); menu.add (item); } else if ("menu".equals (tag)) { menu.add (menu (m)); } } private void addButtonGroup (JMenu menu, Element m) { ButtonGroup group = new ButtonGroup(); Iterator iter = m.getChildren ("radio-button").iterator(); while (iter.hasNext()) { addRadioButton (menu, group, (Element) iter.next()); } } private void addRadioButton (JMenu menu, ButtonGroup group, Element m) { JRadioButtonMenuItem item = new JRadioButtonMenuItem (m.getAttributeValue ("id")); setItemAttributes (item, m); item.setSelected ( "true".equals (m.getAttributeValue ("selected")) ); group.add (item); menu.add (item); } private Dimension getDimension (Element e, Dimension def) { String w = e.getAttributeValue ("width"); String h = e.getAttributeValue ("height"); return new Dimension ( w != null ? Integer.parseInt (w) : def.width, h != null ? Integer.parseInt (h) : def.height ); } private void setItemAttributes (AbstractButton b, Element e) { String s = e.getAttributeValue ("accesskey"); if (s != null && s.length() == 1) b.setMnemonic (s.charAt(0)); String icon = e.getAttributeValue ("icon"); if (icon != null) { try { b.setIcon (new ImageIcon (new URL (icon))); } catch (MalformedURLException ex) { ex.printStackTrace (); } } b.setActionCommand (e.getAttributeValue ("command")); String actionId = e.getAttributeValue ("action"); if (actionId != null) { b.addActionListener ((ActionListener) get (actionId)); } } protected void setLookAndFeel (Element ui) { String laf = ui.getAttributeValue ("look-and-feel"); if (laf != null) { try { UIManager.setLookAndFeel (laf); } catch (Exception e) { warn (e); }; } } private JComponent createComponent (Element e) { if (e == null) return new JPanel (); JComponent component; UIFactory factory = null; String clazz = e.getAttributeValue ("class"); if (clazz == null) clazz = (String) mapping.get (e.getName()); if (clazz == null) { try { clazz = classMapping.getString (e.getName()); } catch (MissingResourceException ex) { // no class attribute, no mapping // let MBeanServer do the yelling } } try { if (clazz == null) factory = this; else factory = (UIFactory) objFactory.newInstance (clazz.trim()); component = factory.create (this, e); setSize (component, e); if (component instanceof AbstractButton) { AbstractButton b = (AbstractButton) component; b.setActionCommand (e.getAttributeValue ("command")); String actionId = e.getAttributeValue ("action"); if (actionId != null) { b.addActionListener ((ActionListener) get (actionId)); } } put (component, e); Element script = e.getChild ("script"); if (script != null) component = doScript (component, script); if ("true".equals (e.getAttributeValue ("scrollable"))) component = new JScrollPane (component); } catch (Exception ex) { ex.printStackTrace (); warn ("Error instantiating class " + clazz); warn (ex); component = new JLabel ("Error instantiating class " + clazz); } return component; } protected JComponent doScript (JComponent component, Element e) { return component; } private void setSize (JComponent c, Element e) { String w = e.getAttributeValue ("width"); String h = e.getAttributeValue ("height"); Dimension d = c.getPreferredSize (); double dw = d.getWidth (); double dh = d.getHeight (); if (w != null) dw = Double.parseDouble (w); if (h != null) dh = Double.parseDouble (h); if (w != null || h != null) { d.setSize (dw, dh); c.setPreferredSize (d); } } public JComponent create (Element e) { JComponent component = null; Iterator iter = e.getChildren().iterator(); for (int i=0; iter.hasNext(); i++) { JComponent c = createComponent((Element) iter.next ()); if (i == 0) component = c; else if (i == 1) { JPanel p = new JPanel (); p.add (component); p.add (c); component = p; put (component, e); } else { component.add (c); } } return component; } public JFrame getMainFrame() { return mainFrame; } private void createObjects (Element e, String name) { Iterator iter = e.getChildren (name).iterator (); while (iter.hasNext()) { try { Element ee = (Element) iter.next (); String clazz = ee.getAttributeValue ("class"); Object obj = objFactory.newInstance (clazz.trim()); if (obj instanceof UIAware) { ((UIAware) obj).setUI (this, ee); } put (obj, ee); } catch (Exception ex) { warn (ex); } } } private void createMappings (Element e) { Iterator iter = e.getChildren ("mapping").iterator (); while (iter.hasNext()) { try { Element ee = (Element) iter.next (); String name = ee.getAttributeValue ("name"); String clazz = ee.getAttributeValue ("factory"); mapping.put (name, clazz); } catch (Exception ex) { warn (ex); } } } protected void warn (Object obj) { if (log != null) log.warn (obj); } private void put (Object obj, Element e) { String id = e.getAttributeValue ("id"); if (id != null) { registrar.put (id, obj); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -