私は初心者です。私は1.png
と呼ばれる画像を持っています。 JFrame
に表示しようとしていましたが、コードを実行するとフレームは実行されますが、画像はありません。ここに私のコードは次のとおりです。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
}
制限:
私はだからここ
JLabel
を使用することはできません、私のエラーに言及し、その補正を書くことをしてみてください私は容易に理解できる。 ありがとうございます。
あなたには2つの主要な方法があります。 – assylias
1キャッチ 'へ)変更 'キャッチ(IOExceptionを電子) {}'(IOExceptionを電子) {e.printStackTrace(); } '2)*"ここで 'JLabel'を使うことはできません。*どうしてですか? 3)アプリケーションリソースはデプロイメント時に埋め込まれたリソースになるので、今のようにアクセスすることをお勧めします。 [タグ:埋め込みリソース]は、ファイルではなくURLでアクセスする必要があります。 [info。埋め込みリソースのページ](http://stackoverflow.com/tags/embedded-resource/info)を参照してください。 –
4) 'グラフィックスg = img.getGraphics(); g.drawImage(img、0、0、rootPane);このコードはイメージを元に戻し、何も達成しません。 5)「コードブロックのぶら下げ括弧の検出/修正」(http://meta.stackexchange.com/q/251795/155831)を参照して、修正できなくなった問題を解決してください。 –