我也是刚刚开始学习java,只知道,Jpanel是面板,一般的组件像JTextField,JTextArea,JLabel等,都应放在panel上,而不能直接放在frame上。

解决方案 »

  1.   

    对Jpanel是面板,一般的组件像
      

  2.   

    Jpanel不是顶级窗口,不能直接输出。它必须放在象JFrame这样的顶级窗口上才能输出。
      

  3.   

    JFrame是顶级窗口,JPanel是一个版面或者容器,它只能依赖JFrame而存在
    比如从下面的程序可以看出来
    import java.sql.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;public class mainFrame extends JFrame
    {
    public mainFrame()
    {
    init();
    }

    void init()
    {
    Container contain=this.getContentPane();
    JPanel pan=new JPanel(new FlowLayout());
    contain.setLayout(new BorderLayout());
    contain.add(txt,BorderLayout.NORTH);
    contain.add(txtarea,BorderLayout.CENTER);
    pan.add(btn1,FlowLayout.LEFT);
    pan.add(btn2,FlowLayout.CENTER);
    pan.add(btn3,FlowLayout.RIGHT);
    contain.add(pan,BorderLayout.SOUTH);


    this.setSize(400,400);
    this.setVisible(true);
    }

    JTextField txt=new JTextField("017100512");
    JTextArea  txtarea=new JTextArea(20,40);
    JButton btn1=new JButton("Search");
    JButton btn2=new JButton("Update");
    JButton btn3=new JButton("Modify");}
      

  4.   

    JFrame是顶级窗口,JPanel是一个容器,它只能依赖JFrame而存在
      

  5.   

    纠正一个错误 : JLable 是可以直接放在 JFrame 上的.
    可以运行如下的例子看效果,例子代码来自 sun.import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;/* FrameDemo.java requires no other files. */
    public class FrameDemo {
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Suggest that the L&F (rather than the system)
            //decorate all windows.  This must be invoked before
            //creating the JFrame.  Native look and feels will
            //ignore this hint.
            JFrame.setDefaultLookAndFeelDecorated(true);        //Create and set up the window.
            JFrame frame = new JFrame("FrameDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        JLabel emptyLabel = new JLabel("");
            emptyLabel.setPreferredSize(new Dimension(175, 100));
            frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);        //Display the window.
            frame.pack();
            frame.setVisible(true);
        }    public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
      

  6.   

    还是有区别的,jframe是javax.swing的组件它不能直接添加jpanel,而frame是java.awt的组件它可以添加panel,一般来说控件添加在jpanel和panel上!!!
      

  7.   

    楼上各位所言正确:
    简而言之:
    相同:frame,panel既是容器,又是组件。
    不同:是panel不能独立存在。就是不能作为顶级存在。
      

  8.   

    JFrame是顶级窗口,JPanel是一个版面或者容器,它只能依赖JFrame而存在
    子窗口与父窗口的关系
      

  9.   

    Frame,Panel都是容器,都可以添加其他组件,但Frame可以作为顶级窗口,而Panel不能.
    JFrame和JPanel是AWT的后继版本SWING中相应的组件
      

  10.   

    JFrame是顶级窗口,JPanel是一个版面或者容器,JPanel可以加在JApplet中,也可加在Frame中,
     JPanel不可单独存在!