gpt4 book ai didi

java - 在 JFrame 上绘制图像的最简单方法?

转载 作者:行者123 更新时间:2023-12-02 03:41:29 24 4
gpt4 key购买 nike

我是java初学者。我有一个名为 1.png 的图像。我试图将它显示在我的 JFrame 上,但是当我运行代码时,框架运行但没有图像。这是我的代码:

package practice;

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class Practice
{
public static void main(String[] args)
{
Jf ob = new Jf();
ob.setVisible(true);
}
}
public class Jf extends javax.swing.JFrame
{

BufferedImage img = null;
public Jf()
{
initComponents();
try
{
img = ImageIO.read(new File("1.png"));
}
catch (IOException e)
{

}
Graphics g = img.getGraphics();
g.drawImage(img, 0, 0, rootPane);
}


//below the code is auto-generated by netbeans , i did nothing




/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Jf().setVisible(true);
}
});
}

// Variables declaration - do not modify
// End of variables declaration

}

限制:

I can't use JLabel here

所以,请指出我的错误并尝试写出更正,以便我可以轻松理解。提前致谢。

最佳答案

使用 JPanel。将其包含在您的 JFrame 类中:

public static class CustomPanel extends javax.swing.JPanel {
CustomPanel(width, height) {
super(width, height);
}
void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}

并在您的主要方法中包含以下内容:

ob.add(new CustomPanel(ob.getWidth(), ob.getHeight()));

关于java - 在 JFrame 上绘制图像的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36790929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com