我在JPanel(jp1)上放了JFrame(fr1),但是jp2会去哪里?

我想在jp2上显示jp1中的fr1吗?

(按钮“开始!”和“调试”应同时显示在窗口上)

怎么做?

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class F extends JFrame {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    F fr1 = new F();
    P jp1 = new P();
    jp1.add(new JButton("start!"));
    fr1.setSize(1024, 768);
    fr1.add(jp1, BorderLayout.CENTER);
    fr1.setVisible(true);

    }

}

class P extends JPanel{
    private JPanel jp2;
    private JButton b2;
    public P(){
        jp2 = new JPanel();
        jp2.setBackground(Color.green);
        b2 = new JButton("DEBUGGING.");
        jp2.add(b2);
    }
}

最佳答案

您永远不会在jp2中将P添加到任何内容

class P extends JPanel{
    private JPanel jp2;
    private JButton b2;
    public P(){
        jp2 = new JPanel();
        jp2.setBackground(Color.green);
        b2 = new JButton("DEBUGGING.");
        jp2.add(b2);
        //???
    }
}


添加add(jp2);作为最后一条语句

07-24 12:47