swing入门例子

// 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的面向对象基础以后


  在第一部分

// 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是什么样的。

posted @ 2014-01-13 20:59  秋晓驹  阅读(419)  评论(0编辑  收藏  举报