简体   繁体   中英

JFrame does not show more than one panel

I am trying to create a simple color selector panel in java for a bigger project. I have a frame that is supposed to include a panel for RGB sliders and three text fields show their values. I am able to add the slider panel with no problem but when I try to add the text field panel the whole thing messes up and none of the panels show. My only question is how to fix this issue of the panels. Thank you.

Here is my code:

//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();
    }

}

At the end of constructor FrameObject , you need to add following line.

this.pack(); 

The pack method sizes the frame so that all its contents are at or above their preferred sizes.

You need to pack your frame.

//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();
    }
}

Take setVisible(true); and make it the last thing you call after you established you 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 is lazy when it comes to update the UI, it allows you to add and remove a number of components in "batches" without updating the UI or performing a new layout pass, which can be expensive.

If you need to update the UI dynamically, remember to call revalidate followed by repaint when you want the UI to be updated

Also, prefer pack over setSize as pack will take into account the frame decorations and the differences in font metrics and other things which can change between platforms and systems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM