2017-12-12 4 views
0

아래의 프로그램을 실행하려고합니다. 배경색이 PINK와 String 인 "This is a test"프로그램을 실행하려고합니다. 화이트java.awt.Frame.setBackground (Color arg0)가 PINK 색상을 표시하지 않습니다.

1) Test.java에서

package Practice; 

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

public class Test extends JFrame{ 

    public static void main(String[] args){ 

     DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN); 
     Test test = new Test(); 
     test.run(dm); 
    } 

    public void run(DisplayMode dm){ 

     setBackground(Color.PINK); 
     setForeground(Color.WHITE); 
     setFont(new Font("Arial", Font.PLAIN, 24)); 

     Screen s = new Screen(); 

     try{ 
      s.setFullScreen(dm, this); 
      try{ 
       Thread.sleep(5000); 
      }catch(Exception ex){ 
       ex.printStackTrace(); 
      } 
     }finally{ 
      s.restoreScreen(); 
     } 

    } 

    public void paint(Graphics g){ 

     g.drawString("This is a test.", 200, 200); 
    } 
} 

2) Screen.java

package Practice; 

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

public class Screen { 

    private GraphicsDevice videoCard; 

    public Screen(){ 

     GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     videoCard = env.getDefaultScreenDevice(); 
    } 

    public void setFullScreen(DisplayMode dm, JFrame window){ 

     window.setUndecorated(true); 
     window.setResizable(false); 
     videoCard.setFullScreenWindow(window); 

     if(dm != null && videoCard.isDisplayChangeSupported()){ 
      try{ 
       videoCard.setDisplayMode(dm); 
      }catch(Exception ex){ 
       ex.printStackTrace(); 
      } 
     } 
    } 

    public Window getFullScreenWindow(){ 

     return videoCard.getFullScreenWindow(); 
    } 

    public void restoreScreen(){ 

     Window w = videoCard.getFullScreenWindow(); 

     if(w != null){ 

      w.dispose(); 
     } 

     videoCard.setFullScreenWindow(null); 
    } 

} 

예상 결과 :

전체 화면 표시 배경 색상 PINK 및 String "이것은 테스트입니다." 화이트

실제 결과에서 :

전체 화면 표시 배경 색상 BLACK 및 문자열 ". 이것은 테스트입니다" 화이트.

저는 Windows 시스템에서 이식을 실행하고 있습니다.

+0

은 프레임의 배경과 전경을 설정하지 마십시오

내가 한 말의 일 예이다. 페인트 메서드의 Graphics 객체는 프레임 배경이나 전경을 처리하지 않습니다. 그래픽 객체에 색상을 설정해야합니다 ('g.setColor (Color.PINK);). 또한, ** 결코 ** paint 메소드를 오버라이드하지 말고 (대신에'paintComponent'를 사용하십시오.) 프레임 클래스에서는 사용하지 마십시오.JPanel ('paintComponent' 메소드에서)을 확장하는 클래스에서 모든 페인트 작업을 할 수 있고, 그 프레임을 프레임에 추가 할 수 있습니다. – Ansharja

+0

@Ansharja 음, 현재이 두 클래스 만 있습니다. 그래픽 객체를 설정 한 후 (g.setColor (Color.PINK);). 또한 나는 같은 문제가 생겼다. – Anand

+0

문자열을 그리기 전에 그래픽 색상 *을 설정하고 있습니까? – Ansharja

답변

1

내가 처음 코멘트에 말했듯이, 많은 것들을 당신이하지 말아야 할이 있습니다 :

  • 직접 일부 사용자 지정 그림을 만들기 위해 JFrame을 사용할 수 없습니다. 대신 JPanel을 사용하십시오. 이 경우에는 JFrame을 확장 할 필요가 없으며 어떤 기능도 추가하지 않습니다.

  • 프레임의 배경색을 변경하려면 프레임의 내용 창의 배경색을보다 잘 설정해야합니다. 또한 문자열을 그리는 데 사용되는 색을 변경하려면 Graphics.setColor()을 호출해야합니다.

  • paint 메서드를 재정의하지 마십시오. 대신 JComponent'spaintComponent 메서드를 재정의하십시오. 또한, 메소드의 첫 번째 행은 부모 페인트 메소드를 호출하여 구성 요소가 다른 작업을 수행하기 전에 정상적으로 페인트 할 수있게해야합니다.

마지막으로, 당신은 당신의 Screen 클래스 내부에서 수행 할 작업을 취소하지,하지만 Thread.sleep를 호출하면이 실제로 나쁜 관행이며, 나에게 모든 코드를 훨씬 이해가되지 않습니다.

전체 화면 모드를 설정하는 가장 좋은 방법은 프레임에 setExtendedState (JFrame.MAXIMIZED_BOTH)으로 전화하는 것입니다.

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Graphics; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.border.EmptyBorder; 
public class Test 
{ 
    public static void main (String [] a) { 
     SwingUtilities.invokeLater (new Runnable() { 
      @Override public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
    private static void createAndShowGUI() { 
     JFrame frame = new JFrame ("App"); 
     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
     frame.setContentPane (new DrawPanel (Color.PINK, Color.WHITE)); 
     frame.pack(); 
     // frame.setExtendedState (JFrame.MAXIMIZED_BOTH); // You can use this instruction to have full screen mode. 
     frame.setLocationRelativeTo (null); 
     frame.setVisible (true); 
    } 
} 
class DrawPanel extends JPanel 
{ 
    Color foregroundColor; 

    public DrawPanel (Color backgroundColor, Color foregroundColor) { 
     setBackground (backgroundColor); 
     this.foregroundColor = foregroundColor; 
    } 
    @Override public Dimension getPreferredSize() { 
     return new Dimension (400, 400); 
    } 
    @Override protected void paintComponent (Graphics g) { 
     super.paintComponent (g); 
     g.setColor (foregroundColor); 
     g.setFont (new Font ("Arial", Font.PLAIN, 24)); 
     g.drawString ("This is a test.", 200, 200); 
    } 
} 

스크린 샷 :

enter image description here

관련 문제