能稍微介绍下 JComponent 与JPanel的区别吗?我在看的是《Java核心技术》上卷,里面第七章用的是JComponent,但是后面用的是JPanel ,前面的绘图之类的怎么用JPanel完成呢?

解决方案 »

  1.   

    在绘图问题上,这两者没啥区别,反正都是用 Graphics 来做的
      

  2.   

    看书又有点小疑惑了,《Java核心技术》第十章,讲的是部署应用程序和applet,这张不大懂啊,有谁能稍微解释下这到底都是在讲什么啊,我现在跳掉会不会有太大影响啊
      

  3.   


    我们老师说,Applet现在没啥利用价值了……因为有了JavaScript等……
    我直接跳过了……《疯狂Java讲义(第二版)》直接就没提到Applet
      

  4.   

    JComponent 类提供: 使用 Swing 架构的标准组件和自定义组件的基类。 
    可由程序员指定,或者由用户在运行时选择(可选)的“可插入外观”(L&F)。每个组件的外观都由 UI 委托 提供,UI 委托是一个继承自 ComponentUI 的对象。有关更多信息,请参阅 The Java Tutorial 中的 How to Set the Look and Feel。 
    全面的键击处理。有关更多信息,请参阅 The Swing Connection 中的文档 Keyboard Bindings in Swing。 
    对工具提示的支持:光标停留在组件时所弹出的简短描述。有关更多信息,请参阅 The Java Tutorial 中的 How to Use Tool Tips。 
    可访问性支持。JComponent 包含 Accessible 接口中的所有方法,但是它实际上不实现该接口。由扩展 JComponent 的每个类负责实现该接口。 
    对特定于组件属性的支持。通过 putClientProperty(java.lang.Object, java.lang.Object) 和 getClientProperty(java.lang.Object) 方法,可以将“名称-对象”对与继承自 JComponent 的任意对象进行关联。 
    用于绘制的基础设施,包括双缓冲和对边框的支持。有关更多信息,请参阅 The Java Tutorial 中的 Painting 和 How to Use Borders 章节。 
    有关这些主题的更多信息,请参阅 Swing 包描述和 The Java Tutorial 中的 The JComponent Class 一节。 
    JComponent 及其子类记录了某些属性的默认值。例如,JTable 记录的默认行高为 16。每个具有 ComponentUI 的 JComponent 子类都将创建该 ComponentUI 作为其构造方法的一部分。为了提供特定的外观,每个 ComponentUI 都可以在创建它的 JComponent 上设置各种属性。例如,自定义的外观可能要求 JTable 的行高为 24。而所记录的默认值是安装 ComponentUI 前的属性值。如果需要为特定的属性使用具体值,则应该显式地设置它。 public class JPanelextends JComponentimplements AccessibleJPanel 是一般轻量级容器。有关 JPanel 的示例和面向任务的文档,请参阅 The Java Tutorial 的 How to Use Panels 一节。
      

  5.   

    GUI学着玩就好,不要太纠结了,用得也不多,布局还很丑。
      

  6.   


    JPanel 对应的UI代理是PanelUI,对于不同 L & F, 背景颜色会有差异等.
    而JComponent是抽象类,没法实例化,所以也不需要UI代理
      

  7.   

    区别:1 - JComponent 是 abstract 类, JPanel 不是
    2 - JPanel 默认为 opaque, JComponent 则默认为 not opaque
    3 - JPanel 默认的 LayoutManager 为 FlowLayout, JComponent 默认的 LayoutManager 为 null总结:没什么太大的区别。
      

  8.   

    不是同一层次的,基本没啥可比性吧,JComponent是抽象类看一下介绍The base class for all Swing components except top-level containers. To use a component that inherits from JComponent, you must place the component in a containment hierarchy whose root is a top-level Swing container. Top-level Swing containers -- such as JFrame, JDialog, and JApplet -- are specialized components that provide a place for other Swing components to paint themselves. For an explanation of containment hierarchies, see Swing Components and the Containment Hierarchy, a section in The Java Tutorial. The JComponent class provides(提供了): 1. The base class for both standard and custom components that use the Swing architecture. (基类)2. A "pluggable look and feel" (L&F) that can be specified by the programmer or (optionally) selected by the user at runtime. The look and feel for each component is provided by a UI delegate -- an object that descends from ComponentUI. See How to Set the Look and Feel in The Java Tutorial for more information. (LnF)3. Comprehensive keystroke handling. See the document Keyboard Bindings in Swing, an article in The Swing Connection, for more information. (全面的按键输入处理)4. Support for tool tips -- short descriptions that pop up when the cursor lingers over a component. See How to Use Tool Tips in The Java Tutorial for more information. (支持tool tips)5. Support for accessibility. JComponent contains all of the methods in the Accessible interface, but it doesn't actually implement the interface. That is the responsibility of the individual classes that extend JComponent. (对accessibility的支持)6. Support for component-specific properties. With the putClientProperty(java.lang.Object, java.lang.Object) and getClientProperty(java.lang.Object) methods, you can associate name-object pairs with any object that descends from JComponent. (对component-specific的支持)7. An infrastructure for painting that includes double buffering and support for borders. For more information see Painting and How to Use Borders, both of which are sections in The Java Tutorial.(双缓冲和对边界的支持)而JPanel的话是它的子类,自然继承了它的特性
      

  9.   

    可以下载个netbeans工具,制作绘图的,完了保存为java文件,很方便的。鸣人。
      

  10.   

    查看api文档啊   上面有很详细的说明啊