2012-02-21 6 views
2

이것은 아마도 어리석은 오류 일뿐입니다. iv'e는 방금 .awt 패키지를 학습하기 시작했습니다. 나는 편지에 튜토리얼을 따라 갔다. 비디오에서 그의 창 배경은 빨간색이고 코드에는 오류가 없지만 배경색은 변경되지 않는다. 도움 주셔서 감사합니다!Java 창에서 배경색을 설정하지 않습니까?

import java.awt.Color; 
import javax.swing.*; 
public class Test { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
JFrame f = new JFrame(); 
f.setVisible(true); 
f.setSize(350,350); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f.setTitle("Window"); 
f.setBackground(Color.RED); 
    } 

} 
+0

잘 모르겠지만 setVisible을 마지막으로 이동하면 어떤 방식 으로든 새로 고치지 않고 현재 설정을 가져올 수 있습니다. – Randy

+0

그것을하지 않으면 나는 두려워! 색맹 인 경우 동영상과 내 애플릿은 다음과 같습니다! http://y.imgur.com/LQMOL.png – Icy100

답변

10

1) JFrame 할 수 없어, 당신은 콘텐츠 창 예에 대한 Color을 변경해야

JFrame.getContentPane().setBackground(myColor) 

2) 당신은 예를 들어, invokeLater

에() main 방식의 GUI 관련 코드를 포장해야합니다

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

public class GUI { 

    public GUI() { 
     JFrame frame = new JFrame(); 
     frame.setTitle("Test Background"); 
     frame.setLocation(200, 100); 
     frame.setSize(600, 400); 
     frame.addWindowListener(new WindowAdapter() { 

      @Override 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 
     frame.getContentPane().setBackground(Color.BLUE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 
       GUI gUI = new GUI(); 
      } 
     }); 
    } 
} 
+0

감사합니다. – Icy100

4

대신

f.setBackground(Color.RED); 

의 전화를

f.getContentPane().setBackground(Color.RED); 

콘텐츠 창이 표시됩니다.

보조 메모로 JFrame 팁이 있습니다. f.add(child)으로 전화하면 자녀가 콘텐츠 창에 추가됩니다.

+0

고마워 ... 나는 더 빨리 대답하는 법을 배워야하고 대답 할 때 나의 동료를 무시해야한다. :) – Paul

관련 문제