// 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.每一部分都有注释,这一段代码是做什么用的. 一起来分析此程序: 在第一部分 // 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对象放入container c中.
第三部分
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是什么样的.
总结:可以看出Swing是java很好的表现,怪不得关于Swing的书可以写成很厚的一本,本章只是教那些初学者,对于java有一个更好的认识,不近近是停留在控制台上的编程.
swing世界是很奇妙的,有待大家一起去探索
| 第1页: Swing入门基础 |