(我見過的其他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傳遞給構造函數,跳轉到它,它也停在相同的語句處。
在添加語句之前(即在諸如大小的主要設置中),顯示器就是框架本身,具有任何設置。
我很確定這是非常愚蠢的問題,因爲它昨天工作,不知道我改變了什麼,但我只是放棄了。
謝謝,它的工作 –