Java 在JFrame上绘制图像的最简单方法?
我是java的初学者。我有一张名为Java 在JFrame上绘制图像的最简单方法?,java,swing,Java,Swing,我是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
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
包装实践;
导入java.awt.*;
导入java.awt.image.*;
导入java.io.*;
导入javax.imageio.*;
导入javax.swing.*;
公共课堂实践
{
公共静态void main(字符串[]args)
{
Jf ob=新的Jf();
ob.setVisible(真);
}
}
公共类Jf扩展了javax.swing.JFrame
{
BuffereImage img=null;
公共Jf()
{
初始化组件();
尝试
{
img=ImageIO.read(新文件(“1.png”);
}
捕获(IOE异常)
{
}
Graphics g=img.getGraphics();
g、 drawImage(img,0,0,根窗格);
}
//下面的代码是由netbeans自动生成的,我什么也没做
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,400,短。最大值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,300,短。最大值)
);
包装();
}//
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(Jf.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新Jf().setVisible(true);
}
});
}
//变量声明-不修改
//变量结束声明
}
限制:
我不能在这里使用JLabel
因此,请提及我的错误,并尝试写下更正,以便我能够轻松理解。
提前感谢。使用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()));
使用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()));
首先,查看图像的路径。您需要将图像放入项目根目录中。(或者可以使用绝对路径,如“C:/…/yourfile”) 不要让异常为空。编写e.printStackTrace(),因为在这里您无法看到是否引发了异常
您需要将代码放入窗格的paintComponent中。首先,查看图像的路径。您需要将图像放入项目根目录中。(或者可以使用绝对路径,如“C:/…/yourfile”) 不要让异常为空。编写e.printStackTrace(),因为在这里您无法看到是否引发了异常
您需要将代码放入窗格的paintComponent中。您有两个主要方法-您正在调用哪一个?1)将
catch(IOException e){}
更改为catch(IOException e){e.printStackTrace();}
2)“我不能在这里使用JLabel
为什么?3) 应用程序资源在部署时将成为嵌入式资源,因此现在就开始访问它们是明智的。必须通过URL而不是文件访问。有关如何形成URL的信息,请参见。4)Graphics g=img.getGraphics();g、 drawImage(img,0,0,根窗格)代码>该代码将图像重新绘制到自身上,但一无所获。5) 请参阅,以了解一个我不再费心修复的问题。请确保您记录任何错误,而不是忽略它们。您有两个主要方法-您正在调用哪一个?1)将catch(IOException e){}
更改为catch(IOException e){e.printStackTrace();}
2)“我不能使用JLabel
here”为什么不?3) 应用程序资源在部署时将成为嵌入式资源,因此现在就开始访问它们是明智的。必须通过URL而不是文件访问。有关如何形成URL的信息,请参见。4)Graphics g=img.getGraphics();g、 drawImage(img,0,0,根窗格)代码>该代码将图像重新绘制到自身上,但一无所获。5) 请参阅,以了解一个我不再费心修复的问题。请确保记录任何错误,而不是忽略它们。此外,请将该代码放入编译器,因为我怀疑它是否正常工作。也请将该代码放入编译器,因为我怀疑它是否正常工作