java图形用户界面添加背景颜色不成功的解决方案

总结:背景颜色不成功,那么使用这个方法试试。getContentpane();

package clientFrame;

import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

public class see extends JFrame {
	// 做一个登陆的密码,账户框。2个h文本框,2个label
	JLabel jl1, jl2;
	JButton jb;
	JPasswordField pf;
	JTextField tf1, tf2;

	public see() {
		jl1 = new JLabel("用户名:");
		jb = new JButton("登  陆");
		jl2 = new JLabel("密   码:");
		tf1 = new JTextField(10);
		pf = new JPasswordField(10);
		tf2 = new JTextField(10);
		this.setVisible(true);

		this.setLayout(new FlowLayout());// 添加背景色
		this.getContentPane().setBackground(Color.red);// 核心,设置背景颜色,用这种方法。getContentPane()
		this.add(jl1);

		this.add(tf1);
		this.add(jl2);
		this.add(pf);
		// this.add(tf2);
		this.add(jb);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(200, 200);
	}

	public static void main(String[] args) {

		see s = new see();
	}
}

  

原文地址:https://www.cnblogs.com/langlove/p/3451641.html