[英]Why does my image not appear when I use FlowLayout for my JFrame?
There is an issue which I do not understand: Why I am able to paint the image with the drawImage()
method if the Layout of the JFrame
is a BorderLayout
or GridLayout
but NOT GridbagLayout
, FlowLayout
or BoxLayout
? 有一个我不明白的问题:如果
JFrame
的布局是BorderLayout
或GridLayout
而不是GridbagLayout
, FlowLayout
或BoxLayout
,为什么我可以用drawImage()
方法绘制图像? Can somebody please explain it to me? 有人可以向我解释吗?
Here is the code: 这是代码:
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);
}
}
Because with the first two layouts, the JLabel will fill the container, here the JPanel. 因为使用前两个布局,JLabel将填充容器,这里是JPanel。 With the other layouts, it will size to its preferredSize which is 0. Consider overriding getPreferredSize if you wish to use other layouts.
对于其他布局,它将调整为其preferredSize为0。如果您希望使用其他布局,请考虑覆盖getPreferredSize。
Also note: 另请注意:
Edit 编辑
For example, something like this: 例如,如下所示:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.