Programing

JLabel의 배경색 설정 방법

c10106 2022. 4. 21. 21:24
반응형

JLabel의 배경색 설정 방법

나의JPanel, 나는 a의 배경을 설정했다.JLabel다른 색으로시험이라는 단어가 보이고 파란색이지만 배경은 전혀 바뀌지 않는다.어떻게 하면 보여줄 수 있을까?

this.setBackground(Color.white);
JLabel label = new JLabel("Test");
label.setForeground(Color.blue);
label.setBackground(Color.lightGray);
this.add(label);

사용하다

label.setOpaque(true);

그렇지 않으면 기본값은 다음과 같으므로 배경을 칠하지 않는다.opaque이다false을 위해JLabel.

JavaDocs에서:

참일 경우 구성요소는 범위 내의 모든 픽셀을 도색한다.그렇지 않으면 구성 요소는 픽셀의 일부 또는 전체를 도색하지 않을 수 있으며, 따라서 기본 픽셀이 투과할 수 있다.

자세한 내용은 Java 자습서 레이블 사용 방법을 참조하십시오.

JLabel 배경은 기본적으로 투명하다.불투명도를 다음과 같이 참으로 설정하십시오.

label.setOpaque(true);

setOpaque(참)를 true로 설정해야 하며, 배경은 양식에 따라 그려지지 않을 것이다.나는 읽으면서 만약 그것이 사실로 설정되지 않는다면 그것의 픽셀의 일부를 형태에 칠할 것이라고 생각한다.배경은 기본적으로 투명해서 적어도 내가 보기엔 이상하지만 프로그래밍 방식에서는 아래와 같이 사실대로 설정해야 한다.

      JLabel lb = new JLabel("Test");
      lb.setBackground(Color.red);
      lb.setOpaque(true); <--This line of code must be set to true or otherwise the 

JavaDocs에서

세토파크

public void setOpaque(boolean isOpaque)
  If true the component paints every pixel within its bounds. Otherwise, 
  the component may not paint some or all of its pixels, allowing the underlying 
  pixels to show through.
  The default value of this property is false for JComponent. However, 
  the default value for this property on most standard JComponent subclasses 
   (such as JButton and JTree) is look-and-feel dependent.

Parameters:
isOpaque - true if this component should be opaque
See Also:
isOpaque()

백그라운드에서 가져온 항목인지 확인하십시오.java.awt.Color소포로 보내주셨어요.

당신 안에main방법, 즉public static void main(String[] args), 이미 가져온 메서드를 호출하십시오.

JLabel name_of_your_label=new JLabel("the title of your label");
name_of_your_label.setBackground(Color.the_color_you_wish);
name_of_your_label.setOpaque(true);

NB: 불투명도를 설정하는 것은 가시성에 영향을 줄 것이다.Java의 사례 민감도를 기억하십시오.

참조URL: https://stackoverflow.com/questions/2380314/how-do-i-set-a-jlabels-background-color

반응형