簡體   English   中英

JFrame不顯示多個面板

[英]JFrame does not show more than one panel

我試圖在Java中為大型項目創建一個簡單的顏色選擇器面板。 我有一個框架,該框架應該包括用於RGB滑塊的面板,並且三個文本字段顯示其值。 我能夠毫無問題地添加滑塊面板,但是當我嘗試添加文本字段面板時,整個過程變得混亂了,所有面板都沒有顯示。 我唯一的問題是如何解決此問題的面板。 謝謝。

這是我的代碼:

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

}

在構造函數FrameObject ,您需要添加以下行。

this.pack(); 

打包方法調整框架的大小,以便其所有內容均等於或大於其首選大小。

您需要收拾行李架。

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

采取setVisible(true); 並在建立用戶界面后將其作為最后調用的內容

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

在更新UI時,Swing很懶惰,它使您可以在“批處理”中添加和刪除許多組件,而無需更新UI或執行新的布局遍歷,這可能會很昂貴。

如果您需要動態更新UI,請記住在要更新UI時先調用revalidate然后進行repaint

另外,與setSize ,首選pack ,因為pack將考慮框架裝飾以及字體規格的差異以及其他可能在平台和系統之間變化的因素

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM