package falcon.test;import java.awt.event.*; import javax.swing.*;import java.awt.*; import java.util.*;public class MyFrame extends JFrame implements ActionListener { public MyFrame() { init(); } private JButton testButton; private void init() { this.setLayout(new BorderLayout()); testButton = new JButton("Test Mouse Click"); testButton.addActionListener(this); this.add(testButton, BorderLayout.NORTH); JButton another = new JButton("another"); another.addActionListener(this); this.add(another, BorderLayout.SOUTH);
this.getGlassPane().addMouseListener( new MouseAdapter() {}); this.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); // this statement will mask any mouse event for all controls on this frame this.getGlassPane().setVisible(true); } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == testButton) { JOptionPane.showMessageDialog(this, "OH, I've been clicked!!"); } }
public static void main(String[] args){ JFrame frame = new MyFrame();
怎样做才能真正置忙?也就是在JFrame区域内都不能起作用。
谢谢
button.setEnabled(false);
等鼠标指针忙完了再设置回来
button.setEnabled(true);
我想要的效果是真正的鼠标置忙。比如有些软件在鼠标忙状态时是不能进行任何操作的。
谢谢大人关注,此帖一定散分,言而有幸
举个例子吧:比如界面上有"打印""暂停""退出"等几个按钮,用户点击"打印"时,打印机开始工作,此时用户点击"暂停",但是打印机的反应比较慢,所以这时候我想设置鼠标忙,也就是不能点击界面上任何按钮,直到暂停结束,恢复鼠标功能,可以进行其它操作.
如果单纯的设置button.enable(false),按钮状态会改变为灰化,这是没有必要的。只是要控制鼠标忙得时候不能在界面上作操作而已。
import javax.swing.*;import java.awt.*;
import java.util.*;public class MyFrame extends JFrame implements ActionListener { public MyFrame() {
init();
} private JButton testButton; private void init() {
this.setLayout(new BorderLayout()); testButton = new JButton("Test Mouse Click");
testButton.addActionListener(this);
this.add(testButton, BorderLayout.NORTH); JButton another = new JButton("another");
another.addActionListener(this);
this.add(another, BorderLayout.SOUTH);
this.getGlassPane().addMouseListener( new MouseAdapter() {});
this.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
// this statement will mask any mouse event for all controls on this frame
this.getGlassPane().setVisible(true);
} public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == testButton) {
JOptionPane.showMessageDialog(this, "OH, I've been clicked!!");
}
}
public static void main(String[] args){
JFrame frame = new MyFrame();
frame.setPreferredSize(new Dimension(500, 300));
frame.pack();
frame.setVisible(true);
}}其中
this.getGlassPane().setVisible(true);是最关键的一个语句。类似于一个mask的作用,呵呵。
希望能有所帮助。