我创建了JFrame,将透明颜色(new Color(0,0,0,0))设置为背景并将JPanel放在其上。我已经覆盖了JPanel的paintComponent方法,并在其上绘制了一些带alpha通道的图像。当用户右键单击我的JPanel时,我也会显示弹出菜单。
我遇到了这样一个事实:弹出菜单的图像应该在JFrame消失之后保留在JFrame上(刚才JPanel的图片覆盖了它)。据我了解,在绘制新框架之前不会清除JFrame。
示例
public class MainWindow extends JFrame {
private MyPanel widget = new MyPanel();
private Action quitAction = new AbstractAction("Quit") {
@Override
public void actionPerformed(ActionEvent actionEvent) {
setVisible(false);
dispose();
}
};
private MainWindow() {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setBackground(new Color(0, 0, 0, 0));
widget.loadImage("image.png");
setContentPane(widget);
pack();
setAlwaysOnTop(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
MainWindow window = new MainWindow();
window.setVisible(true);
});
}
class MyPanel extends JPanel implements MouseListener {
BufferedImage image;
MyPanel() {
super();
addMouseListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintCompontent(g);
if (characterImage != null) {
g.drawImage(characterImage, 0, 0, this);
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
clickPos = e.getLocationOnScreen();
if (SwingUtilities.isRightMouseButton(e)) {
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(quitAction);
popupMenu.show(this, e.getX(), e.getY());
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
boolean loadImage(String name) {
BufferedImage backupImage = image;
try {
image = ImageIO.read(MainWindow.class.getResourceAsStream("/images/" + name));
setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
} catch (IOException ex) {
image = backupImage;
return false;
}
return true;
}
}
}
答案 0 :(得分:1)
所以,基于你的代码松散,一旦我纠正它工作,我似乎没有任何问题
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class MainWindow extends JFrame {
private MyPanel widget = new MyPanel();
private Action quitAction = new AbstractAction("Quit") {
@Override
public void actionPerformed(ActionEvent actionEvent) {
setVisible(false);
dispose();
}
};
private MainWindow() {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setBackground(new Color(0, 0, 0, 0));
widget.loadImage("MT.png");
setContentPane(widget);
pack();
setLocationRelativeTo(null);
setAlwaysOnTop(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
MainWindow window = new MainWindow();
window.setVisible(true);
});
}
class MyPanel extends JPanel implements MouseListener {
BufferedImage image;
MyPanel() {
super();
addMouseListener(this);
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
if (image != null) {
g.drawImage(image, 0, 0, this);
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
// clickPos = e.getLocationOnScreen();
if (SwingUtilities.isRightMouseButton(e)) {
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(quitAction);
popupMenu.show(this, e.getX(), e.getY());
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
boolean loadImage(String name) {
BufferedImage backupImage = image;
try {
image = ImageIO.read(MainWindow.class.getResourceAsStream("/images/" + name));
setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
} catch (IOException ex) {
image = backupImage;
return false;
}
return true;
}
}
}
答案 1 :(得分:0)
我在代码中发现了问题。为了使一切正常工作,我需要在绘图中使用paintCompontent中的g.drawImage插入此代码。
if (g instanceof Graphics2D) {
Graphics2D g2d = (Graphics2D)g;
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
}
此外,问题并非出现在所有情况下。例如,在Windows下它运行良好。