import javax.swing.*; import java.awt.event.*; import java.awt.*;
class JInternalFrame1 extends JFrame implements ActionListener{ JDesktopPane desktopPane; int count = 1; public JInternalFrame1() { super("JInternalFrame1"); Container contentPane = this.getContentPane(); contentPane.setLayout(new BorderLayout()); JButton b = new JButton("Create New Internal Frames"); 可以用var来进行声明变量,声明的变量的类型由初始化此变量值的类型决定, 这一点有点象VB6.0当中的变体型, //Demo namespace Test2008 { class Program { static void Main(string[] args) { //用var关键字声明, var i=0; //声明必须初始化 //var i; //不能初始为空 var i= null; Console.WriteLine(i); //可以再次赋值 // i = 10;
//不能再次赋值为其它类型 // i = "0"; Console.WriteLine(i);
//用var声明可以赋给不同类型的值,他的类型由初始化的值的类型决定 var s = "Hello C#3.0"; Console.WriteLine(s);
//可声明数组 var num = new[] { 0, 1, 2 }; foreach (int n in num) { Console.WriteLine(n); } //也可以这样输出 foreach (var n in num) { Console.WriteLine(n); } //声明对象 var student = new { Name = "萧秋水", Age = 23 }; Console.WriteLine("大侠{0}",student.Name); } } class TestVar { //不能声明类成员 // var s = "abc"; public void TestMethod() { //可以声明方法局部变量 var n = 10; } } } 总结: 1. 使用 var来进行声明变量,声明的变量的类型由初始化此变量值的类型决定 2.声明必须初始化,并且不能初始为空 */ desktopPane = new JDesktopPane(); contentPane.add(desktopPane);
setSize(350, 350); show(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } /*产生一个可关闭、可改变大小、具有标题、可最大化与最小化的Internal Frame. */ public void actionPerformed(ActionEvent e) { JInternalFrame internalFrame = new JInternalFrame( "Internal Frame "+(count++), true, true, true, true);
internalFrame.setLocation( 20,20); internalFrame.setSize(200,200); internalFrame.setVisible(true); //取得JInternalFrame的Content Pane,用以加入新的组件。
Container icontentPane = internalFrame.getContentPane(); JTextArea textArea = new JTextArea(); JButton b = new JButton("Internal Frame Button"); /*将JTextArea与JButton对象加入JInternalFrame中。由此呆知,JInteranlFrame加入组件 *的方式与JFrame是一模一样。 */ icontentPane.add(textArea,"Center"); icontentPane.add(b,"South"); //将JInternalFrame加入JDesktopPane中,如此一来,即使产生很多JInternalFrame,JDesktopPane也 //能将它们之间的关系管理得相当良好。 desktopPane.add(internalFrame); try { internalFrame.setSelected(true); } catch (java.beans.PropertyVetoException ex) { System.out.println("Exception while selecting"); } }
public static void main(String[] args) { new JInternalFrame1(); } }
3.此变量不能再次赋值为其它数据类型值 4.可声明数组 5.可声明对象 6.不能声明类成员,只能方法局部变量
|