示例#1
0
 public static void createAndShowGUI() {
   try {
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
   } catch (ClassNotFoundException
       | InstantiationException
       | IllegalAccessException
       | UnsupportedLookAndFeelException ex) {
     ex.printStackTrace();
   }
   JFrame frame = new JFrame("@title@");
   frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   frame.getContentPane().add(new MainPanel());
   frame.pack();
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);
 }
示例#2
0
  public static void main(String args[]) throws Exception {
    final JFrame frame = new JFrame();
    final JTextArea textArea = new JTextArea(30, 60);
    final JScrollPane scrollPane = new JScrollPane(textArea);
    frame.getContentPane().add(scrollPane);

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    JMenu menu = new JMenu("File");
    ScreenCapture.createImage(menu, "menu.jpg");
    menuBar.add(menu);
    JMenuItem menuItem = new JMenuItem("Frame Image");
    menu.add(menuItem);
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            //  Let the menu close and repaint itself before taking the image

            new Thread() {
              public void run() {
                try {
                  Thread.sleep(50);
                  System.out.println("Creating frame.jpg");
                  frame.repaint();
                  ScreenCapture.createImage(frame, "frame.jpg");
                } catch (Exception exc) {
                  System.out.println(exc);
                }
              }
            }.start();
          };
        });

    final JButton button = new JButton("Create Images");
    button.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            try {
              System.out.println("Creating desktop.jpg");
              ScreenCapture.createDesktopImage("desktop.jpg");
              System.out.println("Creating frame.jpg");
              ScreenCapture.createImage(frame, "frame.jpg");
              System.out.println("Creating scrollpane.jpg");
              ScreenCapture.createImage(scrollPane, "scrollpane.jpg");
              System.out.println("Creating textarea.jpg");
              ScreenCapture.createImage(textArea, "textarea.jpg");
              System.out.println("Creating button.jpg");
              ScreenCapture.createImage(button, "button.jpg");
              button.setText("button refreshed");
              button.paintImmediately(button.getBounds());
              System.out.println("Creating refresh.jpg");
              ScreenCapture.createImage(button, "refresh.jpg");
              System.out.println("Creating region.jpg");
              Rectangle r = new Rectangle(0, 0, 100, 16);
              ScreenCapture.createImage(textArea, r, "region.png");
            } catch (Exception exc) {
              System.out.println(exc);
            }
          }
        });
    frame.getContentPane().add(button, BorderLayout.SOUTH);

    try {
      FileReader fr = new FileReader("ScreenCapture.java");
      BufferedReader br = new BufferedReader(fr);
      textArea.read(br, null);
      br.close();
    } catch (Exception e) {
    }

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }