gpt4 book ai didi

java - 将 JPanel 添加到 JFrame

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:31:57 29 4
gpt4 key购买 nike

我有一个将 JPanel 添加到 JFrame 的程序:

public class Test{

Test2 test = new Test2();
JFrame frame = new JFrame();

Test(){

...
frame.setLayout(new BorderLayout());
frame.add(test, BorderLayout.CENTER);
...

}

//main

...

}

public class Test2{

JPanel test2 = new JPanel();

Test2(){

...

}

}

我收到一条错误消息,要求我将“面板”类型更改为“组件”。我要修复这个错误吗?它要我做:Component panel = new Component();

最佳答案

public class Test{

Test2 test = new Test2();
JFrame frame = new JFrame();

Test(){
...
frame.setLayout(new BorderLayout());
frame.add(test, BorderLayout.CENTER);
...
}

//main
...
}

//public class Test2{
public class Test2 extends JPanel {

//JPanel test2 = new JPanel();

Test2(){
...
}

关于java - 将 JPanel 添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10454913/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com