2011-12-08 73 views
-1

私はこれを理解できないようです。
私のプロジェクトを続行するには、これが必要です。
AWWWは、私は(すなわち、それは見えるように)私はあなたがそれを実現JFrame前にJLabelインスタンスを追加する必要が背景画像としてのJLabel

import javax.swing.*; 
import java.awt.*; 

@SuppressWarnings("serial") 
public class MainFrame extends JFrame { 


public static void Draw(){ 
    DrawFrame(); 
} 


public static void DrawFrame(){ 
    int h = 600; 
    int w = 340; 
    JFrame frame = new JFrame(); 
    JLabel background1 = new JLabel(new ImageIcon("/res/mariocraft_main.png")); 


    frame.setResizable(false); 
    frame.setSize(h, w); 
    frame.setTitle("MarioCraft"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
    frame.add(background1); 

    background1.setVisible(true); 
    background1.setIcon(new ImageIcon("/res/mariocraft_main.png")); 
    background1.setText("Background failed to load"); 

    } 

} 
+0

のために採取したサンプルのコードスニペットを参照してください-1あなたの質問は何ですか?無意味なテキストを追加するのではなく、おそらくもっと徹底的にしたいことを説明しようとするべきです。 – PTBG

+0

私はJLabelをバックグラウンドとして使用しようとしています。質問はタイトルにあります – Alek345

+0

何を求めていますか?あなたのコードの問題は何ですか?コードを質問にダンプして「修正する」と言って、あなたが言うことを説明しないでください。 –

答えて

3

JLabelは常に画像を実際のサイズで表示しますので、手動でフレームのサイズを設定しないでください。

代わりに、コードのようなものでなければなりません:

JLabel background1 = new JLabel(new ImageIcon("/res/mariocraft_main.png")); 

JFrame frame = new JFrame();  
frame.add(background1); 
frame.pack(); 
frame.setResizable(false);  
frame.setVisible(true);  
1

投稿することを可能にするためにこれを追加する必要があります。また、次の3つの通話を削除します。

background1.setVisible(true); 
background1.setIcon(new ImageIcon("/res/mariocraft_main.png")); 
background1.setText("Background failed to load"); 

これらは完全に不要です。また、コンポーネントに背景イメージを設定するもう1つの方法は、paintComponentメソッドをオーバーライドして、そのイメージを直接Graphicsオブジェクトに描画することです。

+0

paintComponentのexampelを作成できますか? – Alek345

0

あなたはJFrameための背景画像としてJLabelを設定しますか。その後、

frame.setContentPane(new JLabel(new ImageIcon("someimage.jpg")); 

here

frame.setLayout(new BorderLayout()); 
frame.setContentPane(new JLabel(new ImageIcon("someimage.jpg"))); 
frame.setLayout(new FlowLayout()); 
l1=new JLabel("Here is a button"); 
b1=new JButton("I am a button"); 
frame.add(l1); 
frame.add(b1); 
0
import java.awt.Container; 
import java.awt.FlowLayout; 


import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 


public class Mainframe extends JFrame 
{ 
    public JLabel image ; 



    public Container c; 

    public Mainframe() 
    { 
     c=this.getContentPane(); 
     image=new JLabel(new ImageIcon("bg.jpg")); 
     image.setSize(500, 550); 

     c.setLayout(new FlowLayout()); 
     c.add(image); 

     add(image); 




     this.setSize(500, 550); 
     this.show(); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    public static void main(String[] args) 
    { 
      new Mainframe(); 
    } 

} 
+0

ここで画像を中央に置くことができます。画像をフレーム全体として設定できます – gangu