我知道已經有很多可用於此主題的線程。我已經訪問過幾乎所有的電子郵件,this one,this one,this one,also this one和this one。但是沒有解決我的問題。
我的問題是不同的在這裏,當我嘗試恢復它閃爍的JFrame,並沒有超出一切。我已經在Ubuntu上運行這個代碼,它在ubuntu上像一個魅力一樣工作。 frame.setAlwaysOnTop(true);
在ubuntu上絕對正常。
在Windows中解決這個問題我試圖使用WindowsListener, 但在Windows 7中,它閃爍,並沒有在每個窗口的頂部。我認爲它試圖超越一切,但可能是其他應用比它消失的優先級更高。我該如何解決這個問題?
編輯:
我有兩個線程在這裏一個線程進行身份驗證,如果它的身份驗證它最小化。如果未通過身份驗證,則應始終位於身份驗證的首位。即使用戶通過按Alt鍵切換窗口,它應該在2秒後再次出現。
代碼進行驗證:
恢復JFrame閃爍,並使用WindowListener將其設置爲全部內容
public class ScreenLockAndUnlock implements Runnable{
public static JFrame frame;
public static boolean working = false;
private JTextField punch;
public void stop(){
working = false;
}
public void run(){
try{
frame = new JFrame("Protected");
frame.setContentPane(new JLabel(new ImageIcon("C:\\Users\\four.four-PC\\eclipse-workspace\\optimization\\src\\main\\java\\com\\greycode\\optimization\\finger_PNG6297.png")));
frame.setVisible(true);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
gs.setFullScreenWindow(frame);
frame.validate();
frame.setLayout(new BorderLayout());
punch = new JTextField();
frame.add(punch,BorderLayout.SOUTH);
punch.requestFocus();
punch.addActionListener(action);
}finally{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
private void onTop() throws AWTException{
// TODO Auto-generated method stub
AlwaysOnTop top = new AlwaysOnTop();
new Thread(top).start();
while(true){
try{
frame.setState(Frame.NORMAL);
if(punch.getText().trim()!= null && punch.getText().trim().toLowerCase().equals("true")){
working = true;
top.cancel();
frame.setState(JFrame.ICONIFIED);
Thread.sleep(10000);
top.star();
top = new AlwaysOnTop();
new Thread(top).start();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
@SuppressWarnings("serial")
Action action = new AbstractAction(){
public void actionPerformed(ActionEvent e){
try{
onTop();
} catch (AWTException e1){
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
};
}
此代碼總是尋找的JFrame是否在頂部或沒有,如果沒有通過認證
public class AlwaysOnTop implements Runnable{
boolean cancelled = false;
public void run(){
while(!cancelled){
try{
lookForMinimised();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void cancel(){
this.cancelled = true;
}
public void star(){
this.cancelled = false;
}
public void lookForMinimised() throws InterruptedException{
// TODO Auto-generated method stub
ScreenLockAndUnlock.frame.addWindowStateListener(new WindowStateListener(){
public void windowStateChanged(WindowEvent e){
// TODO Auto-generated method stub
int newState = e.getNewState();
if((newState & Frame.ICONIFIED) == Frame.ICONIFIED){
System.out.println("Frame is minimised");
ScreenLockAndUnlock.frame.setAlwaysOnTop(false);
ScreenLockAndUnlock.frame.setAlwaysOnTop(true);
ScreenLockAndUnlock.frame.setVisible(true);
ScreenLockAndUnlock.frame.toFront();
ScreenLockAndUnlock.frame.requestFocus();
ScreenLockAndUnlock.frame.validate();
ScreenLockAndUnlock.frame.setState(Frame.NORMAL);
}
else if ((newState & Frame.NORMAL) == Frame.NORMAL){
System.out.println("Waiting for authentication ...");
}
}
});
Thread.sleep(2000);
}
}
主要方法:
public class Authenticate{
public static void main(String[] args){
Thread displayScreen = new Thread(new ScreenLockAndUnlock());
displayScreen.start();
}
}
請提供一個簡單的可運行示例([SSCCE](http://sscce.org)),它演示了您的問題,因此我們也可以理解並調試它。 –
@SergiyMedvynskyy編輯。我也給出了我的問題的簡要說明 – Lokesh
代碼不完整。缺少主要方法。 – Aman