我有两个JPanel层叠在同一个容器中。我正在使用container.add(jpanel,0);和container.add(otherjpanel,1)。它工作正常,但为了顶层显示我必须用鼠标悬停在组件上。组件分层Java Swing,图层显示在悬停上
这是一些可执行的代码,显示我的问题。
只需将鼠标悬停在屏幕上部。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
import java.util.*;
public class test {
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {}
JFrame frame = new GUIframe();
frame.setVisible(true);
frame.setResizable(false);
}
}
class GUIframe extends JFrame{
public GUIframe(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300,400));
setSize(300, 400);
JLayeredPane content = new JLayeredPane();
content.setPreferredSize(new Dimension(300,400));
content.setSize(300,400);
JPanel board = new JPanel();
for (int i = 0;i<5;i++){
JButton button = new JButton("button");
board.add(button);
}
content.add(new ImagePanel());
this.add(content);
this.add(board);
}
}
class ImagePanel extends JPanel {
private Image img;
String imageLocation = "image location here";
ImagePanel() {
img = new ImageIcon(imageLocation).getImage();
setPreferredSize(new Dimension(300,400));
setSize(300,400);
setLayout(null);
setOpaque(false);
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(img, 0, 0, this);
}
}
1)为了更好地帮助越早,张贴[SSCCE](http://pscode.org/sscce.html)2)在Swing非薄层扫描法,超控'的paintComponent(图形)'而不是'涂料(图形)'。 –