我的第一個java圖形化使用者介面程式

來源:互聯網
上載者:User

java,我新手,最近才開始學習。所以經常碰到很多問題。

在編寫下面的第一個java圖形介面程式的時候,我在網上看到過說要配置什麼swt路徑問題。原來swt外掛程式是用於最佳化和美觀圖形介面的,像我下面的是用java基礎包寫的程式,沒有用到swt外掛程式。就沒必要配置swt路徑。

下面解釋一下我的第一個圖形畫介面程式:

顯示一個整數的各位元字。示範一個帶圖形化使用者介面的完整應用程式,包括輸入資料,顯示結果,響應事件和異常處理。

package study;
import java.awt.*;
import java.awt.event.*;;
public class DigitFrame extends Frame implements ActionListener,WindowListener{
 private TextField text_input,text_hundred,text_decade,text_digit;
 private Button button_ok;
 private Dialog dialog;
 private Label label_dialog;
 public DigitFrame()
 {
  super("顯示整數數字");
  this.setSize(190, 150);
  this.setResizable(false);        //視窗大小不能改變
  this.setBackground(java.awt.Color.lightGray);
  this.setLayout(new java.awt.FlowLayout(FlowLayout.LEFT));    //流布局且靠左對齊
  
  
  this.add(new Label("整數"));
  text_input=new TextField(10);
  this.add(text_input);
  text_input.addActionListener(this);   //為文本行註冊單擊事件監聽事件
  
  
  button_ok=new Button("OK");
  this.add(button_ok);
  button_ok.addActionListener(this);     //為按鈕註冊單擊事件監聽器
  
  
  this.add(new Label("百位"));
  text_hundred=new TextField(10);
  text_hundred.setEditable(false);     //只能顯示,不能編輯
  this.add(text_hundred);
  
  
  this.add(new Label("十位"));
  text_decade=new TextField(10);
  text_decade.setEditable(false);
  this.add(text_decade);
  
  
  this.add(new Label("個位"));
  text_digit=new TextField(10);
  text_digit.setEditable(false);
  this.add(text_digit);
  
  
  this.addWindowListener(this);
  this.setVisible(true);
  
  
  dialog=new Dialog(this,"提示",true);      //強制回應視窗
  dialog.setSize(240,80);
  label_dialog=new Label("",Label.CENTER);  //標籤的字串為空白,置中對齊
  dialog.add(label_dialog);
  dialog.addWindowListener(this);    //為對話方塊註冊視窗事件監聽器
  }
  public void actionPerformed(ActionEvent e)   //按鈕單擊,文本行中單擊斷行符號健 
  {
   try
   {
    final int i=Integer.parseInt(text_input.getText());
    text_hundred.setText(""+(i/100));       //百位
    text_decade.setText(""+(i/10%10));      //十位
    text_digit.setText("" +(i%10));         //個位
   }
   catch(NumberFormatException nfe)
   {
    label_dialog.setText("/""+text_input.getText()+"/""+"不能轉換成整數,請重新輸入!");
    dialog.setLocation(this.getX()+100,this.getY()+100);
    dialog.setVisible(true);
   }
   finally {}
  }
  public void windowClosing(WindowEvent e)
  {
   if(e.getSource()==dialog)
    dialog.setVisible(false);   //隱藏對話方塊
   else
    System.exit(0);
  }
  public void windowOpened(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public static void main(String[] args) {
    new DigitFrame();

 }

}

程式設計說明如下:

視窗關閉事件不能委託其他類處理

由於對話方塊也要響應視窗關閉事件,在windowClosing方法體中,需要識別當前事件來源時架構還是對話方塊,所以本例的視窗關閉事件不能委託其他類處理。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.