JFrame & paint - neues JFrame

Hallo MRS,

das mit dem setBackground klappt deswegen nicht, da ein JFrame (Swing) aus mehreren Komponenten besteht. Bei einem Frame (AWT) hingegen würde man direkt auf das Frame zeichnen. Um die Hintergrundfarbe eines JFrames zu ändern holt man sich erst das Contentpane mit getContentPane() und zeichnet da drauf mit setBackground().

Java:
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.*;

public class SetBackgroundExample extends JFrame {

	public SetBackgroundExample() {
		super("SetBackgroundExample");
		this.setSize(300, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationByPlatform(true);

		this.getContentPane().setBackground(new Color(22, 222, 22));

		this.add(new JLabel("Label", new ImageIcon("b1.jpg"), JLabel.LEADING),
				BorderLayout.NORTH);
		this.add(new JButton("Button", new ImageIcon("b2.jpg")),
				BorderLayout.SOUTH);

		this.setVisible(true);
	}

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


Vg Erdal
 
Zurück