簡體   English   中英

為什么在調用JFrame時我的JPanel不顯示?

[英]Why is my JPanel not showing up when called into JFrame?

最初,我在GUI中創建了按鈕和JPanel,但是我想將它們從GUI中刪除,並為它們創建一個不同的類,因為我必須添加它們。

這是以下代碼,給了我一個問題。

問題是我的JPanel沒有顯示。 我將VButtons類擴展到JPanel,假設將JPanel添加到JFrame是可行的,但是沒有運氣。

有什么建議么?

public static void vGui()  {
        JFrame frame = new JFrame();
        B b;
        b = new B();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        frame.setLayout(new BorderLayout());
        frame.add(b, BorderLayout.WEST);
         frame.setVisible(true);
    }





     public static class B extends JPanel {

        B() {

              JPanel Panel1 = new JPanel();
              JRadioButton 1st button = new JRadioButton("1st button");
              JRadioButton 2nd button = new JRadioButton("2nd button");

              Panel1.add(1st button);
              Panel1.add(2nd button);
              Panel1.setLayout(new GridLayout(9,1))


   }




    }

VButtons extends JPanel <-這本身就是一個JPanel

JPanel Panel1 <-但是您要在Panel1中添加所有內容

並且panel1剛剛被創建並且未使用。

因此,刪除Panel1。

只需調用add方法,因為您位於JPanel的子類中。

如果更容易閱讀,則可以調用this.add(...)

您需要使用左側相交處的頂點來編譯右側,以布局整個網格。 如果使用9,1,則頂點不會相交。 我喜歡您使用removeVertex但這是不必要的。

暫無
暫無

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

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