2014-09-02 2 views
0

자바에서 프로그래밍 중이고 Jframe을 새로 고침하고 루프를 통해 색상을 변경하고 싶지만 프레임을 다시로드 할 수는 없지만 새 프레임 만 만들 수 있습니다.JFrame을 다시로드하는 방법 - Java

package frames; 
import java.awt.Color; 

import javax.swing.*; 

public class Frame1 { 
public static void main(String[] args) 
{ 
    int num = 0; 



    while (num<255) 
    { 
     num +=1; 
     JFrame frame = new JFrame(); 
     frame.setSize(400,300); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setBackground(new Color(num,num,num)); 
     frame.setTitle(""); 

    } 
} 
} 
+0

당신이) (JFrame의 번호 무효화() 다음 JFrame의 번호의 유효성 검사를 호출하는 시도 해 봤나 그 3 점 리팩토링있어 참조하십시오? –

답변

1
    당신은 하나의 프레임 (당신이 그들의 255를 작성하는)
  1. 돈을 사용할 필요가
  2. while 루프를 사용하여 배경을 변경하십시오. 스윙 타이머를 대신 사용하십시오. How to Use Swing Timers
  3. Event Dispatch Thread에서 모든 Swing 응용 프로그램을 실행하십시오. 여기

Initial Threads은 당신의 코드는

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 

public class Frame1 { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       final JFrame frame = new JFrame(); 
       frame.getContentPane().setBackground(
         new Color(0, 0, 0)); 
       Timer timer = new Timer(10, new ActionListener() { 
        int num = 0; 
        public void actionPerformed(ActionEvent e) { 
         if (num > 255) { 
          ((Timer) e.getSource()).stop(); 
         } else { 
          frame.getContentPane().setBackground(
            new Color(num, num, num)); 
          num++; 
         } 
        } 
       }); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setSize(300, 300); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
       timer.start(); 
      } 
     }); 
    } 
} 
0

이렇게하면 프레임이 하나만 만들어지고 색을 조정하고 원하는 새로 고침을 적용합니다.

package frames; 
import java.awt.Color; 

import javax.swing.*; 

public class Frame1 { 
public static void main(String[] args) 
{ 
    int num = 0; 

    JFrame frame = new JFrame(); 
    frame.setSize(400,300); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    frame.setTitle(""); 

    while (num<255) 
    { 
     num +=1; 

     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
     // Here, we can safely update the GUI 
     // because we'll be called from the 
     // event dispatch thread 
      frame.getContentPane().setBackground(new Color(num,num,num)); 
      frame.pack(); 
      frame.repaint(); 
     } 
     } 
} 
    }); 


    } 
} 
} 

는 어쩌면 난 그냥 위로를 혼합}하지만 당신은이 작업을 수행해야하는 방법 아이디어를 얻을해야

관련 문제