由于某种原因,每张图像仅显示 1/5(或更少)。由于它是分层窗格,因此至少应完全显示 1 个。布局为空,因为这是一个学习程序 为什么会发生这种情况?
这是代码: 类 test1
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.Image;
import java.net.URL;
public class test extends JPanel
{
private JFrame frame=new JFrame("");
private JLayeredPane layered =new JLayeredPane();
private BufferedImage image;
private test2 image2=new test2();
public test()
{
try
{
image=ImageIO.read(new File("im1.png"));
}
catch (IOException e)
{
//Nothing
e.printStackTrace();
}
}
public void draw()
{
frame.setLayout(null);
image2.setLayout(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setResizable(true);
frame.setSize(800,800);
frame.add(layered);
this.setBounds(30,50,70,80);
layered.add(this,new Integer(1));
image2.setBounds(100,120,170,400);
layered.add(image2,new Integer(1000));
frame.setContentPane(layered);
frame.setVisible(true);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image,0,0,null);
}
public static void main()
{
test t=new test();
t.draw();
}
}
类测试2
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.Image;
import java.net.URL;
public class test2 extends JPanel
{
private JPanel thepanel=new JPanel();;
private BufferedImage image2;
public test2()
{
try{image2=ImageIO.read(new File("im2.png"));}
catch(IOException e){e.printStackTrace();}
}
@Override
protected void paintComponent(Graphics g)
{ super.paintComponent(g);
g.drawImage(image2,0,0,this);
}
}
我也不知道 setOpaque(true/false) 到底是什么以及何时需要。