JAVA (1)–第一個GUI程式 添加標題 關閉視窗 螢幕位置 匯入表徵圖

來源:互聯網
上載者:User

標籤:

 

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();       } }

 

 

運行結果如下:能夠最小化 最大化 但是不能關閉

 

解決問題之一:添加一個標題

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);    }}

運行結果如下: 

單擊視窗B 兩個都關閉

要是先單擊A,則它只關閉自己的視窗

 

小知識:

假設:螢幕的解析度是1024×768 

螢幕的最左上方的座標是(0,0)  最右下角的座標是(1024,768)

東北方向的座標是(1024,0)

JAVA (1)–第一個GUI程式 添加標題 關閉視窗 螢幕位置 匯入表徵圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.