⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windowblocker.java

📁 《SWING HACKS》源码 作者: Joshua Marinacci,Chris Adamson 著 徐晔 译 出版: 2007年2月
💻 JAVA
字号:
import java.awt.event.*;import java.awt.*;import javax.swing.*;import javax.swing.event.*;/* as a future enhancment you could make it beep orpop up a dialog box saying stuff is in process, do you wishto quit.*/public class WindowBlocker extends JComponent    implements MouseInputListener {    private Cursor old_cursor;    public WindowBlocker() {        addMouseListener(this);        addMouseMotionListener(this);    }        public void mouseMoved(MouseEvent e) {    }    public void mouseDragged(MouseEvent e) {    }    public void mouseClicked(MouseEvent e) {        Toolkit.getDefaultToolkit().beep();    }    public void mouseEntered(MouseEvent e) {    }    public void mouseExited(MouseEvent e) {    }    public void mousePressed(MouseEvent e) {    }    public void mouseReleased(MouseEvent e) {    }        public void block() {        old_cursor = getCursor();        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));        setVisible(true);    }        public void unBlock() {        setCursor(old_cursor);        setVisible(false);    }        public static void main(String[] args) {        JFrame frame = new JFrame("Blocking Window");        JTextArea jta = new JTextArea(10,40);        JScrollPane scroll = new JScrollPane(jta);        JButton start = new JButton("Start Processing");        JLabel status = new JLabel("status");                WindowBlocker blocker = new WindowBlocker();        frame.setGlassPane(blocker);        start.addActionListener(new LongProcess(status,blocker));                        Container comp = frame.getContentPane();        comp.add("North",start);        comp.add("Center",scroll);        comp.add("South",status);        frame.pack();        frame.show();    }    }class LongProcess implements ActionListener, Runnable {    JLabel status;    WindowBlocker blocker;    public LongProcess(JLabel status, WindowBlocker blocker) {        this.blocker = blocker;        this.status = status;    }        public void actionPerformed(ActionEvent evt) {        blocker.block();        new Thread(this).start();    }        public void setText(final String text) {        SwingUtilities.invokeLater(new Runnable() {            public void run() {                status.setText(text);            }        });    }        public void run() {        for(int i=10; i>0; i--) {            // set the label            final String text = "("+i+") seconds left";            setText(text);            // sleep for 1 second            try {                Thread.currentThread().sleep(1000);            } catch (Exception ex) {            }        }        // set the final status string        setText("Process Complete");        blocker.unBlock();    }    }

⌨️ 快捷键说明

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