1. 程式人生 > 其它 >1.建立視窗

1.建立視窗

1.建立一個視窗

import java.awt.*;

public class TestFrame {
   public static void main(String[] args) {
       //建立一個視窗
       Frame frame = new Frame("第一個視窗");
       //設定可見性
       frame.setVisible(true);
       //設定座標大小
       frame.setBounds(500,500,500,500);
       //設定視窗顏色
       frame.setBackground(Color.blue);
  }
}

 

2.建立多個視窗

import java.awt.*;

public class TestFrame2 {
   public static void main(String[] args) {
       for (int i = 1; i <=3; i++) {//通過迴圈建立視窗
           new MyFrame(i+50,i+50,500,500,Color.blue);
      }
  }

}
class MyFrame extends Frame {//建立一個類,繼承Frame類
   static int id =0;
   public MyFrame(int x,int y,int w,int h,Color color){//有參構造方法
       super("" + ++id);//呼叫父類構造方法
       setVisible(true);//設定可見性
       setBounds(x,y,w,h);//設定視窗座標,大下
       setBackground(color);//設定背景顏色
  }
}