简体   繁体   English

为什么在调用JFrame时我的JPanel不显示?

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

Initially, I created my buttons and my JPanel in my GUI but I want to take them out of my GUI and create a different class for them since I have to add on to them. 最初,我在GUI中创建了按钮和JPanel,但是我想将它们从GUI中删除,并为它们创建一个不同的类,因为我必须添加它们。

Here is the following code that's giving me an issue. 这是以下代码,给了我一个问题。

The issue being that my JPanel isn't showing up. 问题是我的JPanel没有显示。 I extended the VButtons class to JPanel assuming that adding the JPanel to JFrame would work but no luck. 我将VButtons类扩展到JPanel,假设将JPanel添加到JFrame是可行的,但是没有运气。

Any suggestions? 有什么建议么?

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 <-- This itself is a JPanel VButtons extends JPanel <-这本身就是一个JPanel

JPanel Panel1 <-- But you are adding everything inside Panel1 JPanel Panel1 <-但是您要在Panel1中添加所有内容

And panel1 is just created and left unused. 并且panel1刚刚被创建并且未使用。

So, remove Panel1. 因此,删除Panel1。

Just call add method, because you are inside a subclass of JPanel. 只需调用add方法,因为您位于JPanel的子类中。

You can call this.add(...) if that is clearer to read. 如果更容易阅读,则可以调用this.add(...)

You need to use the vertex from the left intersection to compile the right for layout of the entire grid. 您需要使用左侧相交处的顶点来编译右侧,以布局整个网格。 The vertices won't intersect if you use 9,1. 如果使用9,1,则顶点不会相交。 I like how you you used removeVertex but it is unnecessary. 我喜欢您使用removeVertex但这是不必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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