2016-03-19 31 views
0

(我見過的其他2個問題是類似於標題礦,但它們是不同的,並沒有提供解決我的問題。)爲什麼在調用線程時JFrame無法顯示?

嗨,

我有下面的代碼簡單的線條顯示。我在main中聲明瞭一個JFrame,然後調用DrawGraph1類的一個新實例,傳遞JFrame作爲參數。 在構造函數中,我調用了一個線程(EventQueue.invokeLater)。構造函數使用的JFrame,並用它來創建一些線條和字符串,等等)

(抱歉縮進不當,已經調整了很多)

package test; 
import java.awt.*; 
import java.awt.geom.*; 
import java.text.DateFormatSymbols; 
import javax.swing.*; 

public class test { 
    public static void main(String[] args){     
     final JFrame frame = new JFrame(); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(800, 700); 
     frame.setVisible(true); 
     DrawGraph1 obj = new DrawGraph1(frame); 
} 
} 
class DrawGraph1{   
    DrawGraph1(final JFrame frame){ 
     EventQueue.invokeLater(new Runnable(){    
     @Override    
     public void run(){     
     frame.setTitle("LineDrawing");  
     frame.add(new JComponent() 
      { 
       @Override 
       public void paintComponent(Graphics g) 
       { 
        Graphics2D g2 = (Graphics2D) g; 
        Line2D line = new Line2D.Double(); 
        int decrement = 0, label = 0;     

        g2.setColor(Color.red);   
        g.drawString("Red Line ->> High temperatures", 330, 110); 
        g2.setColor(Color.green);   
        g.drawString("Green Line ->> Low temperatures", 330, 130); 
        } });}});}} 

所以,根據我的測試中,程序到達構造,通過所述框架,並啓動該線程,但顯然它停止在該行

frame.add(new JComponent() 

隨着NetBeans調試下劃線(或其它)add方法。我也嘗試過在main調用線程,將JFrame傳遞給構造函數,跳轉到它,它也停在相同的語句處。

在添加語句之前(即在諸如大小的主要設置中),顯示器就是框架本身,具有任何設置。

我很確定這是非常愚蠢的問題,因爲它昨天工作,不知道我改變了什麼,但我只是放棄了。

回答

1

我剛纔改變了這兩行的順序。

frame.setVisible(true); 
DrawGraph1 obj = new DrawGraph1(frame); 

DrawGraph1 obj = new DrawGraph1(frame); 
frame.setVisible(true); 

和輸出:

Output

+0

謝謝,它的工作 –

1

首先,你通過了的JFrame的JPanel的。您應該創建JPanel,並將其傳遞給JFrame。

以下是我所做的主要更改。

  1. 所有的Swing GUI創建必須位於事件調度線程上。這包括JFrame和JPanel。我在主方法中添加了一個Runnable,並調用EventQueue invokeLater方法在Event Dispatch Thread上啓動Swing GUI。

  2. 我移動了主要方法中的所有JFrame代碼,以及DrawGraph1類paintComponent方法中的所有JPanel繪圖代碼。 paintComponent方法僅用於繪畫。在其他課程和/或其他方法中進行其他處理。

  3. JFrame方法必須按特定順序調用。按照我在代碼中調用它們的順序調用JFrame方法。

  4. 我添加了一個調用super.paintComponent到你的paintComponent方法。這將維護Swing塗料鏈並清除繪圖面板。

  5. 我將GUI的大小從JFrame移至JPanel。您關心繪圖面板的大小,而不是整個GUI。

下面是修改後的代碼。我重命名類,以便它不會與我的測試包中的其他代碼發生衝突。你應該改回它。

package com.ggl.testing; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.Line2D; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class SimpleJPanelTest { 

    public static void main(String[] args) { 
     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       final JFrame frame = new JFrame("Line Drawing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       frame.add(new DrawGraph1()); 
       frame.pack(); 
       frame.setLocationByPlatform(true); 
       frame.setVisible(true); 
      } 
     }; 
     EventQueue.invokeLater(runnable); 
    } 
} 

class DrawGraph1 extends JPanel { 

    private static final long serialVersionUID = 6733473371292195071L; 

    public DrawGraph1() { 
     setPreferredSize(new Dimension(800, 700)); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     Graphics2D g2 = (Graphics2D) g; 
     Line2D line = new Line2D.Double(); 
     int decrement = 0, label = 0; 

     g2.setColor(Color.red); 
     g.drawString("Red Line ->> High temperatures", 330, 110); 
     g2.setColor(Color.green); 
     g.drawString("Green Line ->> Low temperatures", 330, 130); 
    } 

} 
+0

Thx,有很多新的東西在那裏。 –

相關問題