// a simple exmple that can show the basis of swing
-------------------------------------------------------------------------
// import pakages which we need
import javax.swing.*;
import java.awt.*;
public class HelloCsdn
{
public static void main(String[] args)
{
HelloCsdnFrame frame=new HelloCsdnFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/** this part we construct a new frame HelloCsdnFrame
*/
-------------------------------------------------------------------------
class HelloCsdnFrame extends JFrame{
public HelloCsdnFrame()
{
setTitle("Hello CSDN.NET");
setSize(WIDTH,HEIGHT);
HelloCsdnPanel panel=new HelloCsdnPanel();
Container c=getContentPane();
c.add(panel);
}
public static final int WIDTH=300;
public static final int HEIGHT=200;
}
/**this part we extend our HelloCsdnFram to JFrame and
construct a new object HelloCsdnPanel and add it on the frame
/*
--------------------------------------------------------------------
class HelloCsdnPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("Hello CSDN.NET",MESSAGE_X,MESSAGE_Y);
}
public static final int MESSAGE_X=100;
public static final int MESSAGE_Y=100;
}
/** A panel that display a message
*/
我把此程序分为3part.每一部分都有注释,这一段代码是作什么用的。 一块儿来分析此程序:
做者:leeak出处:Java编程教学网责任编辑: 方舟 [ 2004-07-15 14:33 ]当咱们学习过了java中的基本语法,而且熟悉java的面向对象基础之后java
在第一部分
// import pakages which we need
import javax.swing.*;
import java.awt.*;
public class HelloCsdn
{
public static void main(String[] args)
{
HelloCsdnFrame frame=new HelloCsdnFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/** this part we construct a new frame HelloCsdnFrame
*/
能够看到咱们首先导入了2个包 swing 和 awt,建立了一个object对这个object咱们进行实例化, 而后用代码
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show(); 来实现关闭Frame,但不是结束程序,其停止的只是程序的主线程,
第二部分:
class HelloCsdnFrame extends JFrame{
public HelloCsdnFrame()
{
setTitle("Hello CSDN.NET");
setSize(WIDTH,HEIGHT);
HelloCsdnPanel panel=new HelloCsdnPanel();
Container c=getContentPane();
c.add(panel);
}
public static final int WIDTH=300;
public static final int HEIGHT=200;
}
/**this part we extend our HelloCsdnFram to JFrame and
construct a new object HelloCsdnPanel and add it on the frame
/*
在此咱们把咱们创建的object继承java的JFrame类,使他有JFrame的属性.行为.而后设置标题和大小,再次创建一个新的object HelloCsdnPanel 这是由于是在JFrame中实现的因此要创建容器c .把咱们创建的panel对象放入containerc中。
第三部分
class HelloCsdnPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("Hello CSDN.NET",MESSAGE_X,MESSAGE_Y);
}
public static final int MESSAGE_X=100;
public static final int MESSAGE_Y=100;
}
/** A panel that display a message
*/ 继续咱们继承刚创建的HelloCsdnPanel
到JPanel使咱们的对象有JPanel的属性,而后咱们才能调用在frame上输出字符的方法g.drawString
由此程序咱们一方面能够很好的看出java的核心思想----继承关系,另外一方面能够看出swing的基本构架是什么。
他有几个层,每一个层实现本身的什么功能。
5.自此咱们能够看出frame的内部结构:
------JFrame(底层)
|
---------JRoot
|
---------JLayeredPane
|
-----------菜单条
|
-----------内容窗格
|
-----------透明窗格(顶层)
而在这6个层中咱们最关系的是菜单条和内容窗格.由于它觉定咱们的frame是什么样的。
编程