求问java,为什么我这个里面的30行10列格子显示不出来?我的代码哪儿错了吗?中间部分
importjava.awt.Color;importjava.awt.Container;importjava.awt.Dimension;importjava.awt...
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class zhuomian {
public static void main(String []args){
windowdesk one = new windowdesk();
one.setTitle("ATEST");
one.setBounds(500,200,500,500);
GridLayout grid=new GridLayout(5,1);
one.setLayout(grid);
one.init();
one.setVisible(true);
}
}
class windowdesk extends JFrame{
void init(){
JPanel panel1=new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
panel1.setBackground(Color.blue);
add(panel1);
GridLayout grid=new GridLayout(10,10);
panel2.setLayout(grid);
JLabel a[][]= new JLabel[10][10];
for(int i = 0;i<10;i++){
for(int j = 0 ;j<10;j++){
a[i][j]=new JLabel();
if((i+j)%2==0){
a[i][j].setBackground(Color.black);
}
else{
a[i][j].setBackground(Color.white);
}
panel2.add(a[i][j]);
}
}
add(panel2);
panel3.setLayout(grid);
for(int i = 0;i<10;i++){
for(int j = 0 ;j<10;j++){
a[i][j]=new JLabel();
if((i+j)%2==0){
a[i][j].setBackground(Color.black);
}
else{
a[i][j].setBackground(Color.white);
}
panel3.add(a[i][j]);
}
}
add(panel3);
panel4.setLayout(grid);
for(int i = 0;i<10;i++){
for(int j = 0 ;j<10;j++){
a[i][j]=new JLabel();
if((i+j)%2==0){
a[i][j].setBackground(Color.black);
}
else{
a[i][j].setBackground(Color.white);
}
panel4.add(a[i][j]);
}
}
add(panel4);
panel5.setBackground(Color.green);
add(panel5);
}
} 展开
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class zhuomian {
public static void main(String []args){
windowdesk one = new windowdesk();
one.setTitle("ATEST");
one.setBounds(500,200,500,500);
GridLayout grid=new GridLayout(5,1);
one.setLayout(grid);
one.init();
one.setVisible(true);
}
}
class windowdesk extends JFrame{
void init(){
JPanel panel1=new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
panel1.setBackground(Color.blue);
add(panel1);
GridLayout grid=new GridLayout(10,10);
panel2.setLayout(grid);
JLabel a[][]= new JLabel[10][10];
for(int i = 0;i<10;i++){
for(int j = 0 ;j<10;j++){
a[i][j]=new JLabel();
if((i+j)%2==0){
a[i][j].setBackground(Color.black);
}
else{
a[i][j].setBackground(Color.white);
}
panel2.add(a[i][j]);
}
}
add(panel2);
panel3.setLayout(grid);
for(int i = 0;i<10;i++){
for(int j = 0 ;j<10;j++){
a[i][j]=new JLabel();
if((i+j)%2==0){
a[i][j].setBackground(Color.black);
}
else{
a[i][j].setBackground(Color.white);
}
panel3.add(a[i][j]);
}
}
add(panel3);
panel4.setLayout(grid);
for(int i = 0;i<10;i++){
for(int j = 0 ;j<10;j++){
a[i][j]=new JLabel();
if((i+j)%2==0){
a[i][j].setBackground(Color.black);
}
else{
a[i][j].setBackground(Color.white);
}
panel4.add(a[i][j]);
}
}
add(panel4);
panel5.setBackground(Color.green);
add(panel5);
}
} 展开
1个回答
展开全部
因为JLabel缺省的值是背景透明的,所以要为每个JLabel对象设定背景不透明为真,即a[i][j].setOpaque(true);才行.
完整的Java程序如下:(改动的地方见注释)
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class zhuomian {
public static void main(String []args){
windowdesk one = new windowdesk();
one.setTitle("ATEST");
one.setBounds(500,200,500,500);
GridLayout grid=new GridLayout(5,1);
one.setLayout(grid);
one.init();
one.setVisible(true);
}
}
class windowdesk extends JFrame{
void init(){
JPanel panel1=new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
panel1.setBackground(Color.blue);
add(panel1);
GridLayout grid=new GridLayout(10,10);
panel2.setLayout(grid);
JLabel a[][]= new JLabel[10][10];
for(int i = 0;i<10;i++){
for(int j = 0 ;j<10;j++){
a[i][j]=new JLabel();
a[i][j].setOpaque(true);//这里加一句
if((i+j)%2==0){
a[i][j].setBackground(Color.black);
}
else{
a[i][j].setBackground(Color.white);
}
panel2.add(a[i][j]);
}
}
add(panel2);
panel3.setLayout(grid);
for(int i = 0;i<10;i++){
for(int j = 0 ;j<10;j++){
a[i][j]=new JLabel();
a[i][j].setOpaque(true);//这里加一句
if((i+j)%2==0){
a[i][j].setBackground(Color.black);
}
else{
a[i][j].setBackground(Color.white);
}
panel3.add(a[i][j]);
}
}
add(panel3);
panel4.setLayout(grid);
for(int i = 0;i<10;i++){
for(int j = 0 ;j<10;j++){
a[i][j]=new JLabel();
a[i][j].setOpaque(true);//这里加一句
if((i+j)%2==0){
a[i][j].setBackground(Color.black);
}
else{
a[i][j].setBackground(Color.white);
}
panel4.add(a[i][j]);
}
}
add(panel4);
panel5.setBackground(Color.green);
add(panel5);
}
}
运行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询