Java Tutorial/Swing/GlassPane

Материал из Java эксперт
Перейти к: навигация, поиск

Block mouse and key events in an application

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
public class Main extends JFrame {
  JPanel glass = new JPanel(new GridLayout(0, 1));
  JLabel padding = new JLabel();
  JProgressBar waiter = new JProgressBar(0, 100);

  public Main() {
    setSize(300, 300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel controlPane = new JPanel(new GridLayout(2, 1));
    controlPane.setOpaque(false);
    controlPane.add(new JLabel("Please wait..."));
    controlPane.add(waiter);
    glass.setOpaque(false);
    glass.add(padding);
    glass.add(new JLabel());
    glass.add(controlPane);
    glass.add(new JLabel());
    glass.add(new JLabel());
    glass.setSize(new Dimension(300,300));
    setGlassPane(glass);

    JButton startB = new JButton("Start!");
    startB.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent A) {
        glass.setVisible(true);
        padding.requestFocus();
      }
    });
    Container contentPane = getContentPane();
    contentPane.add(startB, BorderLayout.SOUTH);
  }
  public static void main(String[] args) {
    Main ge = new Main();
    ge.setVisible(true);
  }
}





Paint on glass pane

import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Main extends JComponent {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    final JButton activate = new JButton("Show");
    frame.add(activate);
    frame.pack();
    frame.setVisible(true);
    final Main glass = new Main(frame);
    frame.setGlassPane(glass);
    activate.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        glass.setVisible(true);
      }
    });
  }
  JFrame frame;
  Point cursor;
  public Main(JFrame frame) {
    this.frame = frame;
    cursor = new Point();
    this.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseMoved(MouseEvent evt) {
        cursor = new Point(evt.getPoint());
        Main.this.repaint();
      }
    });
    this.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent evt) {
        Main.this.setVisible(false);
      }
    });
  }
  public void paint(Graphics g) {
    Container root = frame.getContentPane();
    myPaint(root, g);
  }
  private void myPaint(Component comp, Graphics g) {
    int x = comp.getX();
    int y = comp.getY();
    g.translate(x, y);
    cursor.translate(-x, -y);
    if (comp.contains(cursor)) {
      String cls_name = comp.getClass().getName();
      g.setColor(Color.black);
      g.drawString(cls_name, 0, 10);
    }
    if (comp instanceof Container) {
      Container cont = (Container) comp;
      for (int i = 0; i < cont.getComponentCount(); i++) {
        Component child = cont.getComponent(i);
        myPaint(child, g);
      }
    }
    cursor.translate(x, y);
    g.translate(-x, -y);
  }
}