gpt4 book ai didi

java - 创建一个消失的 JPanel?

转载 作者:行者123 更新时间:2023-12-01 21:53:08 25 4
gpt4 key购买 nike

我正在尝试创建一个扩展的 JPanel 来突出显示屏幕的某些区域。我从 this SO answer 获取了一些代码但想进一步扩展它,尽管我不知道如何去做。

我希望能够在达到给定超时后让我的 JPanel(下面的 MatchAreaPanel)消失。也就是说,JPanel 将其可见属性设置为 false,然后自行处置。

执行此操作的最佳方法是什么?

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class MatchAreaPanel extends JPanel
{
public MatchAreaPanel()
{

}

@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(new Color(128, 128, 128, 64));
g2d.fillRect(0, 0, getWidth(), getHeight());

float dash1[] = {10.0f};
BasicStroke dashed = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
g2d.setColor(Color.BLACK);
g2d.setStroke(dashed);
g2d.drawRect(0, 0, getWidth() - 3, getHeight() - 3);
g2d.dispose();
}
}

最佳答案

您可以使用 Swing Timer 在给定的延迟后简单地安排回调,并关闭关联的窗口或根据您的需要隐藏组件,例如...

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

Rectangle bounds = getVirtualBounds();
Random rnd = new Random();
int x = bounds.x + (rnd.nextInt(bounds.width) - 100);
int y = bounds.y + (rnd.nextInt(bounds.height) - 100);

MatchAreaPanel pane = new MatchAreaPanel();
JWindow frame = new JWindow();
frame.setBackground(new Color(0, 0, 0, 0));
frame.add(pane);
frame.setBounds(x, y, 100, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
pane.start();
}
});
}

public static Rectangle getVirtualBounds() {

Rectangle bounds = new Rectangle(0, 0, 0, 0);

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
bounds.add(gd.getDefaultConfiguration().getBounds());

return bounds;

}

public class MatchAreaPanel extends JPanel {

public MatchAreaPanel() {
setOpaque(false);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
SwingUtilities.windowForComponent(MatchAreaPanel.this).dispose();
}
});
}

public void start() {
Timer timer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.windowForComponent(MatchAreaPanel.this).dispose();
}
});
timer.start();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(new Color(128, 128, 128, 64));
g2d.fillRect(0, 0, getWidth(), getHeight());

float dash1[] = {10.0f};
BasicStroke dashed = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
g2d.setColor(Color.BLACK);
g2d.setStroke(dashed);
g2d.drawRect(0, 0, getWidth() - 3, getHeight() - 3);
g2d.dispose();
}
}

}

参见How to use Swing Timers了解更多详情

已更新...

现在,简单地“隐藏”面板很无聊,用户也有可能错过面板,因为突然出现并不能保证用户会看到它,所以相反,您可以添加淡出效果。

在此示例中,您可以通过单击面板淡出面板(但我这样做是作为测试的一部分,因此您不需要它)或在指定的超时后...

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

Rectangle bounds = getVirtualBounds();
Random rnd = new Random();
int x = bounds.x + (rnd.nextInt(bounds.width) - 100);
int y = bounds.y + (rnd.nextInt(bounds.height) - 100);

MatchAreaPanel pane = new MatchAreaPanel();
JWindow frame = new JWindow();
frame.setBackground(new Color(0, 0, 0, 0));
frame.add(pane);
frame.setBounds(x, y, 100, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
pane.start();
}
});
}

public static Rectangle getVirtualBounds() {

Rectangle bounds = new Rectangle(0, 0, 0, 0);

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
bounds.add(gd.getDefaultConfiguration().getBounds());

return bounds;

}

public static class MatchAreaPanel extends JPanel {

protected static final long FADE_OUT_TIME = 2500;

private float alpha = 1f;
private long fadeStartAt;
private Timer fadeTimer;
private Timer waitTimer;

public MatchAreaPanel() {
setOpaque(false);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
fadeOut();
}
});
fadeTimer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
long runTime = System.currentTimeMillis() - fadeStartAt;
float progress = 0f;
if (runTime >= FADE_OUT_TIME) {
progress = 1f;
} else {
progress = (float) runTime / (float) FADE_OUT_TIME;
if (progress > 1f) {
progress = 1f;
}
}
alpha = 1f - progress;

if (progress >= 1f) {
((Timer) e.getSource()).stop();
SwingUtilities.windowForComponent(MatchAreaPanel.this).dispose();
}
repaint();
}
});
waitTimer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((Timer) e.getSource()).stop();
fadeOut();
}
});
}

protected void fadeOut() {
waitTimer.stop();
fadeStartAt = System.currentTimeMillis();
fadeTimer.start();
}

public void start() {
if (!waitTimer.isRunning() && !fadeTimer.isRunning()) {
waitTimer.start();
}
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(alpha));
g2d.setColor(new Color(128, 128, 128, 64));
g2d.fillRect(0, 0, getWidth(), getHeight());

float dash1[] = {10.0f};
BasicStroke dashed = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
g2d.setColor(Color.BLACK);
g2d.setStroke(dashed);
g2d.drawRect(0, 0, getWidth() - 3, getHeight() - 3);
g2d.dispose();
}
}

}

关于java - 创建一个消失的 JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34845342/

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