Java 응용 프로그램을 작성하고 BufferedImage를 페인트하려고합니다. 내 메인에서는 JFrame이 생성되고 JPanel이 생성되고 JFrame에 추가됩니다. 다시 그리기를 위해 스레드가 시작되었지만 아무것도 표시되지 않으며 paintComponent의 System.out.println()도 호출되지 않습니다. 나는 이걸 많이 봤어,하지만 난 내 문제를 해결할 아무것도 찾지 못했습니다.JPanel의 PaintComponent가 호출되지 않습니다.
내가 뭘 잘못하고 왜 잘못 된거야 ???
내 코드 :
주요 + 스레드:
public class Main extends Thread
{
public static Frame frame = new Frame();
public static void main(String[] args) throws IllegalStateException, IOException
{
frame.activePanel = new LoginPanel();
frame.add(frame.activePanel);
new Main();
}
public Main()
{
this.start();
}
@Override
public void run()
{
while(true)
{
if(Main.frame.activePanel != null)
Main.frame.activePanel.repaint();
try{Thread.sleep(15);}catch(InterruptedException e){}
}
}
}
JFrame에 :
public class Frame extends JFrame
{
public JPanel activePanel = null;
public Frame()
{
super();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(this.getToolkit().getScreenSize().width/2 - 640,this.getToolkit().getScreenSize().height/2 - 400,1279,799);
this.setResizable(false);
this.setUndecorated(true);
this.setVisible(true);
}
}
와 JPanel의 :
public class LoginPanel extends JPanel
{
BufferedImage loginImg;
public LoginPanel() throws IOException
{
loginImg = ImageIO.read(new File("src/images/Login.PNG"));
}
@Override
protected void paintComponent(Graphics g)
{System.out.println("painting");
g.drawImage(loginImg, 0, 0, null);
}
}
확신이 없지만 paintComponent를 재정의하지 않으면 어떻게됩니까? – Frakcool
동일한, 나는 백색 회색 창을 얻는다. 슬프게도,이 방법은 그것이 호출되었는지 확인할 수 없습니다. – Geosearchef
와아, 와아! Swing에서 쓰레드를 다룰 때 조심해야하며, 다시 그리기를 주기적으로 호출 할 필요가 없다. 칠한 후에 컴포넌트를 추가/제거하면 다시 칠하기를 원할 것이다. [swing in threads 사용 방법] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html)을 읽으십시오. [Event dispatch thread] (http : /docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html). – DSquare