《網際網路程式設計(Java)》——課程筆記1:JAVA圖形視窗程式設計
教學與實踐目的:學會Java圖形介面的設計及標準輸入輸出方法。
一、程式設計第一步:在指定的區域錄入資訊,到指定的區域顯示該資訊。
圖形介面如圖1所示。
知識點:基於介面的Java字串讀寫技術。
圖1 使用者介面
程式技術:
用一個Java窗體類來實現,並完成上述的任務。
製作過程:
(1) 啟動NetBeans平臺,新建一個專案,選“JVAVA應用程式”,並命名該專案,如JavaApp,並完成專案建立所需的其它工作。然後在專案名下新建一個java包,如basicIO,在該包名目下新建一個JFrame窗體程式,命名為IOJFrame;
(2) 在IOJFrame的設計介面上分別選右邊“元件面板”中的“標籤”“文字區域”“文字欄位”“按鈕”拖放到IOJFrame的設計介面上,並按圖1命名各元件。
先試執行IOJFrame.java程式,成功後進行下面的工作。若出現“沒有主類”等不能執行的資訊,則刪除原有專案,重新建立新專案,建立過程中,除了命名和位置資訊外,不要改動其他的預設配置資訊。
(3) 滑鼠右擊“傳送”按鈕,如圖2,進入源程式程式碼設計區,設定事件響應動作。
圖2 右擊按鈕新增動作程式碼
按鈕動作響應程式碼:
String msg = jTextField1.getText();//從資訊錄入區中取出字串
jTextField1.setText(null); //資訊錄入區置空
jTextArea1.append("echo:"+msg +"\n"); //將獲取的字串顯示在資訊顯示區。
說明:其中‘\n’為換行顯示字元。在執行中若出現響應動作不如期望的,觀察元件的變數名(如jTextField1)是否一致。
(4) 在“退出”按鈕中,新增程式結束執行程式碼:
System.exit(0);
(5) 讓欄位錄入框jTextField1支援回車鍵和ALT鍵。
在介面設計模式下,選中jTextField1框,右擊滑鼠選中事件下的Action,進入程式碼設計區,呼叫傳送按鈕的功能:
this.jButton1ActionPerformed(evt);
在介面設計模式下,選中jTextField1框,右擊滑鼠選中事件下的Key進入程式碼設計區,呼叫傳送按鈕的功能:
if(evt.isAltDown())//在evt事件類中判斷Alt按鍵事件。
this.jButton1ActionPerformed(null);
對特殊按鍵事件的響應是介面程式設計中重要的技術,尤其是遊戲程式設計技術,能舉一反三,多練習幾個按鍵的動作設計。
我的實踐:
程式碼:
package basicIO;
import java.util.Date;
/**
*
* @author
*/
public class IOJFrame extends javax.swing.JFrame {
/**
* Creates new form IOJFrame
*/
FileWrite fw = new FileWrite();
public IOJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("資訊顯示區");
jLabel2.setText("資訊讀取區");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jTextField1KeyPressed(evt);
}
});
jButton1.setText("傳送");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("退出");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(97, 97, 97)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
.addComponent(jTextField1))))
.addContainerGap(52, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(4, 4, 4)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
this.jButton1ActionPerformed(evt); // TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String msg = jTextField1.getText();//從資訊錄入區中取出字串
jTextField1.setText(null); //資訊錄入區置空
jTextArea1.append(msg +"\n"); //將獲取的字串顯示在資訊顯示區。
fw.append(new Date()+msg);//儲存聊天資訊到磁碟檔案.
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); // TODO add your handling code here:
fw.close();
}
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
if(evt.isAltDown())//在evt事件類中判斷Alt按鍵事件。
this.jButton1ActionPerformed(null);
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(IOJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(IOJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(IOJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(IOJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new IOJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}