Java 为什么我在JFrame中使用FlowLayout时不显示图像?
有一个问题我不明白:如果Java 为什么我在JFrame中使用FlowLayout时不显示图像?,java,swing,layout,layout-manager,Java,Swing,Layout,Layout Manager,有一个问题我不明白:如果JFrame的布局是BorderLayout或GridLayout但不是gridbagloayout,FlowLayout或BoxLayout,为什么我能够用drawImage()方法绘制图像?谁能给我解释一下吗 代码如下: package footballQuestioner; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.
JFrame
的布局是BorderLayout
或GridLayout
但不是gridbagloayout
,FlowLayout
或BoxLayout
,为什么我能够用drawImage()
方法绘制图像?谁能给我解释一下吗
代码如下:
package footballQuestioner;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class attempter {
public static void main(String[] args) {
JFrame frame = new Beispielfenster();
}
}
class Beispielfenster extends JFrame {
private class TransparentBG extends JLabel {
BufferedImage image;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
image = ImageIO
.read(TransparentBG.class
.getClassLoader()
.getResourceAsStream(
"footballQuestioner/rightAnswerSign.png"));
} catch (IOException e) {
e.printStackTrace();
}
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
}
}
public Beispielfenster() {
//setLayout(new FlowLayout());
JPanel panel=new JPanel(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label=new TransparentBG();
panel.add(label);
add(panel);
pack();
centeringWindow();
setVisible(true);
}
public void centeringWindow() {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x;
int y;
x = (int) (dimension.getWidth() - getWidth()) / 2;
y = (int) (dimension.getHeight() - getHeight()) / 2;
setLocation(x, y);
}
}
因为对于前两个布局,JLabel将填充容器,这里是JPanel。对于其他布局,它的大小将调整为其首选大小0。如果希望使用其他布局,请考虑重写GETAPYRADE大小。< /P> 另请注意:
- 如果没有paintComponent方法,则不应读取图像文件
- 如果可能的话,您不应该重新读取图像文件(如果您使用一个被反复调用的方法读取图像,就会发生这种情况)。将它们读入一次,例如在构造函数中,然后将它们存储在字段中
- 您不应该处理JVM提供给您的图形对象,而应该只处理您自己创建的图形对象(例如从BuffereImage)
编辑
例如,类似这样的内容:
private class TransparentBG extends JLabel {
BufferedImage image;
public TransparentBG() throws IOException {
image = ImageIO.read(TransparentBG.class.getClassLoader()
.getResourceAsStream("footballQuestioner/rightAnswerSign.png"));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if (image != null) {
g2d.drawImage(image, 0, 0, null);
}
// g2d.dispose();
}
@Override
public Dimension getPreferredSize() {
if (image != null) {
int w = image.getWidth();
int h = image.getHeight();
return new Dimension(w, h);
}
return super.getPreferredSize();
}
}