2016-12-14 98 views
-1

我搜索了一種將圖片添加爲我的JFrame的Bakground的方式。 我發現了一些問題,並嘗試了幾個解決方案,但我的圖像不會出現,我不知道什麼是錯的(我是一個noob btw xd) 我看到其他問題,但他們沒有幫助我,我努力嘗試,但找不到我的錯!所以請(人類將我的帖子標記爲重複)。JFrame背景圖片不起作用

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


public class prognose extends Frame { 
    // Anfang Attribute 
    private JTextField spendenbetrag = new JTextField(); 
    private JTextField streamzeit = new JTextField(); 
    private JLabel sBetragL = new JLabel(); 
    private JLabel sZeitL = new JLabel(); 
    private JButton prognosebutton = new JButton(); 
    private ImageIcon prognosebuttonIcon = new ImageIcon("C:\\Users\\user\\Documents\\Programmieren\\Workspace\\images\\Button.png"); 
    private JTextField ergebnis = new JTextField(); 
    // Ende Attribute 

    public prognose() { 
    // Frame-Initialisierung 
    super(); 
    addWindowListener(new WindowAdapter() { 
     public void windowClosing(WindowEvent evt) { dispose(); } 
    }); 
    int frameWidth = 455; 
    int frameHeight = 580; 
    setSize(frameWidth, frameHeight); 
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 
    int x = (d.width - getSize().width)/2; 
    int y = (d.height - getSize().height)/2; 
    setLocation(x, y); 
    setTitle("Loot für die Welt"); 
    setResizable(false); 
    Panel cp = new Panel(null); 
    add(cp); 

    // Anfang Komponenten 

    spendenbetrag.setText(""); 
    cp.add(spendenbetrag); 
    streamzeit.setText(""); 
    cp.add(streamzeit); 
    sBetragL.setText("Aktueller Spendenbetrag"); 
    cp.add(sBetragL); 
    streamzeit.setBounds(152, 184, 145, 25); 
    spendenbetrag.setBounds(152, 112, 145, 25); 
    sBetragL.setBounds(152, 80, 145, 25); 
    sZeitL.setBounds(152, 152, 155, 25); 
    sZeitL.setText("Aktuelle Streamzeit"); 
    cp.add(sZeitL); 
    prognosebutton.setBounds(184, 224, 80, 280); 
    prognosebutton.setText(""); 
    prognosebutton.setMargin(new Insets(2, 2, 2, 2)); 
    prognosebutton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
     prognosebutton_ActionPerformed(evt); 
     } 
    }); 
    prognosebutton.setIcon(prognosebuttonIcon); 
    prognosebutton.setBorderPainted(false); 
    prognosebutton.setBackground(Color.WHITE); 
    prognosebutton.setBorder(BorderFactory.createEtchedBorder(0, Color.DARK_GRAY, new Color(0xC0C0C0))); 
    prognosebutton.setIconTextGap(0); 
    cp.setBackground(Color.WHITE); 
    setUndecorated(false); 
    cp.add(prognosebutton); 
    ergebnis.setBounds(152, 512, 145, 25); 
    ergebnis.setText(""); 
    ergebnis.setEditable(false); 
    cp.add(ergebnis); 
    // Ende Komponenten 

    setVisible(true); 
    setLayout(new BorderLayout()); 
    setSize(455,580); 
    setVisible(true); 
    JLabel background=new JLabel(new ImageIcon("C:\\Users\\user\\Documents\\Programmieren\\Workspace\\images\\Background.png")); 

    add(background); 

    background.setLayout(new FlowLayout()); 


    } // end of public prognose 

    // Anfang Methoden 

    public static void main(String[] args) { 
    new prognose(); 
    } // end of main 

    public void prognosebutton_ActionPerformed(ActionEvent evt) { 
    // TODO hier Quelltext einfügen 
    String a; 
    String b; 
    a = spendenbetrag.getText(); 
    b = streamzeit.getText(); 
    double d; 
    double e = Double.parseDouble(a); 
    double f = Double.parseDouble(b); 
    d = e*(60/f)*48; 
    d = ((double)((int)(d*100)))/100; 
    String g = String.valueOf(d); 
    ergebnis.setText(g); 
    } // end of prognosebutton_ActionPerformed 

    // Ende Methoden 
} // end of class prognose 
+1

http://stackoverflow.com/questions/18777893/jframe-background-image –

+0

