demo.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 629 行 · 第 1/2 页

JAVA
629
字号
  private static JScrollPane mkScrollPane(JComponent inner)  {    JScrollPane jsp;    jsp = new JScrollPane(inner,			  JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 			  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);        return jsp;  }  public Demo()  {    frame = new JFrame("Swing Activity Board");    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    frame.setJMenuBar(mkMenuBar());    JComponent component = (JComponent) frame.getContentPane();    component.setLayout(new BorderLayout());    component.add(mkToolBar(), BorderLayout.NORTH);    JPanel main = new JPanel();    main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));    desktop = createDesktop();    main.add(desktop);    main.add(mkButtonBar());    component.add(main, BorderLayout.CENTER);    frame.pack();    frame.show();  }  public static class LaterMain    implements Runnable  {    public void run()    {      Demo demo = new Demo();          }  }  public static void main(String args[])  {    SwingUtilities.invokeLater(new LaterMain());  }  private static JButton mkBigButton(String title)  {    JButton b = new JButton(title);    b.setMargin(new Insets(5,5,5,5));    return b;  }  private static JPanel mkPanel(JComponent[] inners)  {    JPanel p = new JPanel();    for (int i = 0; i < inners.length; ++i)      {        p.add(inners[i]);      }    return p;  }  static JButton mkDisposerButton(final JFrame c)  {    JButton close = mkBigButton("Close");    close.addActionListener(new ActionListener()      {        public void actionPerformed(ActionEvent e)        {          c.dispose();        }      });    return close;  }  public static JColorChooser mkColorChooser()  {    return new JColorChooser();  }  /**   * This action brings up a new Window with the specified content.   */  private class PopupAction    extends AbstractAction  {    /**     * The component to be shown.     */    private DemoFactory demoFactory;    /**     * Creates a new PopupAction with the specified name and showing the     * component created by the specified DemoFactory when activated.     *     * @param n the name of the action     * @param factory the demo factory     */    PopupAction(String n, DemoFactory factory)    {      putValue(NAME, n);      demoFactory = factory;    }    /**     * Brings up the new window showing the component stored in the     * constructor.     *     * @param e the action event that triggered the action     */    public void actionPerformed(ActionEvent e)    {      JInternalFrame frame = new JInternalFrame((String) getValue(NAME));      frame.setClosable(true);      frame.setIconifiable(true);      frame.setMaximizable(true);      frame.setResizable(true);      frame.setContentPane(demoFactory.createDemo());      frame.pack();      desktop.add(frame);      frame.setVisible(true);    }  }  /**   * Create the tree.   *    * @return thr scroll pane, containing the tree.   */  private static JComponent mkTree()  {    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root node");        addChildren("Node", root, 12);    JTree tree = new JTree(root);    tree.setLargeModel(true);    DefaultTreeSelectionModel dtsm = new DefaultTreeSelectionModel();    dtsm.setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);    tree.setSelectionModel(dtsm);        // Make it editable.    tree.setEditable(true);        JComponent t = mkScrollPane(tree);    t.setPreferredSize(new Dimension(200,200));    return t;  }    /**   * Add the specified number of children to this parent node. For each   * child, the method is called recursively adding the nChildren-3 number of    * grandchildren.   *    * @param parent the parent node   * @param nChildren the number of children   */  private static void addChildren(String name, DefaultMutableTreeNode parent,                                                int nChildren)  {    for (int i = 0; i < nChildren; i++)      {        String child_name = parent+"."+i;        DefaultMutableTreeNode child = new DefaultMutableTreeNode         (child_name);        parent.add(child);        addChildren(child_name, child, nChildren-3);      }  }    private JPanel mkButtonBar()  {        JPanel panel = new JPanel(new GridLayout(3, 1, 5, 5));    panel.add(new JButton(new PopupAction("Buttons",                                          ButtonDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("Slider",                                          SliderDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("ProgressBar",                                        ProgressBarDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("ScrollBar",                                          ScrollBarDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("Spinner",                                          SpinnerDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("TextField",                                          TextFieldDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("TextArea",                                          TextAreaDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("FileChooser",                                        FileChooserDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("ComboBox",                                          ComboBoxDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("Table",                                          TableDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("List",                                          ListDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("TabbedPane",                                         TabbedPaneDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("Tree",                                          TreeDemo.createDemoFactory())));    panel.add(new JButton(new PopupAction("Theme Editor",                                       MetalThemeEditor.createDemoFactory())));    JButton exitDisposer = mkDisposerButton(frame);    panel.add(exitDisposer);        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE,                                        panel.getPreferredSize().height));    exitDisposer.addActionListener(new ActionListener()      {	public void actionPerformed(ActionEvent e)	{	  System.exit(1);	}      });    return panel;  }  /**   * Creates and returns the main desktop.   *   * @return the main desktop   */  private JDesktopPane createDesktop()  {    JDesktopPane d = new DemoDesktop();    d.setPreferredSize(new Dimension(900, 500));    return d;  }  /**   * This Action is used to switch Metal themes.   */  class ChangeThemeAction extends AbstractAction  {    /**     * The theme to switch to.     */    MetalTheme theme;    /**     * Creates a new ChangeThemeAction for the specified theme.     *     * @param t the theme to switch to     */    ChangeThemeAction(MetalTheme t)    {      theme = t;      putValue(NAME, t.getName());    }    /**     * Changes the theme to the one specified in the constructor.     *     * @param event the action event that triggered this action     */    public void actionPerformed(ActionEvent event)    {      MetalLookAndFeel.setCurrentTheme(theme);      try        {          // Only switch theme if we have a metal L&F. It is still necessary          // to install a new MetalLookAndFeel instance.          if (UIManager.getLookAndFeel() instanceof MetalLookAndFeel)            UIManager.setLookAndFeel(new MetalLookAndFeel());        }      catch (UnsupportedLookAndFeelException ex)        {          ex.printStackTrace();        }      SwingUtilities.updateComponentTreeUI(frame);    }      }  /**   * This Action is used to switch Metal themes.   */  class ChangeLAFAction extends AbstractAction  {    /**     * The theme to switch to.     */    private UIManager.LookAndFeelInfo laf;    /**     * Creates a new ChangeLAFAction for the specified L&F.     *     * @param l the L&F to switch to     */    ChangeLAFAction(UIManager.LookAndFeelInfo l)    {      laf = l;      putValue(NAME, laf.getName());    }    /**     * Changes the theme to the one specified in the constructor.     *     * @param event the action event that triggered this action     */    public void actionPerformed(ActionEvent event)    {      try        {          UIManager.setLookAndFeel(laf.getClassName());        }      catch (Exception ex)        {          ex.printStackTrace();        }      SwingUtilities.updateComponentTreeUI(frame);      themesMenu.setEnabled(laf.getClassName()                           .equals("javax.swing.plaf.metal.MetalLookAndFeel"));    }      }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?