2017-08-22 117 views
-1

我在顯示JFrame上的組件時遇到問題。我正在關閉當前窗口並打開新窗口並希望顯示jLabel,但沒有任何事情發生。代碼如下:JFrame不顯示組件

   Frame[] nF = DBChooser.getFrames(); 

       nF[0].setVisible(false); 
       JFrame windoow = new JFrame("Processing");      
       JPanel pan = new JPanel(); 
       windoow.setPreferredSize(new Dimension(400, 150)); 
       pan.setPreferredSize(new Dimension(400, 150));     

       JLabel textLabel = new JLabel ("Processing..."); 
       textLabel.setLayout(null); 
       pan.setLayout(null); 
       windoow.setLayout(null);     
       pan.add(textLabel); 
       pan.revalidate(); 
       pan.repaint();     
       windoow.getContentPane().add(pan); 
       windoow.setLocationRelativeTo(null);      
       windoow.pack();     
       windoow.setVisible(true); 

我感謝所有幫助

+0

1)Java的圖形用戶界面有不同的OS」,屏幕大小,屏幕分辨率等方面的工作在不同的地區使用不同的PLAFs。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 2)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 3)以最小尺寸提供ASCII圖形或簡單的GUI圖形佈局* –

+0

..如果可調整大小,則具有更大的寬度和高度。 –

回答

1

這是因爲你設置一個空的佈局窗口,面板不指定任何寬度,lenght或位置,或者使用一些LayoutManager或設置這些屬性(如邊界)。一個空的LayoutManager意味着你需要自己設置所有的東西,因爲沒有任何東西(沒有LayoutManager)會自動放置你的元素。這個例子使用一個BorderLayout的,它創建了一個很好的效果:

enter image description here

代碼:

import java.awt.BorderLayout; 
import java.awt.Dimension; 

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

public class Test { 
    public static void main(String[] args) { 
     JFrame windoow = new JFrame("Processing"); 
     JPanel pan = new JPanel(); 
     windoow.setPreferredSize(new Dimension(400, 150)); 
     pan.setPreferredSize(new Dimension(400, 150)); 

     JLabel textLabel = new JLabel("Processing..."); 
     textLabel.setLayout(null); 
     pan.setLayout(new BorderLayout()); 
     windoow.setLayout(new BorderLayout()); 
     pan.add(textLabel); 
     pan.revalidate(); 
     pan.repaint(); 
     windoow.getContentPane().add(pan); 
     windoow.setLocationRelativeTo(null); 
     windoow.pack(); 
     windoow.setVisible(true); 
    } 
} 
+0

仍然是一樣的,標籤不顯示 – johansonik

+0

@johansonik當我運行相同的代碼時,如何不顯示它,並且我已經發布了帶有消息的窗口圖像? –

+0

我不知道爲什麼,但我可以看到沒有任何文字的空白窗口... – johansonik

1

爲什麼需要這麼多setLayout(null);?我刪除它們,它的工作

public class DBChooser extends Frame { 

    public static void main(String args[]) { 
     Frame[] nF = DBChooser.getFrames(); 
//  nF[0].setVisible(false); 
     JFrame windoow = new JFrame("Processing"); 
     JPanel pan = new JPanel(); 
     windoow.setPreferredSize(new Dimension(400, 150)); 
     pan.setPreferredSize(new Dimension(400, 150)); 
     JLabel textLabel = new JLabel("Processing..."); 
//  textLabel.setLayout(null); 
//  pan.setLayout(null); 
//  windoow.setLayout(null);     
     pan.add(textLabel); 
     pan.revalidate(); 
     pan.repaint(); 
     windoow.getContentPane().add(pan); 
     windoow.setLocationRelativeTo(null); 
     windoow.pack(); 
     windoow.setVisible(true); 
    } 
} 
+0

它沒有幫助,標籤不顯示 – johansonik

+0

它在我的電腦上正常工作。也許你需要在這裏發佈更多的代碼 –

+0

什麼是'nF [0] .setVisible(false);'?你確定你的代碼可以在沒有評論的情況下進行編譯? –