2017-04-21 88 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会考虑到框架的装饰和差异字体指标和其他可以在平台和系统之间切换的东西