Array ของวัตถุในภาษา Java (Array of objects)

  Array ของวัตถุในภาษา Java (Array of objects)           ทุกคนที่เขียน Java มาแล้วคงรู้จัก โครงสร้างข้อมูลแบบ Array ทั้ง 1, 2 หรือ 3 มิติ (Dimensions) (หรือตัวเลขต่อท้าย - subscripts) โดยทั่วไปชนิดของข้อมูลที่นำมาใช้จะเป็นข้อมูลพื้นฐาน (Primitive data types) ที่จะเขียนในตอนนี้คือ array ของ วัตถุ (Objects) ตัวอย่างเริ่มสร้าง class ของ object ขึ้นมาก่อน   public class Xyz { private int score;           public Xyz(int n) //Constructor           {                     score = n;           }           int getScore()           {                    return score;           } }     Compile ซะให้เรียบร้อย ต่อมาก็สร้าง สร้าง object และเรียกใช้งาน เขียนให้ดู 2 แบบคือ object ธรรมดา 3 Continue reading →

ตัวอย่างการรับชื่อด้วย Text field และแสดงผลที่ Status bar

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ShowName extends JApplet implements ActionListener { JLabel EnterName;    JTextField NameField; JButton enterButton;                                   public void init() {  Container c = getContentPane();                    c.setLayout(new FlowLayout());                   EnterName = new JLabel( "Enter your name " );  c.add(EnterName);  NameField = new JTextField( 40 );  c.add(NameField); // Enter button  enterButton = new JButton( "Enter name" );  enterButton.addActionListener( this );     c.add(enterButton);             }// end init public void actionPerformed( ActionEvent action ) {     showStatus("Your name is "+NameField.getText()); } // end actionPerformed } // end Class อย่าลืมสร้าง Continue reading →