轉 用Java語言編程

來源:互聯網
上載者:User
用Java語言編程

下面我們舉一個Java的基於GUI的應用程式例子(用Java 開發的文字編輯器)。

首先通過項目嚮導和應用嚮導建立項目,然後應用可視化設計工具,修改UI設計,串連事件,編輯源碼以及常用控制項和任務諸如功能表項目、工具條、文本地區和系統事件等常用控制項和任務的處理。具體技術包括:

* 用JFileChooser 對話方塊讓使用者選擇文字檔。

* 用JtextArea讀、寫和處理文字檔中的文字。

* 設定前景色彩和背景色。

* 用dbSwing FontChooser對話方塊設定字型。

* 在狀態列和視窗標題列顯示資訊。

* 手工添加處理UI事件的代碼 。

* 通過將代碼放在一個可被功能表項目和按鈕兩個事件處理器調用的新的“協助”方法中,使得功能表項目和按鈕執行相同的代碼。

* 給JtextArea控制項增加一個右擊菜單。

* 保持對檔案的位置以及檔案是否活動過的跟蹤,展示對檔案|建立,檔案|開啟,檔案|儲存,檔案|另存新檔,編輯和退出等邏輯的處理。

* 將"Text Editor" 應用程式展開為JAR 檔案。

Swing組件介紹

在講解來源程式之前,介紹一下Swing組件(這些組件將在後面的來源程式中用到),Swing是一個帶有豐富組件的GUI工具包,它組成了JFC(Java Foundation Class)的使用者介面功能的核心部分,它不僅是AWT所提供的組件的替代品,並且在這些組件的基礎上有了很大進步。Swing 都是100% 純Java的,不依賴具體的Windows系統,可以在各種平台上實現。Swing 中最迷人的是它對可插入觀感PL&F(Pluggable look and feel)的支援,可以讓使用者定製自己的案頭,更換新的顏色方案,讓視窗系統適應您的習慣和需要。Swing PL&F體繫結構使得同時定製Swing控制項或控制群組更加容易。Swing也提供了一些預定義的外觀(PL&F),包括預設、Motif 和 Windows 的外觀。

Swing包含了250多個類,提供了40多個組件,是AWT組件的四倍,Swing還提供了大量有助於開發GUI介面所需的附加組件,它們都定義在javax.swing擴充包中。為了和原來的java.awt包區別開來,SUN專門為Swing設定了擴充包(javax.swing),並規定所有的Swing組件都是以字母“J”開頭,如JLable、JButton、JTextField等,以示和AWT組件進行區別。下面是Swing組件的類階層。


JComponent  |——AbstractButton——|——JToggleButton——|——JCheckBox  |——JColorChooser     |——JButton          |——JRadioButton  |——JFileChooser      |——JMenuItem    |——JLabel                    |——JMenu  |——JMenuBar                  |——JRadioButtonMenuItem    |——JPanel                    |——JCheckButtonMenuItem  |——JToolBar  |——JScrollPane  |——JTextCompoment——|——JTextArea  |——JOptionPane       |——JTextField——JPasswordField  |——JScrollPane       |——JEditorPane——JTextPane   .........   .........

組件的含義同AWT的同類的組件意義類似,這裡就不在闡述了。

文字編輯器程式包含三個java 來源程式即TextEditFrame.java、TextEditclass.java 和TextEditFrame_AboutBox.java 程式,下面將分別介紹如下:

TextEditFrame.java的來源程式代碼(節選部分)


