2016-09-24 10 views
0
public static void main(String[] args) { 
    myForm.setSize(500, 400); 
    myForm.setLocation(0, 0); 
    myForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    myForm.setVisible(true); 
    // try { 
     // myForm.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:\\Users\\Heshamm\\Desktop\\light.jpg"))))); 
     //} catch (IOException e1) { 
     //JOptionPane.showMessageDialog(null, "Error !!!", null, JOptionPane.PLAIN_MESSAGE); 
     //} 
    draw.mai(myForm, jp1, button, tf[0], tf[1], tf[2], tf[3]); 

を追加し、これがそう、別のクラスの舞()関数のJFrameの背景として写真

 public void mai(JFrame myForm, JPanel jp1, JButton[] button, JTextField tf1, JTextField tf2, JTextField tf3, JTextField tf4) 
    { 

     myForm.getContentPane().removeAll(); 
     jp1.removeAll(); 
     jp1.add(button[0]); 
     jp1.add(button[1]); 
     jp1.add(button[2]); 
     jp1.add(button[3]); 
     jp1.add(button[9]); 
     jp1.add(button[4]); 
     tf1.setText(""); 
     tf2.setText(""); 
     tf3.setText(""); 
     tf4.setText(""); 
     Hotel.update(); 
    } 

大丈夫です、私はこのコードを持っている、と私はIMGの背景として表示されることがしたかったですボタンの後ろにあるものの、その中のtryやwhatsのコメントを外すと、フレームにはボタンのない背景しか表示されません。どうすれば修正できますか?

ここにupdate()関数があります。

public static void update() 
{ 
    cbChoice=""; 
    myForm.add(jp1); 
    myForm.repaint(); 
    myForm.revalidate(); 
} 

ありがとうございます。

答えて

1

イメージをJLabelに追加して、JLabelのような背景をストレッチすることができます。

簡単な例があります:

ImageIcon icon = createImageIcon("images/middle.gif"); 
    . . . 
    JLabel label1 = new JLabel("Image and Text", icon, JLabel.CENTER); 
    label1.setSize(frame width, frame height); 
+0

私はあなた(PIC)ファイルパスから必ずしてください – Ahmed

+0

このエラー 「メソッドcreateImageIcon(文字列)型ホテルについて定義されていません」を取得。 –

+0

ImageIcon icon = createImageIcon( "C:\\ Users \\ Heshamm \\ Desktop \\ light.jpg"); 元の投稿に投稿したコードと同じパス、もう1つは正常に写真を見つけることができます。 – Ahmed

関連する問題