java 中的JPanle 在使用setBackground()为什么会失效
packagecom.Swing.corejava19;importjava.awt.Color;importjava.awt.GridLayout;importjava...
package com.Swing.corejava19;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test_5_4 {
static final int WIDTH = 200;
static final int HEIGHT = 300;
public static void main(String []args)
{
JFrame jf = new JFrame();
jf.setSize(WIDTH, HEIGHT);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
JPanel contenpan = new JPanel();
jf.setContentPane(contenpan);
contenpan.setBackground(Color.red); //颜色设置失效。。?????
// jf.setContentPane(contenpan);
JButton b1= new JButton("港币");
JButton b2 = new JButton("人民币");
JButton b3 = new JButton("美元");
JButton b4 = new JButton("欧元");
JButton b5 = new JButton("英镑");
JButton b6 = new JButton("主板");
JButton b7 = new JButton("内存");
JButton b8 = new JButton("硬盘");
JButton b9 = new JButton("显示器");
contenpan.setLayout(new GridLayout(3,3));
contenpan.add(b1);
contenpan.add(b2);
contenpan.add(b3);
contenpan.add(b4);
contenpan.add(b5);
contenpan.add(b6);
contenpan.add(b7);
contenpan.add(b8);
contenpan.add(b9);
jf.pack();
}
} 展开
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test_5_4 {
static final int WIDTH = 200;
static final int HEIGHT = 300;
public static void main(String []args)
{
JFrame jf = new JFrame();
jf.setSize(WIDTH, HEIGHT);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
JPanel contenpan = new JPanel();
jf.setContentPane(contenpan);
contenpan.setBackground(Color.red); //颜色设置失效。。?????
// jf.setContentPane(contenpan);
JButton b1= new JButton("港币");
JButton b2 = new JButton("人民币");
JButton b3 = new JButton("美元");
JButton b4 = new JButton("欧元");
JButton b5 = new JButton("英镑");
JButton b6 = new JButton("主板");
JButton b7 = new JButton("内存");
JButton b8 = new JButton("硬盘");
JButton b9 = new JButton("显示器");
contenpan.setLayout(new GridLayout(3,3));
contenpan.add(b1);
contenpan.add(b2);
contenpan.add(b3);
contenpan.add(b4);
contenpan.add(b5);
contenpan.add(b6);
contenpan.add(b7);
contenpan.add(b8);
contenpan.add(b9);
jf.pack();
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询