JAVA (1)–第一个GUI程序 添加标题 关闭窗口 屏幕位置 导入图标

import java.awt.*;                                // 可以改成  import  javax.swing.*;                    

public class FirstFrame {

    public static void main( String[] args ) {

        Frame f = new Frame();                       //可以改成  JFrame f = new JFrame();

        f.setSize( 300, 200 );

        f.setVisible(true);

    }

}                                                  //Frame 的意思是框架
import java.awt.*;   
public class First extends Frame {
      public First(){

          
     // super("新建文本");                         //设置标题
     //   this.setBounds(200, 1111, 1024,500);   //设置大小

    //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭

                                                     
  
                  setSize( 100, 100);
      this.setVisible(true);                  //设置可见

    }

       public static void main(String[] args){

        new First();

       } }

运行结果如下:能够最小化 最大化 但是不能关闭

wpsFC79.tmp

解决问题之一:添加一个标题

import java.awt.*;                   // 可以改成  import javax.swing.*;                      
public class FirstFrame {
    public static void main( String[] args ) {
        Frame f = new Frame();           //可以改成  JFrame f = new JFrame();
        f.setSize( 300, 200 );
        f.setVisible(true);
        
        f.setTitle("New title");             //也可以这样变更窗口标题
        f.setVisible(true);
    }
}

链接:可以给标题旁边添加logo

图标链接:http://wangyali90516.blog.163.com/blog/static/117205101201231632140788/

解决问题之二:如何关闭窗口

import javax.swing.JFrame;

import static javax.swing.JFrame.*;//引入JFramed的静态常量

public class First {

public static void main(String[] args) {   

 JFrame window1 = new JFrame("窗口A");//创建带标题的窗口    
 
 JFrame window2 = new JFrame("窗口B");  

  window1.setBounds(600, 100, 180, 100);//左,上,宽,高  
  
  window2.setBounds(260, 100, 180, 100);  
  
  window1.setVisible(true);   
 
 window2.setVisible(true);  
  window1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);  
  
  window2.setDefaultCloseOperation(EXIT_ON_CLOSE);    }}

运行结果如下:  image

单击窗口B 两个都关闭

要是先单击A,则它只关闭自己的窗口

小知识:

假设:屏幕的分辨率是1024×768 

屏幕的最左上角的坐标是(0,0)  最右下角的坐标是(1024,768)

东北方向的坐标是(1024,0)

原文地址:https://www.cnblogs.com/cs-lcy/p/4588298.html