将面板添加到主框架

时间:2023-01-28 18:02:57

I have a main frame named as "frame1". In "frame1" i want to add a panel to display something on the panel but I am not able to add the panel to my main frame i.e "frame1"

我有一个名为“frame1”的主框架。在“frame1”我想添加一个面板来显示面板上的东西,但我无法将面板添加到我的主框架,即“frame1”

 public void drawstack()
 {
    JPanel m1 = new JPanel(new BorderLayout());
    m1.setBorder(BorderFactory.createRaisedSoftBevelBorder());
    m1.setBackground(Color.red);
    frame1.add(m1);
 }

This is my code m getting an error at the last line i.e "frame1.add(m1);" error is

这是我的代码在最后一行收到错误,即“frame1.add(m1);”错误是

cannot find symbol : frame1.

4 个解决方案

#1


1  

frame1 has to be a field in your class or has to be passed as a parameter to your method

frame1必须是您的类中的一个字段,或者必须作为参数传递给您的方法

#2


1  

The error is telling you that the compiler cannot find a variable (or class) named frame1. In order for the flagged statement to work you will need to declare a variable named frame1 and instantiate it with a JFrame object, something like this:

该错误告诉您编译器无法找到名为frame1的变量(或类)。为了使标记语句起作用,您需要声明一个名为frame1的变量,并使用JFrame对象对其进行实例化,如下所示:

JFrame frame1 = new JFrame();
frame1.add(m1);

Note: You can make this variable a member of the class as well if you want to access it from other methods of the same class.

注意:如果要从同一个类的其他方法访问它,也可以使此变量成为类的成员。

Note: The intantiated JFrame referenced by frame1 is initially invisible, you will need to make it visible by calling setVisible(true) on it

注意:frame1引用的实例化JFrame最初是不可见的,您需要通过调用setVisible(true)使其可见

If your class derives from JFrame and you want to add the panel to the frame represented by the current object, you can use the this reference instead of frame1:

如果您的类派生自JFrame并且您想要将面板添加到当前对象所表示的框架,则可以使用此引用而不是frame1:

this.add(m1);

in this ase you can even leave the this out:

在这个问题上你甚至可以把它留下来:

add(m1);

#3


0  

obviously theres an error..you haven't initialized a variable named frame1.. use this.add(m1); instead..it should work.

显然是一个错误..你还没有初始化一个名为frame1的变量..使用this.add(m1);相反..它应该工作。

#4


0  

If method you posted is inside your class that extends JFrame you need to call

如果你发布的方法在扩展JFrame的类中,你需要调用

getContentPane().add(m1);

Also if you want to add more than 1 element to your frame use layout managers.

此外,如果要向框架添加多个元素,请使用布局管理器。

#1


1  

frame1 has to be a field in your class or has to be passed as a parameter to your method

frame1必须是您的类中的一个字段,或者必须作为参数传递给您的方法

#2


1  

The error is telling you that the compiler cannot find a variable (or class) named frame1. In order for the flagged statement to work you will need to declare a variable named frame1 and instantiate it with a JFrame object, something like this:

该错误告诉您编译器无法找到名为frame1的变量(或类)。为了使标记语句起作用,您需要声明一个名为frame1的变量,并使用JFrame对象对其进行实例化,如下所示:

JFrame frame1 = new JFrame();
frame1.add(m1);

Note: You can make this variable a member of the class as well if you want to access it from other methods of the same class.

注意:如果要从同一个类的其他方法访问它,也可以使此变量成为类的成员。

Note: The intantiated JFrame referenced by frame1 is initially invisible, you will need to make it visible by calling setVisible(true) on it

注意:frame1引用的实例化JFrame最初是不可见的,您需要通过调用setVisible(true)使其可见

If your class derives from JFrame and you want to add the panel to the frame represented by the current object, you can use the this reference instead of frame1:

如果您的类派生自JFrame并且您想要将面板添加到当前对象所表示的框架,则可以使用此引用而不是frame1:

this.add(m1);

in this ase you can even leave the this out:

在这个问题上你甚至可以把它留下来:

add(m1);

#3


0  

obviously theres an error..you haven't initialized a variable named frame1.. use this.add(m1); instead..it should work.

显然是一个错误..你还没有初始化一个名为frame1的变量..使用this.add(m1);相反..它应该工作。

#4


0  

If method you posted is inside your class that extends JFrame you need to call

如果你发布的方法在扩展JFrame的类中,你需要调用

getContentPane().add(m1);

Also if you want to add more than 1 element to your frame use layout managers.

此外,如果要向框架添加多个元素,请使用布局管理器。