是啊,我已經看到了一個和我都試過的方法,但都沒有工作 – Veronique

回答

0

延長JFrame代替Frame,使用setComponentPane設置底色,移動背景標籤上的聲明到代碼的頂部。將所有組件添加到該標籤。

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


public class prognose extends JFrame { 
    // Anfang Attribute 
    private JTextField spendenbetrag = new JTextField(); 
    private JTextField streamzeit = new JTextField(); 
    private JLabel sBetragL = new JLabel(); 
    private JLabel sZeitL = new JLabel(); 
    private JButton prognosebutton = new JButton(); 
    private ImageIcon prognosebuttonIcon = new ImageIcon("C:\\test\\rak.png"); 
    private JTextField ergebnis = new JTextField(); 
    // Ende Attribute 

    public prognose() { 
    // Frame-Initialisierung 
    super(); 
    addWindowListener(new WindowAdapter() { 
     public void windowClosing(WindowEvent evt) { dispose(); } 
    }); 
    int frameWidth = 455; 
    int frameHeight = 580; 
    setSize(frameWidth, frameHeight); 
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 
    int x = (d.width - getSize().width)/2; 
    int y = (d.height - getSize().height)/2; 
    setLocation(x, y); 
    setTitle("Loot für die Welt"); 
    setResizable(false); 
    JLabel background=new JLabel(new ImageIcon("C:\\test\\rak.png")); 

    setContentPane(background); 
    background.setLayout(new FlowLayout()); 
    Panel cp = new Panel(null); 
    background.add(cp); 

    // Anfang Komponenten 

    spendenbetrag.setText(""); 
    background.add(spendenbetrag); 
    streamzeit.setText(""); 
    background.add(streamzeit); 
    sBetragL.setText("Aktueller Spendenbetrag"); 
    background.add(sBetragL); 
    streamzeit.setBounds(152, 184, 145, 25); 
    spendenbetrag.setBounds(152, 112, 145, 25); 
    sBetragL.setBounds(152, 80, 145, 25); 
    sZeitL.setBounds(152, 152, 155, 25); 
    sZeitL.setText("Aktuelle Streamzeit"); 
    background.add(sZeitL); 
    prognosebutton.setBounds(184, 224, 80, 280); 
    prognosebutton.setText(""); 
    prognosebutton.setMargin(new Insets(2, 2, 2, 2)); 
    prognosebutton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
     prognosebutton_ActionPerformed(evt); 
     } 
    }); 
    prognosebutton.setIcon(prognosebuttonIcon); 
    prognosebutton.setBorderPainted(false); 
    prognosebutton.setBackground(Color.WHITE); 
    prognosebutton.setBorder(BorderFactory.createEtchedBorder(0, Color.DARK_GRAY, new Color(0xC0C0C0))); 
    prognosebutton.setIconTextGap(0); 
    background.setBackground(Color.WHITE); 
    setUndecorated(false); 
    background.add(prognosebutton); 
    ergebnis.setBounds(152, 512, 145, 25); 
    ergebnis.setText(""); 
    ergebnis.setEditable(false); 
    background.add(ergebnis); 
    // Ende Komponenten 

    setVisible(true); 
    setLayout(new BorderLayout()); 
    setSize(455,580); 
    setVisible(true); 




    } // end of public prognose 

    // Anfang Methoden 

    public static void main(String[] args) { 
    new prognose(); 
    } // end of main 

    public void prognosebutton_ActionPerformed(ActionEvent evt) { 
    // TODO hier Quelltext einfügen 
    String a; 
    String b; 
    a = spendenbetrag.getText(); 
    b = streamzeit.getText(); 
    double d; 
    double e = Double.parseDouble(a); 
    double f = Double.parseDouble(b); 
    d = e*(60/f)*48; 
    d = ((double)((int)(d*100)))/100; 
    String g = String.valueOf(d); 
    ergebnis.setText(g); 
    } // end of prognosebutton_ActionPerformed 

    // Ende Methoden 
} // end of class prognose 
+0

現在看來似乎不知道setComponentPane,我得到這個錯誤:*異常在線程「main」java.lang.Error:未解決的編譯問題: \t在預測方式setComponentPane(JLabel)未定義類型預測 \t。 (prognose.java:36) \t在prognose.main(prognose.java:95)* – Veronique

+0

是否更換延伸的框體,以JFrame的 –

+0

是的,我有:// – Veronique