package texteditor;//TextEditFrame.javaimport java.awt.*;//匯入類import java.awt.event.*;import javax.swing.*;import com.borland.dbswing.*;import java.io.*;import javax.swing.text.*;import javax.swing.event.*;public class TextEditFrame extends JFrame {  IntlSwingSupport intlSwingSupport1 = new IntlSwingSupport(); //Swing 控制項互連網化:即本地化應用程式,需要添加一行代碼以便Swing 控制項JfileChooser //和JcolorChooser出現在程式啟動並執行語言中  JPanel contentPane; //設定內容窗(contentPane)的JPanel控制項  JMenuBar menuBar1 = new JMenuBar();//建立菜單條並加入到架構表單中  JMenu menuFile = new JMenu();//建立File菜單和相應的功能表項目  JMenuItem menuFileExit = new JMenuItem();  JMenu menuHelp = new JMenu();//建立Help菜單和相應的功能表項目  JMenuItem menuHelpAbout = new JMenuItem();  JToolBar toolBar = new JToolBar();//建立工具條組件  JButton jButton1 = new JButton();//建立按鈕組件  JButton jButton2 = new JButton();  JButton jButton3 = new JButton();  ImageIcon image1;//定義表徵圖  ImageIcon image2;  ImageIcon image3;  JLabel statusBar = new JLabel();//建立標籤組件  BorderLayout borderLayout1 = new BorderLayout();//建立BorderLayout 布局器  JScrollPane jScrollPane1 = new JScrollPane();//建立滾動窗控制項  JTextArea jTextArea1 = new JTextArea();//建立多行文本域組件  JMenuItem jMenuItem1 = new JMenuItem();//建立功能表項目  JMenuItem jMenuItem2 = new JMenuItem();  JMenuItem jMenuItem3 = new JMenuItem();  JMenuItem jMenuItem4 = new JMenuItem();  FontChooser fontChooser1 = new FontChooser();//建立字型選擇對話方塊  JMenu jMenu1 = new JMenu();  JMenuItem jMenuItem5 = new JMenuItem();  JMenuItem jMenuItem6 = new JMenuItem();  JMenuItem jMenuItem7 = new JMenuItem();  JFileChooser jFileChooser1 = new JFileChooser();//建立文本選擇對話方塊  String currFileName = null;  // Full path with filename. null means new/untitled.  boolean dirty = false;  Document document1;  //文本  DBTextDataBinder dBTextDataBinder1 = new DBTextDataBinder();    // True means modified text.  //構造架框  public TextEditFrame() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();      updateCaption();    }    catch(Exception e) {      e.printStackTrace();    }  }  //組件初始化  private void jbInit() throws Exception  {     //三個工具列按鈕表徵圖    image1 = new ImageIcon(TextEditFrame.class.getResource("openFile.gif"));    image2 = new ImageIcon(TextEditFrame.class.getResource("closeFile.gif"));    image3 = new ImageIcon(TextEditFrame.class.getResource("help.gif"));    contentPane = (JPanel) this.getContentPane();//內容創格    document1 = jTextArea1.getDocument();//多行文本域文檔    contentPane.setLayout(borderLayout1);//borderLayout布局器    this.setSize(new Dimension(400, 300));//視窗大小    this.setTitle("Text Editor");//視窗標題    statusBar.setText(" ");    menuFile.setText("File");    menuFileExit.setText("Exit");    menuFileExit.addActionListener(new TextEditFrame_menuFileExit_ActionAdapter        (this));//添加事件監聽器    menuHelp.setText("Help");    menuHelpAbout.setText("About");    menuHelpAbout.addActionListener(new TextEditFrame_menuHelpAbout_ActionAdapter    (this));    jButton1.setIcon(image1);//設定三個工具列按鈕表徵圖,添加事件監聽器    jButton1.addActionListener(new TextEditFrame_jButton1_actionAdapter(this));    jButton1.setToolTipText("Open File");    jButton2.setIcon(image2);    jButton2.addActionListener(new TextEditFrame_jButton2_actionAdapter(this));    jButton2.setToolTipText("Close File");    jButton3.setIcon(image3);    jButton3.addActionListener(new TextEditFrame_jButton3_actionAdapter(this));    jButton3.setToolTipText("About");    jTextArea1.setLineWrap(true);    jTextArea1.setWrapStyleWord(true);    jTextArea1.setBackground(Color.white);    jMenuItem1.setText("New");//設定菜單,添加事件監聽器    jMenuItem1.addActionListener(new TextEditFrame_jMenuItem1_actionAdapter(this));    jMenuItem2.setText("Open");    jMenuItem2.addActionListener(new TextEditFrame_jMenuItem2_actionAdapter(this));    jMenuItem3.setText("Save");    jMenuItem3.addActionListener(new TextEditFrame_jMenuItem3_actionAdapter(this));    jMenuItem4.setText("Save As");    jMenuItem4.addActionListener(new TextEditFrame_jMenuItem4_actionAdapter(this));    fontChooser1.setFrame(this);    fontChooser1.setTitle("Font");    jMenu1.setText("Edit");    jMenuItem5.setText("Font");    jMenuItem5.addActionListener(new TextEditFrame_jMenuItem5_actionAdapter(this));    jMenuItem6.setText("Foreground Color");    jMenuItem6.addActionListener(new TextEditFrame_jMenuItem6_actionAdapter(this));    jMenuItem7.setText("Background Color");    jMenuItem7.addActionListener(new TextEditFrame_jMenuItem7_actionAdapter(this));    document1.addDocumentListener(new TextEditFrame_document1_documentAdapter(this));    dBTextDataBinder1.setJTextComponent(jTextArea1);    //Turn off right-click file Open... menu item.    dBTextDataBinder1.setEnableFileLoading(false);    //Turn off right-click file Save... menu item.    dBTextDataBinder1.setEnableFileSaving(false);    toolBar.add(jButton1);//工具組件添加按鈕    toolBar.add(jButton2);    toolBar.add(jButton3);    menuFile.add(jMenuItem1);//菜單組件添加功能表項目    menuFile.add(jMenuItem2);    menuFile.add(jMenuItem3);    menuFile.add(jMenuItem4);    menuFile.addSeparator();//采單組件添加分隔線    menuFile.add(menuFileExit);    menuHelp.add(menuHelpAbout);    menuBar1.add(menuFile);    menuBar1.add(jMenu1);    menuBar1.add(menuHelp);    this.setJMenuBar(menuBar1);    contentPane.add(toolBar, BorderLayout.NORTH); //內容窗設定borderLayout布局器    contentPane.add(statusBar, BorderLayout.SOUTH);    contentPane.add(jScrollPane1, BorderLayout.CENTER);    jScrollPane1.getViewport().add(jTextArea1, null);    jMenu1.add(jMenuItem5);    jMenu1.add(jMenuItem6);    jMenu1.add(jMenuItem7);  }  // Display the About box.  void helpAbout() {    TextEditFrame_AboutBox dlg = new TextEditFrame_AboutBox(this);    Dimension dlgSize = dlg.getPreferredSize();    Dimension frmSize = getSize();    Point loc = getLocation();    dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);    dlg.setModal(true);    dlg.show();  }..................

編程技巧說明:

TextEditFrame.java 是實現文字編輯器的主要程式,有以下特點:

(1) 製作一個完全充滿使用者介面頂部功能表列和底部狀態列之間地區的文本區。主使用者介面容器的布局管理器需要採用邊界布局(Borderlayout)。在主容器中,含有一個叫做內容窗(contentPane)的JPanel 控制項,被改變成邊界布局,需要做的只是在內容窗添加一個文本區控制項。為此,先在內容窗添加一個滾動窗,再在滾動窗內放上文本區控制項(jTextArea)。滾動窗提供一個帶滾動棒(JScollPane)的文本區。

相關文章

聯繫我們

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