2017-04-21 67 views
0

我想創建一個簡單的顏色選擇器面板在Java爲一個更大的項目。我有一個框架,應該包括一個RGB滑塊面板和三個文本字段顯示其值。我能夠毫無問題地添加滑塊面板,但是當我嘗試添加文本字段面板時,整個事情就會混亂起來,並且沒有任何面板顯示。我唯一的問題是如何解決面板的這個問題。謝謝。JFrame不顯示多個面板

這裏是我的代碼:

//importing necessary libraries 
import java.awt.*; 
import javax.swing.*; 

//Object extends JFrame 
public class FrameObject extends JFrame 
{ 
    //declaring the panels, one for the color sliders and the other for the text fields 
    private JPanel color_panel; 
    private JPanel textFileds; 

    //arrays to hold the J components for further efficiency 
    private JSlider[] RGB = new JSlider[3]; 
    private JTextField[] RGBFileds = new JTextField[3]; 

    public FrameObject() 
    { 
     //Preparing the frame 
     super("Color panel"); 
     setVisible(true); 
     setSize(400, 400); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //A grid layout to give desired orientation 
     color_panel = new JPanel(new GridLayout(3, 1)); 
     textFileds = new JPanel(new GridLayout(3, 1)); 

     //initializing the individual components through a loop in the arrays 
     for(int c=0; c<RGB.length; c++) 
     { 
      RGB[c] = new JSlider(SwingConstants.HORIZONTAL,0,255,100); 
      RGBFileds[c] = new JTextField(12); 

      //Adding each component to its specific panel 
      color_panel.add(RGB[c]); 
      textFileds.add(RGBFileds[c]); 
     } 

     //adding the sub panels to the main panel. 
     add(color_panel,BorderLayout.CENTER); 
     add(textFileds,BorderLayout.EAST); 
    } 


} 

public class FrameTest 
{ 
    public static void main(String[] args) 
    { 
     FrameObject f = new FrameObject(); 
    } 

} 
+0

只有在填充幀內容後才調用'setVisible(true)'。將運行時添加組件添加到可見組件需要重新驗證和重新繪製。 – kiheru

+0

謝謝一堆。 –

回答

0

在構造FrameObject結束後,您需要添加以下行。

this.pack(); 

pack方法調整框架的大小,以使其所有內容都達到或超過其首選大小。

0

你需要打包你的框架。

//importing necessary libraries 
import java.awt.*; 
import javax.swing.*; 

//Object extends JFrame 
public class FrameObject extends JFrame 
{ 
    //declaring the panels, one for the color sliders and the other for the text fields 
    private JPanel color_panel; 
    private JPanel textFileds; 

    //arrays to hold the J components for further efficiency 
    private JSlider[] RGB = new JSlider[3]; 
    private JTextField[] RGBFileds = new JTextField[3]; 

    public FrameObject() 
    { 
     //Preparing the frame 
     super("Color panel"); 
     setVisible(true); 
     setSize(400, 400); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //A grid layout to give desired orientation 
     color_panel = new JPanel(new GridLayout(3, 1)); 
     textFileds = new JPanel(new GridLayout(3, 1)); 

     //initializing the individual components through a loop in the arrays 
     for(int c=0; c<RGB.length; c++) 
     { 
      RGB[c] = new JSlider(SwingConstants.HORIZONTAL,0,255,100); 
      RGBFileds[c] = new JTextField(12); 

      //Adding each component to its specific panel 
      color_panel.add(RGB[c]); 
      textFileds.add(RGBFileds[c]); 
     } 

     //adding the sub panels to the main panel. 
     add(color_panel,BorderLayout.CENTER); 
     add(textFileds,BorderLayout.EAST); 
     pack(); 
    } 

    public static void main(String[] args) 
    { 
     FrameObject f = new FrameObject(); 
    } 
} 
0

採取setVisible(true);並使它的最後一件事,你叫後你建立你UI

public FrameObject() { 
    //Preparing the frame 
    super("Color panel"); 
    //setVisible(true); 
    //setSize(400, 400); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //... 

    //adding the sub panels to the main panel. 
    add(color_panel, BorderLayout.CENTER); 
    add(textFileds, BorderLayout.EAST); 

    pack(); 
    setVisible(true); 
} 

Swing是懶惰的,當涉及到更新UI,它可以讓你添加和刪除一些不需要更新UI或執行新的佈局過程,這可能很昂貴。

如果您需要動態地更新UI,記得其次repaintrevalidate當你想要的UI進行更新

而且打電話,喜歡pack超過setSize作爲pack會考慮到框架的裝飾和差異字體指標和其他可以在平臺和系統之間切換的東西