我搜索了一種將圖片添加爲我的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
http://stackoverflow.com/questions/18777893/jframe-background-image –
是啊,我已經看到了一個和我都試過的方法,但都沒有工作 – Veronique