我搜索了一种为我的JFrame添加Image作为Bakground的方法。 我找到了一些问题,并尝试了几种解决方案,但我的图像不会出现,我不知道什么是错的(我是一个noob btw xd) 我看到其他问题,但他们没有帮助我,我努力尝试,但无法找到我的错!所以请(将我的帖子标记为重复的人)。
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
答案 0 :(得分: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