java中设置了背景颜色,为什么不能显示?

2024-12-29 14:05:54
推荐回答(4个)
回答1:

private JLabel jla=new JLabel("用户名");;
private JLabel jlb=new JLabel("主机名");
private JLabel jlc=new JLabel("IP地址");;
不要ADD到this而是ADD到jpList

如:
import java.awt.*;
import javax.swing.*;

public class T extends JFrame
{

private JLabel jla=new JLabel("用户名");;
private JLabel jlb=new JLabel("主机名");
private JLabel jlc=new JLabel("IP地址");;

JPanel jpList;
JScrollPane jsp;

public T()
{
//界面
JPanel jpList=new JPanel();
jpList.setBackground(Color.blue);

this.setLayout(null);

jla.setBounds(30,20,40,40);
jpList.add(jla);

jlb.setBounds(90,20,40,40);
jpList.add(jlb);

jlc.setBounds(150,20,40,40);
jpList.add(jlc);

jsp=new JScrollPane(jpList);
jsp.setBounds(5,20,280,150);

this.add(jsp);

this.setTitle("Java局域网文件传输器");
this.setBounds(100,100,300,400);
this.setBackground(Color.blue);
this.setVisible(true);
}

public static void main(String[] args)
{

new T();

}
}

回答2:

现在可以了

import java.awt.*;
import javax.swing.*;

public class TransAndChat extends JFrame
{

private JLabel jla=new JLabel("用户名");;
private JLabel jlb=new JLabel("主机名");
private JLabel jlc=new JLabel("IP地址");;

public Test5()
{
//界面

jla.setBounds(30,20,40,40);
this.add(jla);

jlb.setBounds(90,20,40,40);
this.add(jlb);

jlc.setBounds(150,20,40,40);
this.add(jlc);

JPanel jp=new JPanel();
jp.setBounds(5,20,280,150);
jp.setBackground(Color.blue);
this.add(jp);

this.setTitle("Java局域网文件传输器");
this.setBounds(100,100,300,400);
this.setBackground(Color.blue);
this.setVisible(true);
this.setLayout(null);
}

public static void main(String[] args)
{

new TransAndChat ();

}
}

回答3:

第一,前面那个JPanel jpList; 要去掉,你在后面重新定义了一次~~
第二,……

回答4:

this.setBackground(Color.blue)完全是多余的,你设置的是面板里的jpList.setBackground(Color.blue); 而且JPanel jp=new JPanel()定义的2次