gpt4 book ai didi

java - JWindow - 总是在顶部,但在系统栏下方?

转载 作者:行者123 更新时间:2023-11-30 11:21:25 25 4
gpt4 key购买 nike

我一直在制作一个程序来监视远程系统并在屏幕的一个角落(用户首选项)显示非侵入式警报,以提醒用户远程系统的变化。为此,我使用了 JWindow,以便为更重要的警报产生脉冲效果,以引起用户的注意。我还有一个从屏幕外滚动到屏幕上的警报。

我遇到的问题是,如果我不将这些警报设置为总是在最前面,它们不会总是显示,但是当我将它们设置为总是在最前面时,滚动选项还显示在任务栏的上方。有什么办法可以强制它显示在所有其他程序之上(不需要全屏程序),但在任务栏下方?

编辑:这是我用于滚动 JWindow 开/关屏幕的代码:

Edit2:更新代码以显示我合并了 NESPowerGlove 的答案:

public void scrollOn() {
//Get the normal screen area minus taskbar
Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
int taskBar = scnMax.bottom; //Bottom of the normal window area
int x = screenSize.width - getWidth(); //Horizontal start point of the window
int yEnd = screenSize.height - taskBar - getHeight(); //where the window will stop
int yStart = screenSize.height; //Vertical start point of the window
setLocation(x,yStart); //set window to start location
int current = yStart; //windows current location
newHeight = yStart - current; //Set the newHeight field to the clipping start height
while(current > yEnd) { //loop while window is still enroute to final destination
current-=2; //increments to move the window, in pixels
newHeight = yStart - current; //Update the newHeight field to clip the window appropriately based on position
setLocation(x,current); //move the window to the next position
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//This is basically the reverse of the scrollOn() method
public void scrollOff() {
Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
int taskBar = scnMax.bottom;
int x = screenSize.width - getWidth();
int yEnd = screenSize.height - taskBar;
int yStart = this.getBounds().y;
setLocation(x,yStart);
int current = yStart;
newHeight = this.getBounds().height;
while(current < yEnd) {
current+=2;
newHeight = yEnd - current;
setLocation(x,current);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Tells the system the next alert can be triggered
//This prevents screen clutter from multiple alerts by setting up a queue
Main.alertControl.setAlertActive(false);
}

下面是我实际构建窗口的方式(当前)。在我获得构建最终 L&F 窗口的图像之前,此方法只是一个占位符。

public AlertScroller(String msg) {
addComponentListener(new ComponentAdapter() {
@Override
public void componentMoved(ComponentEvent e) {
//Sets the area to be shown while the rest is clipped,
//updating whenever the component moves
setShape(new Rectangle2D.Double(0, 0, getWidth(), newHeight));
if(!isVisible())
setVisible(true);
}
});
setAlwaysOnTop(true);
JPanel panel = new JPanel();
panel.setBorder(compound);
panel.setBackground(Color.yellow);

JLabel imgLbl = new JLabel(msg);
imgLbl.setFont(new Font(null,Font.BOLD,16));

panel.add(imgLbl);
setContentPane(panel);
pack();

this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent click) {
if(SwingUtilities.isLeftMouseButton(click)) {
autoClear = false;
scrollOff();
}
}
});
}

最佳答案

我不确定这是否是推荐的做法,但以下是您想要的:

while(tillYouWantToDisplay) {
window.setAlwaysOnTop(true);
window.setAlwaysOnTop(false);
delay(delayTime); // You have to write this method using sleep()
// delayTime may be about 500ms
}
window.dispose();

您可能必须将上述代码放在一个线程中。

我在下面编写了快速演示代码来说明我的意思(只需运行它即可查看):

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JWindow;

public class TestProgram extends JWindow {

public static void main(String[] args) throws Exception {
TestProgram window = new TestProgram();
window.setVisible(true);

Dimension windowDim = window.getSize();
int steps = 20;
for (int i = 0; i < steps; i++) {
window.setAlwaysOnTop(true);
window.setAlwaysOnTop(false);
Point p = window.getLocation();
window.setLocation(p.x, p.y - windowDim.height / steps);
Thread.sleep(100);
}
for (int i = 0; i < 10; i++) {
window.setAlwaysOnTop(true);
window.setAlwaysOnTop(false);
Thread.sleep(100);
}
for (int i = 0; i < steps; i++) {
window.setAlwaysOnTop(true);
window.setAlwaysOnTop(false);
Point p = window.getLocation();
window.setLocation(p.x, p.y + windowDim.height / steps);
Thread.sleep(100);
}
window.dispose();
}

public TestProgram() {
setSize(200, 100);
Rectangle scrDim = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
setLocation(scrDim.x + scrDim.width - getSize().width, scrDim.y + scrDim.height);

JLabel msg = new JLabel("Your Message");
msg.setHorizontalAlignment(JLabel.CENTER);
msg.setVerticalAlignment(JLabel.CENTER);
getContentPane().add(msg);
getContentPane().setBackground(Color.red);
}
}

关于java - JWindow - 总是在顶部,但在系统栏下方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22232575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com