JLayeredPane 中的多个 JPanels 绘制图像
multiple JPanels paint image in JLayeredPane
这是我的代码,当我将 JPanel 添加到 JLayeredPane 时,它无法显示任何图像。
当我需要将它们放在屏幕上时,我不知道如何显示多个 jpanel
public class game extends JFrame {
private JLayeredPane layeredPane;
private GraphicPanel gui;
private gameWindows test;
public game(){
this.setTitle("gameVer0.01");
this.setUndecorated(true);
setLocationRelativeTo(this);
gui=new GraphicPanel("url");
test=new gameWindows("url");
this.setLayout(new BorderLayout());
layeredPane =new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(1280, 720));
layeredPane.add(test, 100);
layeredPane.add(gui, 200);
layeredPane.setOpaque(true);
layeredPane.setVisible(true);
this.add(layeredPane,BorderLayout.CENTER);
setResizable(false);
setExtendedState(JFrame.MAXIMIZED_BOTH);
final GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(this);
device.setDisplayMode(new DisplayMode(1280,720,32,60));
this.setSize(400,400);
this.pack();
this.setVisible(true);
}
public static void main(String srg[]){
game window=new game();
new BasicListener(window);
}
}
class gameWindows extends JPanel {
public gameWindows(String url)
{
setPreferredSize(new Dimension(1280,720));
this.setVisible(true);
this.setBackground(Color.blue);
this.setOpaque(false);
}
Image image1,image2;
public void paintComponent(Graphics g) {
/* Call the original implementation of this method */
super.paintComponent(g);
try {
FileInputStream fi = new FileInputStream("C:\Users\casper\Desktop\123\game test\src\data\res\background.png");
image1 = ImageIO.read(fi);
fi.close();
}
catch (Exception ex) {
System.out.println("No example.jpg!!");
}
g.drawImage(image1, 0,0, null);
}
}
class GraphicPanel extends JPanel
{
public GraphicPanel(String url)
{
this.setPreferredSize(new Dimension(800,600));
this.setVisible(true);
this.setBackground(Color.blue);
this.setOpaque(false);
}
@Override
public void paintComponent(Graphics g) {
/* Call the original implementation of this method */
super.paintComponent(g);
try {
FileInputStream fi = new FileInputStream("C:\Users\casper\Desktop\123\game test\src\data\res\1.jpg");
image = ImageIO.read(fi);
fi.close();
}
catch (Exception ex) {
System.out.println("No example.jpg!!");
}
g.drawImage(image, 0, 0, null);
}
}
这个问题困扰了我一天
感谢所有回复者
JLayeredPane 使用空布局。因此,您有责任设置添加到分层窗格中的任何组件的大小和位置。否则默认大小为 (0, 0) 所以没有什么可画的。
阅读有关 How to Use Layered Panes 的 Swing 教程部分以获取工作示例。下载演示代码并进行测试,确保您理解它。然后你就可以修复你的代码了。
这是我的代码,当我将 JPanel 添加到 JLayeredPane 时,它无法显示任何图像。 当我需要将它们放在屏幕上时,我不知道如何显示多个 jpanel
public class game extends JFrame {
private JLayeredPane layeredPane;
private GraphicPanel gui;
private gameWindows test;
public game(){
this.setTitle("gameVer0.01");
this.setUndecorated(true);
setLocationRelativeTo(this);
gui=new GraphicPanel("url");
test=new gameWindows("url");
this.setLayout(new BorderLayout());
layeredPane =new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(1280, 720));
layeredPane.add(test, 100);
layeredPane.add(gui, 200);
layeredPane.setOpaque(true);
layeredPane.setVisible(true);
this.add(layeredPane,BorderLayout.CENTER);
setResizable(false);
setExtendedState(JFrame.MAXIMIZED_BOTH);
final GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(this);
device.setDisplayMode(new DisplayMode(1280,720,32,60));
this.setSize(400,400);
this.pack();
this.setVisible(true);
}
public static void main(String srg[]){
game window=new game();
new BasicListener(window);
}
}
class gameWindows extends JPanel {
public gameWindows(String url)
{
setPreferredSize(new Dimension(1280,720));
this.setVisible(true);
this.setBackground(Color.blue);
this.setOpaque(false);
}
Image image1,image2;
public void paintComponent(Graphics g) {
/* Call the original implementation of this method */
super.paintComponent(g);
try {
FileInputStream fi = new FileInputStream("C:\Users\casper\Desktop\123\game test\src\data\res\background.png");
image1 = ImageIO.read(fi);
fi.close();
}
catch (Exception ex) {
System.out.println("No example.jpg!!");
}
g.drawImage(image1, 0,0, null);
}
}
class GraphicPanel extends JPanel
{
public GraphicPanel(String url)
{
this.setPreferredSize(new Dimension(800,600));
this.setVisible(true);
this.setBackground(Color.blue);
this.setOpaque(false);
}
@Override
public void paintComponent(Graphics g) {
/* Call the original implementation of this method */
super.paintComponent(g);
try {
FileInputStream fi = new FileInputStream("C:\Users\casper\Desktop\123\game test\src\data\res\1.jpg");
image = ImageIO.read(fi);
fi.close();
}
catch (Exception ex) {
System.out.println("No example.jpg!!");
}
g.drawImage(image, 0, 0, null);
}
}
这个问题困扰了我一天
感谢所有回复者
JLayeredPane 使用空布局。因此,您有责任设置添加到分层窗格中的任何组件的大小和位置。否则默认大小为 (0, 0) 所以没有什么可画的。
阅读有关 How to Use Layered Panes 的 Swing 教程部分以获取工作示例。下载演示代码并进行测试,确保您理解它。然后你就可以修复你的代码了。