以下是我目前的代码: 所有的导入都是正确的。我敢肯定。 :D
当我运行该程序时,我得到的只是一个空白框,没有图片。它应该出现。
public class WindowPractice extends JFrame {
final static int width= 800;
final static int height= 400;
int x;
int y;
Image steve;
Dimension gamesize= new Dimension (width, height);
public WindowPractice(){
setTitle ("Hangman");
setSize (gamesize);
setVisible (true);
setResizable (false);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args) {
new WindowPractice();
ImageIcon steve= new ImageIcon ("Libraries/Pictures/ba190cd951302bcebdf216239e156a4.jpg");
JLabel imageLabel = new JLabel(steve);
}
public void paint(Graphics g){
g.setColor(Color.red);
//g.fillRect( x, y, 100, 20);
g.drawImage(steve, x, y,this);
x= 150;
y= 250;
}
}
最佳答案
这有很多问题我不知道从哪里开始......
让我们从头开始......
问题 #1
您声明一个名为 steve
的实例字段在你里面WindowPractice
类,这很好,但是在您的主要方法中,您声明了另一个名为 steve
的变量您正在使用对加载图像的引用...
public static void main(String[] args) {
new WindowPractice();
ImageIcon steve = new ImageIcon("C:/Users/shane/Dropbox/issue459.jpg");
JLabel imageLabel = new JLabel(steve);
}
这意味着类实例变量永远不会被初始化并保持为null
。 .
问题 #2
虽然没有直接关系,但您永远不会调用 super.paint
来自你的 paint
方法。这是一个很大的不,不。您有义务维护油漆链。绘画方法很复杂,而且非常非常重要。
问题 #3
您永远不应该覆盖顶级容器(例如 JFrame
),也不应该覆盖其中的任何一个 paint
方法。这有很多原因,但在前两个中,大多数顶级容器实际上包含许多组件( JRootPane
,其中包含玻璃 Pane 、内容 Pane 、图层 Pane 和菜单栏),它们可以位于您的绘制工作,一般来说,它们不是双缓冲的,这意味着您的绘制更新会闪烁并且看起来很糟糕;)
您还应该避免使用 paint
,相反,您应该考虑使用 paintComponent
在哪里可用。
问题 #4
ImageIcon
不是您加载图像的最佳选择。我不使用它们的主要原因是您不知道正在加载的图像何时真正可用(实际上有很多方法,但坦率地说,ImageIO
更简单)。这在 1999 年是一个很棒的功能,当时拨号速度大约为 14.4k 标记,但现在...
ImageIO
支持更广泛的图片格式,支持读取和写入图像,并保证当方法返回(成功)时,图像像素数据可供您的应用程序使用。
示例
这是一个更好的(恕我直言)方法...
public class BetterDrawing {
public static void main(String[] args) {
new BetterDrawing();
}
public BetterDrawing() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new PaintPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintPane extends JPanel {
private BufferedImage background;
public PaintPane() {
try {
background = ImageIO.read(new File("/path/to/image"));
// Use this instead to load embedded resources instead
//background = ImageIO.read(getClass().getResource("/path/to/image"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g.drawImage(background, x, y, this);
}
}
}
}
读一读
了解更多信息。
关于java - JFrame 不显示图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13871307/